I'm trying to setup my sshd_config file using Ansible. For design reasons (we have both virtual and physical machines, with different NICs) the management interface is not fixed, so in my template I cannot just put
{{ ansible_eth0.ipv4.address }}
because I don't know in advance which interface is going to be management interface so, I need discovering it. I know the IP is always going to be inside a range (e.g. 192.168.1.0/24).
How can I discover which interface has that IP and then use it in the template?
Ansible provides a fact named ansible_all_ipv4_addresses
that is a list of all ip addresses.
To find the management IP see this working example:
test.yml:
- hosts: all
gather_facts: yes
tasks:
- debug: var=ansible_all_ipv4_addresses
- set_fact:
man_ip: "{{ item }}"
with_items: "{{ ansible_all_ipv4_addresses }}"
when: "item.startswith('192.168.1')"
- debug: var=man_ip
Run the test play locally:
ansible-playbook -c local -i localhost, test.yml
The variable {{ man_ip }}
will have the ip address of the management ip address.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With