python - 当 terraform 从 python 子进程运行时,为什么 args 被 '=' 分割

标签 python terraform

我有一个 terraform 的 Python 包装器,terraform 会以某种方式误解它所传递的参数。有什么想法可以解决这个问题吗?

#!/usr/bin/env python
import subprocess

args_echo = ["/bin/echo", "/home/vmindru/proj/tera/terraform",
             "plan",
             "-var foo=bar"]
args_terr = ["/home/vmindru/proj/tera/terraform",
             "plan",
             "-no-color",
             "-var foo=bar"]
echo = subprocess.call(args_echo)
terr = subprocess.call(args_terr)

print echo, terr

__注意:在运行简单的 echo (或任何其他二进制文件)时观察到的 __ ass 会正确解释所有参数,出于某种原因,terraform 决定将 arg -var foo=bar 拆分为 -var foo 和可能的 bar

vmindru@vmhodesk:/tmp/test2$ ./test.py 
/home/vmindru/proj/tera/terraform plan -var foo=bar
flag provided but not defined: -var foo
Usage: terraform plan [options] [dir]

  Generates an execution plan for Terraform.

  This execution plan can be reviewed prior to running apply to get a
  sense for what Terraform will do. Optionally, the plan can be saved to
  a Terraform plan file, and apply can take this plan file to execute
  this plan exactly.

Options:

  -backup=path        Path to backup the existing state file before
                      modifying. Defaults to the "-state-out" path with
                      ".backup" extension. Set to "-" to disable backup.

  -destroy            If set, a plan will be generated to destroy all resources
                      managed by the given configuration and state.

  -detailed-exitcode  Return detailed exit codes when the command exits. This
                      will change the meaning of exit codes to:
                      0 - Succeeded, diff is empty (no changes)
                      1 - Errored
                      2 - Succeeded, there is a diff

  -input=true         Ask for input for variables if not directly set.

  -module-depth=n     Specifies the depth of modules to show in the output.
                      This does not affect the plan itself, only the output
                      shown. By default, this is -1, which will expand all.

  -no-color           If specified, output won't contain any color.

  -out=path           Write a plan file to the given path. This can be used as
                      input to the "apply" command.

  -parallelism=n      Limit the number of concurrent operations. Defaults to 10.

  -refresh=true       Update state prior to checking for differences.

  -state=statefile    Path to a Terraform state file to use to look
                      up Terraform-managed resources. By default it will
                      use the state "terraform.tfstate" if it exists.

  -target=resource    Resource to target. Operation will be limited to this
                      resource and its dependencies. This flag can be used
                      multiple times.

  -var 'foo=bar'      Set a variable in the Terraform configuration. This
                      flag can be set multiple times.

  -var-file=foo       Set variables in the Terraform configuration from
                      a file. If "terraform.tfvars" is present, it will be
                      automatically loaded if this flag is not specified.
0 1
vmindru@vmhodesk:/tmp/test2$ 

最佳答案

记下这一点,以便将来轻松查找,但完全归功于 @nightuser 在问题评论中的回答。

Python subprocess.call() 函数要求参数中不存在空格,并且任何空格都应该是列表中的单独元素。

在这种情况下:

args_echo = ["/bin/echo", "/home/vmindru/proj/tera/terraform",
             "plan",
             "-var foo=bar"]

变成:

args_echo = ["/bin/echo", "/home/vmindru/proj/tera/terraform",
             "plan",
             "-var",
             "foo=bar"]

关于python - 当 terraform 从 python 子进程运行时,为什么 args 被 '=' 分割,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36270788/

相关文章:

terraform 子模块更改未在计划中识别或应用

数据 template_file 的 Terraform 插值语法错误

python - 为什么使用 Python 的 os 模块方法而不是直接执行 shell 命令?

python - 通过SimpleHTTPServer调用python脚本将数据插入MYSQL数据库

ansible - 如何自动配置 Azure B2C?

google-cloud-platform - 无法使用 Terraform 创建具有静态 IP 的 GCE 实例

postgresql - 使用 Terraform 的 AWS RDS IAM 身份验证

Python 应用程序框架在应用程序内部创建 webview

python - 为什么这是 python 中的无限循环?

python - 在python中对实现ImageDraw的方法进行单元测试