如何在同一任务中展开多个列表

时间:2015-08-23 08:18:37

标签: yaml ansible ansible-playbook

我有一种情况,我有两个不同的变量,我想在一个命令中引用它:

例如,我期待以下输出:

list 1
  item a
  item b
list 2
  another item from different var 10

-name : Run a module which executes a command on a host eg. via ssh
command:
  host: {{ device_ip }}
  cmd_str: 
   - 'list 1 '
    - '  {{ item item[0].name }}'
   - 'list 2 '
    - '  {{ another item from different var item[1].id }}'
  with_items:
   - {{ var1 }}
   - {{ var2 }}

var1:
   - { name:a, address:test }
   - { name:b, address:test2 }

var2:
   - { name:x, id:10 }

我要写什么而不是 " with_items" 来完成这项工作?

问题是我如何在同一个地方展开两个不同的变量,而不必迭代整个命令(如果我将with_items移动到与模块调用相同的缩进级别,这是可行的)

1 个答案:

答案 0 :(得分:3)

我无法理解你真正想做什么,但是下面的剧本演示了:

  • 使用dict
  • 在单个项目中传递多个变量
  • 使用Jinja2模板迭代每个var

playbook.yml:

---
- hosts: all
  gather_facts: no
  vars:
    var1:
      - { name: a, address: test }
      - { name: b, address: test2 }
    var2:
      - { name: x, id: 10 }
  tasks:
    - debug:
        msg: |
          list 1
          {% for x in item.1 %}
            item {{x.name}}
          {% endfor %}
          list 2
          {% for x in item.2 %}
            another item from different var {{x.id}}
          {% endfor %}
      with_items:
        - { 1: "{{var1}}", 2: "{{var2}}" }
    - shell: |
        >/tmp/output.txt # truncate file
        {% for x in item.1 %}
        echo item {{x.name}} >>/tmp/output.txt
        {% endfor %}
        {% for x in item.2 %}
        echo another item from different var {{x.id}} >>/tmp/output.txt
        {% endfor %}
      with_items:
        - { 1: "{{var1}}", 2: "{{var2}}" }

示例会话:

$ ansible-playbook -i localhost, playbook.yml 

PLAY [all] ******************************************************************** 

TASK: [debug ] **************************************************************** 
ok: [localhost] => (item={1: [{'name': 'a', 'address': 'test'}, {'name': 'b', 'address': 'test2'}], 2: [{'name': 'x', 'id': 10}]}) => {
    "item": {
        "1": [
            {
                "address": "test", 
                "name": "a"
            }, 
            {
                "address": "test2", 
                "name": "b"
            }
        ], 
        "2": [
            {
                "id": 10, 
                "name": "x"
            }
        ]
    }, 
    "msg": "list 1\n  item a\n  item b\nlist 2\n  another item from different var 10\n"
}

TASK: [shell >/tmp/output.txt # truncate file
{% for x in item.1 %}
echo item {{x.name}} >>/tmp/output.txt
{% endfor %}
{% for x in item.2 %}
echo another item from different var {{x.id}} >>/tmp/output.txt
{% endfor %}
] *** 
changed: [localhost] => (item={1: [{'name': 'a', 'address': 'test'}, {'name': 'b', 'address': 'test2'}], 2: [{'name': 'x', 'id': 10}]})

PLAY RECAP ******************************************************************** 
localhost                  : ok=2    changed=1    unreachable=0    failed=0   

来自msg模块的debug中显示的输出:

list 1
  item a
  item b
list 2
  another item from different var 10

来自/tmp/output.txt模块的shell输出:

item a
item b
another item from different var 10