我只是想看看使用子流程模块使用AWS cli命令是否有效。运行以下命令可成功输出
vpc = subprocess.run("aws ec2 describe-vpcs | jq -r '.Vpcs[].VpcId'", stdout=subprocess.PIPE, shell=True)
当在jq中运行withselect语句时,如下所示会出现错误。
subnets = subprocess.run("aws ec2 describe-subnets | jq -r '.Subnets[] | select(.Tags[].Value == "az1") .SubnetId'", stdout=subprocess.PIPE, shell=True)
错误为
File "awsinit.py", line 42
subnets = subprocess.run("aws ec2 describe-subnets | jq -r '.Subnets[] | select(.Tags[].Value == "az1") .SubnetId'", stdout=subprocess.PIPE, shell=True)
^
SyntaxError: invalid syntax
有没有关于解决这个问题的指南或其他建议?
修复引号将帮助您解决问题
subnets = subprocess.run('''aws ec2 describe-subnets | jq -r '.Subnets[] | select(.Tags[].Value == "az1") .SubnetId' ''', stdout=subprocess.PIPE, shell=True)