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

 

相关文章:

  • 2021-04-25
  • 2022-12-23
  • 2021-07-08
  • 2022-03-08
  • 2021-09-13
  • 2021-09-01
猜你喜欢
  • 2021-09-02
  • 2021-07-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案