Ansible 传递哈希以包含模块
Ansible passing hash to include module
我在传递一些复合数据(如 YAML 映射)作为 ansible 包含模块的输入时遇到问题。
这是我的测试:
test.yml
---
- hosts: all
gather_facts: no
vars:
test_var:
test1:
hello: world
hi: people
test2:
hello2: world2
hi2: people2
tasks:
- debug: "msg='Variable item.value is: {{ item.value }}'"
with_dict: "{{ test_var.test1 }}"
- include: "test1.yml test_variable={{ item.value }}"
with_dict: "{{ test_var.test1 }}"
test1.yml
---
- debug: "var={{ test_variable }}"
这是输出。注意部分:"people": "VARIABLE IS NOT DEFINED!"
$ ansible-playbook -i localhost, test.yml
PLAY ***************************************************************************
TASK [debug] *******************************************************************
ok: [localhost] => (item={'value': u'people', 'key': u'hi'}) => {
"item": {
"key": "hi",
"value": "people"
},
"msg": "Variable item.value is: people"
}
ok: [localhost] => (item={'value': u'world', 'key': u'hello'}) => {
"item": {
"key": "hello",
"value": "world"
},
"msg": "Variable item.value is: world"
}
TASK [include] *****************************************************************
included: /home/mot/jira-scripts/ansible-playbooks/test1.yml for localhost
included: /home/mot/jira-scripts/ansible-playbooks/test1.yml for localhost
TASK [debug] *******************************************************************
ok: [localhost] => {
"people": "VARIABLE IS NOT DEFINED!"
}
TASK [debug] *******************************************************************
ok: [localhost] => {
"world": "VARIABLE IS NOT DEFINED!"
}
PLAY RECAP *********************************************************************
localhost : ok=5 changed=0 unreachable=0 failed=0
只需更改 test1.yml 文件
---
- debug: var=test_variable
它适合我。
我在传递一些复合数据(如 YAML 映射)作为 ansible 包含模块的输入时遇到问题。
这是我的测试:
test.yml
---
- hosts: all
gather_facts: no
vars:
test_var:
test1:
hello: world
hi: people
test2:
hello2: world2
hi2: people2
tasks:
- debug: "msg='Variable item.value is: {{ item.value }}'"
with_dict: "{{ test_var.test1 }}"
- include: "test1.yml test_variable={{ item.value }}"
with_dict: "{{ test_var.test1 }}"
test1.yml
---
- debug: "var={{ test_variable }}"
这是输出。注意部分:"people": "VARIABLE IS NOT DEFINED!"
$ ansible-playbook -i localhost, test.yml
PLAY ***************************************************************************
TASK [debug] *******************************************************************
ok: [localhost] => (item={'value': u'people', 'key': u'hi'}) => {
"item": {
"key": "hi",
"value": "people"
},
"msg": "Variable item.value is: people"
}
ok: [localhost] => (item={'value': u'world', 'key': u'hello'}) => {
"item": {
"key": "hello",
"value": "world"
},
"msg": "Variable item.value is: world"
}
TASK [include] *****************************************************************
included: /home/mot/jira-scripts/ansible-playbooks/test1.yml for localhost
included: /home/mot/jira-scripts/ansible-playbooks/test1.yml for localhost
TASK [debug] *******************************************************************
ok: [localhost] => {
"people": "VARIABLE IS NOT DEFINED!"
}
TASK [debug] *******************************************************************
ok: [localhost] => {
"world": "VARIABLE IS NOT DEFINED!"
}
PLAY RECAP *********************************************************************
localhost : ok=5 changed=0 unreachable=0 failed=0
只需更改 test1.yml 文件
---
- debug: var=test_variable
它适合我。