|
| 1 | +- hosts: localhost |
| 2 | + #become: true |
| 3 | + gather_facts: false |
| 4 | + vars: |
| 5 | + os_metadata: "{{ lookup('url', 'http://169.254.169.254/openstack/latest/meta_data.json') | from_json }}" |
| 6 | + #gateway_ip: "{{ os_metadata.meta.gateway_ip | default('') }}" |
| 7 | + access_ip: "{{ os_metadata.meta.access_ip | default('') }}" |
| 8 | + gateway_ip: 172.16.0.1 # DEBUG - actual |
| 9 | + # gateway_ip: 192.168.9.1 |
| 10 | + #gateway_ip: 10.20.0.0 |
| 11 | + gateway_ip: '' |
| 12 | + tasks: |
| 13 | + - name: Read nmcli device info |
| 14 | + command: nmcli --get GENERAL.DEVICE,GENERAL.CONNECTION,IP4.ADDRESS,IP4.GATEWAY device show |
| 15 | + register: _nmcli_device_raw |
| 16 | + changed_when: false |
| 17 | + |
| 18 | + - name: Set fact for nmcli devices |
| 19 | + set_fact: |
| 20 | + # creates a dict with keys as per zip arg below, values might be '' |
| 21 | + nmcli_devices: >- |
| 22 | + {{ |
| 23 | + _nmcli_device_raw.stdout_lines | |
| 24 | + batch(5, '') | |
| 25 | + map('zip', ['device', 'connection', 'ip4_address', 'ip4_gateway']) | |
| 26 | + map('map', 'reverse') | map('community.general.dict') |
| 27 | + }} |
| 28 | + # batch=5 because per device have 4x lines + blank line between devices |
| 29 | + # batch takes default '' because last devices doesn't have trailing blank line |
| 30 | + |
| 31 | + - name: Examine whether device address contains gateway_ip |
| 32 | + set_fact: |
| 33 | + device_is_gateway_device: "{{ nmcli_devices | map(attribute='ip4_address') | map('ansible.utils.network_in_network', gateway_ip) }}" |
| 34 | + |
| 35 | + - name: Get name of connection containing gateway_ip |
| 36 | + # might be empty string |
| 37 | + set_fact: |
| 38 | + gateway_ip_connection: >- |
| 39 | + {{ nmcli_devices | map(attribute='connection') | |
| 40 | + zip(device_is_gateway_device) | selectattr('1') | |
| 41 | + map(attribute=0) | list | first | default ('') }} |
| 42 | + |
| 43 | + - name: Error if device has a gateway which is not the desired one |
| 44 | + # TODO: document |
| 45 | + assert: |
| 46 | + that: item.gateway == gateway_ip |
| 47 | + fail_msg: "Device {{ item | to_nice_json }} has gateway: cannot apply gateway {{ gateway_ip }}" |
| 48 | + when: |
| 49 | + - item.connection == gateway_ip_connection |
| 50 | + - item.ip4_gateway != '' |
| 51 | + - item.ip4_gateway != gateway_ip |
| 52 | + loop: "{{ nmcli_devices }}" |
| 53 | + |
| 54 | + - name: Remove undesired gateways |
| 55 | + command: >- |
| 56 | + echo nmcli connection modify '{{ item.connection }}' ipv4.gateway '' |
| 57 | + && |
| 58 | + echo nmcli connection up '{{ item.connection }}' |
| 59 | + when: |
| 60 | + - gateway_ip != '' |
| 61 | + - item.ip4_gateway != '' |
| 62 | + - item.connection != gateway_ip_connection |
| 63 | + loop: "{{ nmcli_devices }}" |
| 64 | + |
| 65 | + - name: Add desired gateways |
| 66 | + command: >- |
| 67 | + echo nmcli connection modify '{{ item.connection }}' |
| 68 | + ipv4.address {{ item.ip4_address }} |
| 69 | + ipv4.gateway {{ gateway_ip }} |
| 70 | + && |
| 71 | + echo nmcli connection up '{{ item.connection }}' |
| 72 | + when: |
| 73 | + - gateway_ip != '' |
| 74 | + - item.ip4_gateway != gateway_ip |
| 75 | + - item.connection == gateway_ip_connection |
| 76 | + loop: "{{ nmcli_devices }}" |
| 77 | + |
| 78 | + - name: Create dummy connection and gateway |
| 79 | + # see https://docs.k3s.io/installation/airgap#default-network-route |
| 80 | + command: >- |
| 81 | + nmcli connection add type dummy ifname dummy0 con-name dummy0 |
| 82 | + && |
| 83 | + nmcli connection modify dummy0 |
| 84 | + ipv4.address {{ access_ip }} |
| 85 | + ipv4.gateway {{ access_ip }} |
| 86 | + ipv4.route-metric 1000 |
| 87 | + ipv4.method manual |
| 88 | + && |
| 89 | + nmcli connection up dummy0 |
| 90 | + when: |
| 91 | + - gateway_ip == '' # no gateway specified |
| 92 | + - nmcli_devices | selectattr('ip4_gateway', 'ne', '') | length == 0 |
| 93 | + # no gateway from networks |
0 commit comments