Skip to content

Commit 3602767

Browse files
committed
add gateway ansible-init role
1 parent 1e3c8f9 commit 3602767

File tree

6 files changed

+119
-1
lines changed

6 files changed

+119
-1
lines changed

ansible/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,5 @@ roles/*
8686
!roles/pytools/**
8787
!roles/rebuild/
8888
!roles/rebuild/**
89+
!roles/gateway/
90+
!roles/gateway/**

ansible/fatimage.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
- import_playbook: extras.yml
8080

8181
# TODO: is this the right place?
82-
- name: Install compute_init script
82+
- name: Install compute_init playbook
8383
hosts: compute_init
8484
tags: compute_init # tagged to allow running on cluster instances for dev
8585
become: yes
@@ -88,6 +88,15 @@
8888
name: compute_init
8989
tasks_from: install.yml
9090

91+
- name: Install gateway playbook
92+
hosts: gateway
93+
tags: compute_init
94+
become: yes
95+
gather_facts: no
96+
tasks:
97+
- include_role:
98+
name: gateway
99+
91100
- hosts: builder
92101
become: yes
93102
gather_facts: yes
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
- name: Add gateway playbook
2+
copy:
3+
src: gateway-init.yml
4+
dest: /etc/ansible-init/playbooks/05-gateway-init.yml
5+
owner: root
6+
group: root
7+
mode: 0644

environments/common/inventory/groups

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,6 @@ extra_packages
169169

170170
[chrony]
171171
# Hosts where crony configuration is applied. See docs/chrony.md for more details.
172+
173+
[gateway]
174+
# Add builder to this group to install gateway ansible-init playbook into image

environments/common/layouts/everything

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,7 @@ builder
112112

113113
[chrony]
114114
# Hosts where crony configuration is applied. See docs/chrony.md for more details.
115+
116+
[gateway:children]
117+
# Add builder to this group to install gateway ansible-init playbook into image
118+
builder

0 commit comments

Comments
 (0)