在下面的任务中:
- name: Create a data container
docker_container:
name: mydeploycontainer
image: 1111112222.dkr.ecr.us-east-1.amazonaws.com/someteam/app-deploy:v.1
env:
name1: {{name1_var}}
name2: {{name2_var}}
name3: {{name3_var}}
env:
部分仅允许带双引号的值,例如:
env:
name1: "value1"
但这些是基于传递给剧本的输入的动态值:
--extra-vars '{ "name1_var": "value_1", "name2_var": "value_2", "name3_var": "value_3" }'
如何将变量传递到环境部分?
答案1
太长了;
所有 jinja2 模板表达式必须被引用在你的剧本中,当他们以双花括号开头否则你会得到一个语法错误(我猜你会得到这个)
您应该像下面这样编写您的任务以清除错误并将您的环境设置为传递的额外变量:
- name: Create a data container
docker_container:
name: mydeploycontainer
image: 1111112222.dkr.ecr.us-east-1.amazonaws.com/someteam/app-deploy:v.1
env:
name1: "{{ name1_var }}"
name2: "{{ name2_var }}"
name3: "{{ name3_var }}"
作为一个好的实践,当字符串包含 jinja2 模板表达式时,即使它们不以双花括号开头(例如path: "/fix/path/{{ var_file_name }}"
),您也应该始终引用它们
欲了解更多信息,请参阅Ansible yaml 语法陷阱
POC
这是我用 ansible 2.8.1 编写和使用的概念证明,向您展示它按预期工作。要重播,请确保您pip install jmespath
使用了我json_query
用于调试的值。
剧本test.yml
:
---
- name: Test passing vars to docker from command line
hosts: localhost
gather_facts: false
tasks:
- name: Spin up a container
docker_container:
state: started
image: busybox:latest
command: sh -c "while true; do sleep 1000; done"
name: test_for_so
env:
oneenv: "{{ oneenv_value }}"
otherenv: "{{ otherenv_value }}"
- name: Get env values from container
command: >-
docker exec test_for_so sh -c 'echo ${{ item }}'
register: env_out
loop:
- oneenv
- otherenv
- name: Display collected values
debug:
msg: "{{ env_out.results | json_query('[].{env: item, value: stdout}') }}"
命令:
ansible-playbook test.yml \
-e '{"oneenv_value": "This is value 1", "otherenv_value": "This is value 2"}'
结果:
PLAY [Test passing vars to docker from command line] **********************************************************************************************************************************************************************
TASK [Spin up a container] ************************************************************************************************************************************************************************************************
changed: [localhost]
TASK [Get env values from container] *************************************************************************************************************************************************************************************
changed: [localhost] => (item=oneenv)
changed: [localhost] => (item=otherenv)
TASK [Display collected values] *******************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": [
{
"env": "oneenv",
"value": "This is value 1"
},
{
"env": "otherenv",
"value": "This is value 2"
}
]
}
PLAY RECAP ****************************************************************************************************************************************************************************************************************
localhost : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
柜台POC
如果您删除 env 部分中 jinja2 模板字符串周围的引号(如您的示例中所示),则会发生以下情况:
ERROR! Syntax Error while loading YAML.
found unacceptable key (unhashable type: 'AnsibleMapping')
The error appears to be in '/home/user/test.yml': line 15, column 20, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
env:
oneenv: {{ oneenv_value }}
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
答案2
这可能会达到你想要的效果 ( ansible 2.8
)
-e 'name1_var=""testa"" name2_var=""testb"" name1_var3=""testc""'