I'm trying to set /etc/hosts entries in a playbook.
I'm managing three hashes of lists: common_stubs, env_stubs, and host_stubs.
example of what I'm working with here
common_stubs:
"127.0.0.1":
- localhost
env_stubs:
"127.0.0.1":
- someservice.mydomain
host_stubs: {}
I want to combine these so I get
combined_stubs:
"127.0.0.1":
- localhost
- someservice.mydomain
This is what I'm currently doing
- name: "Configure /etc/hosts"
lineinfile:
path: /etc/hosts
regex: "{{ '^' + item.key + '.*$' }}"
line: "{{ item.key + ' ' + ' '.join(item.value) }}"
state: "{% if item.value | length > 0 %}present{% else %}absent{% endif %}"
loop: "{{ common_stubs | combine(env_stubs, host_stubs, recursive=True) | dict2items }}"
become: true
but the array in env_stubs
is overriding the array in common_stubs
Best Answer
Let's create the list of IPs first
then loop the list and combine the dictionary
give
Next option is to create custom filter
Then the tasks below
give