How to convert a dictionary of dictionaries into a list of dictionaries in a Ansible vars file?

泄露秘密 提交于 2019-12-19 05:45:10

问题


Within an Ansible vars file, I want to convert a dict of dicts into a list of dicts that I can pass to an external role from Ansible Galaxy.

Input:

postgres_users:
  dc1:
    name: user_dc1
    password: pass_dc1
  dc2:
    name: user_dc2
    password: pass_dc2
  dc3:
    name: user_dc3
    password: pass_dc3

Desired output:

postgres_users:
  - name: user_dc1
    password: pass_dc1
  - name: user_dc2
    password: pass_dc2
  - name: user_dc3
    password: pass_dc3

Is there a simple way to do this within an Ansible vars file?


回答1:


{{ postgres_users.values() | list }} seems to do it.



来源:https://stackoverflow.com/questions/37287013/how-to-convert-a-dictionary-of-dictionaries-into-a-list-of-dictionaries-in-a-ans

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!