Ansible传递哈希以包含模块

时间:2016-03-18 20:41:52

标签: ansible ansible-playbook ansible-2.x

我有问题将一些复合数据(如YAML映射)作为ansible include模块的输入传递。

这是我的测试:

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

1 个答案:

答案 0 :(得分:2)

只需更改test1.yml文件

即可

--- - debug: var=test_variable

它对我有用。