How to loop over this dictionary in Ansible?

前端 未结 5 519
说谎
说谎 2020-12-02 22:59

Say I have this dictionary

war_files:
  server1:
  - file1.war
  - file2.war
  server2:
  - file1.war
  - file2.war
  - file3.war

and for n

5条回答
  •  有刺的猬
    2020-12-02 23:39

    dict2items

    I found myself wanting to iterate over a heterogeneous set of keys and their associated values and use the key-value pair in a task. The dict2items filter is the least painful way I've found. You can find dict2items in Ansible 2.6

    Example Dict

    systemsetup:
      remotelogin: "On"
      timezone: "Europe/Oslo"
      usingnetworktime: "On"
      sleep: 0
      computersleep: 0
      displaysleep: 0
      harddisksleep: 0
      allowpowerbuttontosleepcomputer: "Off"
      wakeonnetworkaccess: "On"
      restartfreeze: "On"
      restartpowerfailure: "On"
    

    Example Task

    ---
    - debug:
        msg: "KEY: {{ item.key }}, VALUE: {{ item.value }}"
      loop: "{{ systemsetup | dict2items }}"
    

提交回复
热议问题