Почему аргументы разбиваются на «=», когда terraform запускается из подпроцесса python

У меня есть оболочка Python для terraform, 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

__Примечание: __ ass, наблюдаемый ниже, при запуске простого эха (или любого другого двоичного файла) он правильно интерпретировал бы все аргументы, по какой-то причине terraform решил разделить аргумент -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$ 

person Veaceslav Mindru    schedule 28.03.2016    source источник
comment
Вы уверены, что это не должно быть -foo=bar   -  person Brendan Abel    schedule 28.03.2016
comment
Вы пробовали что-то вроде "-var", "'foo=bar'"?   -  person nightuser    schedule 29.03.2016
comment
Как указывает nightuser, и TF четко указывает в приведенном выше выводе, что вам нужно заключить определение var в кавычки.   -  person ydaetskcoR    schedule 29.03.2016
comment
можете ли вы показать вывод при работе с кавычками вокруг определения var?   -  person ydaetskcoR    schedule 29.03.2016
comment
@nightuser - разделение их помогло, но простое заключение в кавычки не помогает.   -  person Veaceslav Mindru    schedule 29.03.2016
comment
@ydaetskcoR pastebin.com/BfhLSaJC   -  person Veaceslav Mindru    schedule 29.03.2016
comment
@VeaceslavMindru, так ты решил свою проблему?   -  person nightuser    schedule 29.03.2016
comment
@nightuser да, спасибо! по крайней мере, это не ошибка в том, что было до сих пор.   -  person Veaceslav Mindru    schedule 29.03.2016
comment
@ydaetskcoR и nightuser спасибо, ребята!   -  person Veaceslav Mindru    schedule 29.03.2016
comment
@ydaetskcoR, вы можете опубликовать свое решение как ответ и отметить его как принятое. Кто-то может найти вашу проблему (и решение) полезной.   -  person nightuser    schedule 30.03.2016


Ответы (1)


Отметим это для облегчения поиска в будущем, хотя вся заслуга принадлежит @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"]
person Ewan    schedule 02.04.2016