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

标签 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 移动到与模块调用相同的缩进级别,这是可行的)

最佳答案

我无法理解您真正想要做什么,但以下剧本演示了:

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

  • 剧本.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}}" }
    

    示例 session :
    $ 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
    

    关于yaml - 如何在同一任务中展开多个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32164618/

    相关文章:

    parsing - 使用 YAML.mapping 解析复杂的 YAML 结构

    javascript - 在 JS 中仅从文件中获取 YAML 文本

    python - Ansible 根据键获取唯一的字典列表

    ansible - 在主 Ansible 剧本中指定变量

    ansible - 如果主机无法访问,则中止 ansible playbook

    ansible - 在任务中访问 ansible.cfg 变量

    amazon-web-services - 使用替换的顶级参数的类型

    python - 在 block 样式 yaml 文件 (ruamel.yaml) 中将来自 Python 的列表显示为流样式

    python - 指定操作系统 - Ansible

    ansible - 正则表达式匹配并转换为 Ansible 事实