Ansible-----循环

with_dict迭代字典项

使用"with_dict"可以迭代字典项。迭代时,使用"item.key"表示字典的key,"item.value"表示字典的值。

---
    - hosts: localhost
      tasks:
        - debug: msg="{{item.key}} & {{item.value}}"
          with_dict: { address: 1,netmask: 2,gateway: 3 }

另一种情况,字典是已存储好的。例如ansible facts中的ansible_eth0.ipv4,其内容如下:

"ipv4": {
    "address": "192.168.100.65",
    "netmask": "255.255.255.0",
    "gateway": "192.168.100.2"
}

这种情况下,with_dict处可以直接指定该字典的key。即:

---
    - hosts: localhost
      tasks:
        - debug: msg="{{item.key}} & {{item.value}}"
          with_dict: ansible_eth0.ipv4
原文地址:https://www.cnblogs.com/jinyuanliu/p/10710641.html