小编Tra*_*vis的帖子

Ansible:迭代字典列表 - 循环与 with_items

尝试迭代字典列表时,使用循环与 with_items 时得到不同的结果。

我试过使用 loop|dict2items (结构不是字典,它告诉我很多。嘿)并使用 flatten 过滤器循环。

以下是字典列表:

    "msg": [
        {
            "id": "id1", 
            "ip": "ip1", 
            "name": "name1"
        }, 
        {
            "id": "id2", 
            "ip": "ip2", 
            "name": "name2"
        }, 
        {
            "id": "id3", 
            "ip": "ip3", 
            "name": "name3"
        }, 
        {
            "id": "id4", 
            "ip": "ip4", 
            "name": "name4"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

这是剧本中的任务:

 - name: Add privateIp windows_instances to inventory
        add_host:
          name: "{{ item.ip }}"
          aws_name: "{{ item.name }}"
          groups: windows_instances
          aws_instanceid: "{{ item.id }}"
          ansible_user: "{{ windows_user }}"
          ansible_password: "{{ windows_password }}"
          ansible_port: 5985
          ansible_connection: winrm …
Run Code Online (Sandbox Code Playgroud)

loops ansible

9
推荐指数
1
解决办法
2万
查看次数

Ansible - 组合两个以上列表时循环 + zip 的正确语法是什么?

组合超过 2 个列表时,我无法找到 loop + zip 的语法。

由于Ansible 2.5,如图这里,下面的语法内容替换with_together带环+ ZIP:

- name: with_together
  debug:
    msg: "{{ item.0 }} - {{ item.1 }}"
  with_together:
    - "{{ list_one }}"
    - "{{ list_two }}"

- name: with_together -> loop
  debug:
    msg: "{{ item.0 }} - {{ item.1 }}"
  loop: "{{ list_one|zip(list_two)|list }}"
Run Code Online (Sandbox Code Playgroud)

我的问题是,虽然在使用 with_together 时,您可以简单地附加列表,并用迭代数字引用它们,但我无法找到与循环 + zip 一起使用的方法。我试过了:

loop: "{{ list_one|zip(list_two)|zip(list_three)|zip(list_four)list }}"
Run Code Online (Sandbox Code Playgroud)

没有成功。

loops ansible

5
推荐指数
1
解决办法
1575
查看次数

标签 统计

ansible ×2

loops ×2