Last active
September 27, 2017 20:23
-
-
Save shalomb/997f4bb68201803ab5c7f4a67ab8a3b2 to your computer and use it in GitHub Desktop.
Discover IPv4 management address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# requires python-netaddr on the control vm | |
# pip install netaddr | |
- name: Discover management ipv4 address | |
hosts: localhost | |
vars: | |
mgmt_network_cidr: '172.19.0.0/16' # Change this to reflect our mgmt. subnet cidr | |
tasks: | |
- name: Collect subnet addresses | |
set_fact: | |
ipv4_subnets: | |
"{{ (ipv4_subnets | default([])) + [{ | |
'subnet': | |
( hostvars[inventory_hostname]['ansible_' ~ item]['ipv4']['address'] | |
~ '/' ~ | |
hostvars[inventory_hostname]['ansible_' ~ item]['ipv4']['netmask'] | |
) | ipaddr('subnet'), | |
'ipv4': | |
hostvars[inventory_hostname]['ansible_' ~ item]['ipv4']['address'] | |
}] | |
}}" | |
when: | |
"{{ hostvars[inventory_hostname]['ansible_' ~ item]['ipv4'] is defined }}" | |
with_items: | |
- "{{ ansible_interfaces | map('replace', '-','_') | list }}" | |
tags: | |
- debug | |
- name: Extract management ipv4 address | |
set_fact: | |
mgmt_ipv4_address: | |
'{{ mgmt_ipv4 | selectattr("subnet", "equalto", mgmt_network_cidr) | map(attribute="ipv4") | first }}' | |
- name: Echo mgmt_ipv4_address | |
debug: | |
msg: 'mgmt_ipv4_address is {{ mgmt_ipv4_address }}' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment