如何将字符串从python传递到AWS使用boto3运行Powershell脚本命令部分



是否有传递变量(var)在python中定义空闲AWS-RunPowerShellScript命令节?

下面是我的代码:
import boto3
ssm = boto3.client("ssm")
var = "test"
res = ssm.send_command(
DocumentName="AWS-RunPowerShellScript",
Targets=[
{
'Key': 'tag:test',
'Values': ['testing']
}
] 
Comment="Test",
Parameters={'commands':[
'hostname',
'$var'
]
}
)
在上面的代码中,我定义了变量var同样,我想在send_command的命令部分引用$var,但由于远程执行,它似乎不工作。是否有可能将变量从python传递到命令部分?

您可以在使用ssm_client调用send_command之前构建命令字符串。然后在参数中引用该变量。

此操作可用于任何类型的send_command,包括AWS-RunShellScriptAWS-RunPowershellScript

在上面的例子中,请注意您使用的'$var'实际上是一个字符串,因为它被包装在''中。还要注意,在Python中,$字符不用于引用变量。这是PHP的东西

import boto3
ssm_client = boto3.client('ssm')
# build your command string here
command = "echo 'hello world' > testfile.txt"
# send command
response = ssm_client.send_command(
DocumentName="AWS-RunShellScript",
Targets=[
{
'Key': 'tag:test',
'Values': ['testing']
}
],
# the command var is just a string after all
Parameters={'commands': [command]}
)
print(response)

最新更新