My inventory looks like this:
db0 ansible_host=10.0.0.1
db1 ansible_host=10.0.0.2
app0 ansible_host=10.0.0.3
app1 ansible_host=10.0.0.4
...
From this, I need to extract a list like this:
- name: db0
ip: 10.0.0.1
- name: db1
ip: 10.0.0.2
- name: app0
ip: 10.0.0.3
- name: app1
ip: 10.0.0.4
I know I can get all hosts using groups['all']
.
I can also get the ansible_host
value for each host using hostvars['<hostname>']['ansible_host']
.
How do I combine this to create the list I need?
Best Answer
For example
creates the dictionary
Then, use dict2items to create the list
gives the expected structure