i have a simple role, which just gets the ip/hostnames of all hosts in the play, and updates the /etc/hosts file.
- name: Add the inventory into /etc/hosts
lineinfile:
dest: /etc/hosts
regexp: '.*{{ item }}$'
line: "{{ hostvars[item]['ansible_default_ipv4']['address'] }} {{item}}"
state: present
when: hostvars[item]['ansible_facts']['default_ipv4'] is defined
with_items:
- "{{ groups['all'] }}"
become: true
I want to also have a test for this, for molecule. I am somewhat familiar with molecule/testinfra, and know how to make tests loop through, and how to check the content of files etc. I do not know how to get the ip/hostnames from this test, and then use those to verify they exist in the /etc/hosts file.
v = host.ansible.get_variables()
I imagine that would contain the information I need, but I see no way to print that information out from molecule/testinfra so i can actually see it. If I was able to actually see the ip's, i know i could use them like in the below:
def test_conf_custom(host):
conf = host.file(
'/etc/hosts'
).content
expected = [
b'hostname',
b'443',
]
for line in expected:
assert line in conf