Add role gather_ip_interfaces
This commit is contained in:
parent
7a6c896187
commit
8ebb071097
3 changed files with 219 additions and 3 deletions
166
README.md
166
README.md
|
@ -1,3 +1,165 @@
|
||||||
# ansible_Jonny007MKD_interfaces
|
# Ansible Collection - Jonny007MKD.interfaces
|
||||||
|
|
||||||
The ansible collection Jonny007MKD.interfaces
|
## Role gather_ip_interfaces
|
||||||
|
|
||||||
|
Ansible facts contain information about the IP configuration in a non-optimal way (IMO).
|
||||||
|
This role restructures the facts and sets new variables:
|
||||||
|
|
||||||
|
### interfaces
|
||||||
|
|
||||||
|
A list of dicts containing the configuration of each network interfaces.
|
||||||
|
The specific content comes from the ansible variables; this is only an aggregate of all interface dictionaries.
|
||||||
|
|
||||||
|
ok: [localhost] => {
|
||||||
|
"interfaces": [
|
||||||
|
{
|
||||||
|
"active": true,
|
||||||
|
"device": "lo",
|
||||||
|
"features": {
|
||||||
|
"esp_hw_offload": "off [fixed]",
|
||||||
|
"esp_tx_csum_hw_offload": "off [fixed]",
|
||||||
|
"fcoe_mtu": "off [fixed]",
|
||||||
|
"generic_receive_offload": "on",
|
||||||
|
"generic_segmentation_offload": "on",
|
||||||
|
...
|
||||||
|
},
|
||||||
|
"hw_timestamp_filters": [],
|
||||||
|
"ipv4": {
|
||||||
|
"address": "127.0.0.1",
|
||||||
|
"broadcast": "",
|
||||||
|
"netmask": "255.0.0.0",
|
||||||
|
"network": "127.0.0.0",
|
||||||
|
"prefix": "8"
|
||||||
|
},
|
||||||
|
"ipv6": [
|
||||||
|
{
|
||||||
|
"address": "::1",
|
||||||
|
"prefix": "128",
|
||||||
|
"scope": "host"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"mtu": 65536,
|
||||||
|
"promisc": false,
|
||||||
|
"timestamping": [],
|
||||||
|
"type": "loopback"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"active": true,
|
||||||
|
"device": "wlo1",
|
||||||
|
"features": {
|
||||||
|
"esp_hw_offload": "off [fixed]",
|
||||||
|
"esp_tx_csum_hw_offload": "off [fixed]",
|
||||||
|
"fcoe_mtu": "off [fixed]",
|
||||||
|
"generic_receive_offload": "on",
|
||||||
|
"generic_segmentation_offload": "off [requested on]",
|
||||||
|
...
|
||||||
|
},
|
||||||
|
"hw_timestamp_filters": [],
|
||||||
|
"ipv4": {
|
||||||
|
"address": "192.168.1.100",
|
||||||
|
"broadcast": "192.168.1.255",
|
||||||
|
"netmask": "255.255.255.0",
|
||||||
|
"network": "192.168.1.0",
|
||||||
|
"prefix": "9"
|
||||||
|
},
|
||||||
|
"ipv6": [
|
||||||
|
{
|
||||||
|
"address": "fddc:ffeb:2910::206",
|
||||||
|
"prefix": "128",
|
||||||
|
"scope": "global"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"address": "fddc:ffeb:2910:0:18c7:c3ff:efa6:35ae",
|
||||||
|
"prefix": "64",
|
||||||
|
"scope": "global"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"macaddress": "18:c7:c3:a6:35:ae",
|
||||||
|
"module": "rtw88_8821ce",
|
||||||
|
"mtu": 1500,
|
||||||
|
"pciid": "0000:01:00.0",
|
||||||
|
"promisc": false,
|
||||||
|
"timestamping": [],
|
||||||
|
"type": "ether"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
## all_ipv4_addresses
|
||||||
|
|
||||||
|
A list of dicts with the information about this machine's IPv4 networks
|
||||||
|
|
||||||
|
ok: [localhost] => {
|
||||||
|
"all_ipv4_addresses": [
|
||||||
|
{
|
||||||
|
"interface": "lo",
|
||||||
|
"address": "127.0.0.1",
|
||||||
|
"address_prefix": "127.0.0.1/8",
|
||||||
|
"broadcast": "",
|
||||||
|
"netmask": "255.0.0.0",
|
||||||
|
"network": "127.0.0.0",
|
||||||
|
"network_prefix": "127.0.0.0/8",
|
||||||
|
"prefix": "8"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"interface": "wlo1",
|
||||||
|
"address": "192.168.1.100",
|
||||||
|
"address_prefix": "192.168.1.100/24",
|
||||||
|
"broadcast": "192.168.1.255",
|
||||||
|
"netmask": "255.255.255.0",
|
||||||
|
"network": "192.168.1.0",
|
||||||
|
"network_prefix": "192.168.1.0/24",
|
||||||
|
"prefix": "24"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### all_ipv6_addresses
|
||||||
|
|
||||||
|
A list of dicts with the information about this machine's IPv6 networks
|
||||||
|
|
||||||
|
ok: [localhost] => {
|
||||||
|
"all_ipv6_addresses": [
|
||||||
|
{
|
||||||
|
"address": "::1",
|
||||||
|
"address_prefix": "::1/128",
|
||||||
|
"interface": "lo",
|
||||||
|
"prefix": "128",
|
||||||
|
"scope": "host"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"address": "fddc:ffeb:2910::206",
|
||||||
|
"address_prefix": "fddc:ffeb:2910::206/128",
|
||||||
|
"interface": "wlo1",
|
||||||
|
"prefix": "128",
|
||||||
|
"scope": "global"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"address": "fddc:ffeb:2910:0:18c7:c3ff:efa6:35ae",
|
||||||
|
"address_prefix": "fddc:ffeb:2910:0:18c7:c3ff:efa6:35ae/64",
|
||||||
|
"interface": "wlo1",
|
||||||
|
"prefix": "64",
|
||||||
|
"scope": "global"
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
### Usage
|
||||||
|
|
||||||
|
Simply include the role in your playbook:
|
||||||
|
|
||||||
|
---
|
||||||
|
- name: My playbook
|
||||||
|
hosts: localhost
|
||||||
|
connection: local
|
||||||
|
roles:
|
||||||
|
- Jonny007MKD.interfaces.gather_ip_interfaces
|
||||||
|
|
||||||
|
Or include it in your role when you need it:
|
||||||
|
|
||||||
|
- name: Gather IP facts on servers
|
||||||
|
ansible.builtin.import_role:
|
||||||
|
name: Jonny007MKD.interfaces.gather_ip_interfaces
|
||||||
|
|
|
@ -11,7 +11,10 @@ description: Network interface related roles
|
||||||
license:
|
license:
|
||||||
- GPL-2.0-or-later
|
- GPL-2.0-or-later
|
||||||
|
|
||||||
tags: []
|
tags:
|
||||||
|
- interface
|
||||||
|
- ipv4
|
||||||
|
- ipv6
|
||||||
|
|
||||||
dependencies: {}
|
dependencies: {}
|
||||||
|
|
||||||
|
|
51
roles/gather_ip_interfaces/tasks/main.yml
Normal file
51
roles/gather_ip_interfaces/tasks/main.yml
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
- name: Gather network interfaces with `vars` name
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
_gather_ip_interfaces_with_vars_name: "{{ ansible_interfaces | map('regex_replace', '^', 'ansible_') | map('regex_replace', '[^a-zA-Z0-9]', '_') }}"
|
||||||
|
|
||||||
|
- name: Gather network interface information
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
interfaces: "{{ _gather_ip_interfaces_with_vars_name | map('extract', vars) }}"
|
||||||
|
|
||||||
|
- name: Gather all IPv4 addresses of interface
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
all_ipv4_addresses: "{{ (all_ipv4_addresses | default([])) + new_items }}"
|
||||||
|
loop: "{{ ansible_interfaces }}"
|
||||||
|
when: vars['ansible_' + item | regex_replace('[^a-zA-Z0-9]', '_')].ipv4 is defined
|
||||||
|
vars:
|
||||||
|
new_items: "{{
|
||||||
|
(
|
||||||
|
[vars['ansible_' + item | regex_replace('[^a-zA-Z0-9]', '_')].ipv4]
|
||||||
|
+ vars['ansible_' + item | regex_replace('[^a-zA-Z0-9]', '_')].ipv4_secondaries | default([])
|
||||||
|
) | map('combine', {'interface': item})
|
||||||
|
}}"
|
||||||
|
|
||||||
|
- name: Gather all IPv6 addresses of interface
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
all_ipv6_addresses: "{{ (all_ipv6_addresses | default([])) + new_items }}"
|
||||||
|
loop: "{{ ansible_interfaces }}"
|
||||||
|
when: vars['ansible_' + item | regex_replace('[^a-zA-Z0-9]', '_')].ipv6 is defined
|
||||||
|
vars:
|
||||||
|
new_items: "{{
|
||||||
|
vars['ansible_' + item | regex_replace('[^a-zA-Z0-9]', '_')].ipv6 | map('combine', {'interface': item})
|
||||||
|
}}"
|
||||||
|
|
||||||
|
- name: Compute additional info for IPv4
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
all_ipv4_addresses: "{{ all_ipv4_addresses | difference([item]) | list + [item | combine(additional_info)] }}"
|
||||||
|
loop: "{{ all_ipv4_addresses }}"
|
||||||
|
vars:
|
||||||
|
additional_info:
|
||||||
|
network_prefix: "{{ item.network }}/{{ item.prefix }}"
|
||||||
|
address_prefix: "{{ item.address }}/{{ item.prefix }}"
|
||||||
|
loop_control:
|
||||||
|
label: "{{ item.interface }}: {{ item.address }}"
|
||||||
|
|
||||||
|
- name: Compute additional info for IPv6
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
all_ipv6_addresses: "{{ all_ipv6_addresses | difference([item]) | list + [item | combine(additional_info)] }}"
|
||||||
|
loop: "{{ all_ipv6_addresses }}"
|
||||||
|
vars:
|
||||||
|
additional_info:
|
||||||
|
address_prefix: "{{ item.address }}/{{ item.prefix }}"
|
||||||
|
loop_control:
|
||||||
|
label: "{{ item.interface }}: {{ item.address }}"
|
Loading…
Reference in a new issue