fix: apply network changes on systems using nm
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Robert Kaussow 2022-10-10 15:17:16 +02:00
parent 82034b0a34
commit 9dea3340ae
Signed by: xoxys
GPG Key ID: 4E692A2EAECC03C0
6 changed files with 84 additions and 61 deletions

View File

@ -1,10 +1,9 @@
--- ---
- block: - block:
- name: Restart network - name: Restart network
shell: sleep 2 && systemctl restart network shell: "{{ __network_restart_command }}"
async: 1 async: 1
poll: 0 poll: 0
ignore_errors: yes
when: not require_reboot | bool when: not require_reboot | bool
listen: __network_restart listen: __network_restart

View File

@ -1,57 +1,15 @@
--- ---
- name: Set flag require_reboot - include_vars: "{{ var_files }}"
set_fact: vars:
require_reboot: "{{ True if (network_fqdn != ansible_fqdn and network_reboot_enabled | bool) else False }}" var_files: "{{ lookup('first_found', params, errors='ignore') }}"
params:
files:
- "{{ ansible_lsb.id | default('') | lower }}.yml"
- "{{ ansible_os_family | lower }}-{{ ansible_distribution_major_version }}.yml"
- "{{ ansible_os_family | lower }}.yml"
- default.yml
paths:
- "vars"
when: var_files
- block: - include_tasks: setup.yml
- name: Set hostname
hostname:
name: "{{ network_hostname }}"
when: network_hostname
notify: __host_reboot
- name: Write hosts file
template:
src: etc/hosts.j2
dest: /etc/hosts
owner: root
group: root
mode: 0644
notify: __host_reboot
- name: Add network interface configuration
template:
src: etc/sysconfig/network-scrips/ifcfg.j2
dest: "/etc/sysconfig/network-scripts/ifcfg-{{ item.key }}"
owner: root
group: root
mode: 0644
loop: "{{ network_interfaces | dict2items }}"
loop_control:
label: "{{ item.key }}"
when: item.key in ansible_interfaces or item.key.split(":")[0] in ansible_interfaces
notify: __network_restart
- name: Add static routes
copy:
content: "{{ item.value }}"
dest: "/etc/sysconfig/network-scripts/route-{{ item.key }}"
owner: root
group: root
mode: 0644
loop: "{{ network_routes | dict2items }}"
loop_control:
label: "{{ item.key }}"
when: item.key in ansible_interfaces
notify: __network_restart
- name: Set DNS servers
template:
src: etc/resolv.conf.j2
dest: /etc/resolv.conf
owner: root
group: root
mode: 0644
when: network_resolve_enabled | bool
become: True
become_user: root

58
tasks/setup.yml Normal file
View File

@ -0,0 +1,58 @@
---
- name: Set flag require_reboot
set_fact:
require_reboot: "{{ True if (network_fqdn != ansible_fqdn and network_reboot_enabled | bool) else False }}"
- block:
- name: Set hostname
hostname:
name: "{{ network_hostname }}"
when: network_hostname
notify: __host_reboot
- name: Write hosts file
template:
src: etc/hosts.j2
dest: /etc/hosts
owner: root
group: root
mode: 0644
notify: __host_reboot
- name: Add network interface configuration
template:
src: etc/sysconfig/network-scrips/ifcfg.j2
dest: "/etc/sysconfig/network-scripts/ifcfg-{{ item.key }}"
owner: root
group: root
mode: 0644
loop: "{{ network_interfaces | dict2items }}"
loop_control:
label: "{{ item.key }}"
when: item.key in ansible_interfaces or item.key.split(":")[0] in ansible_interfaces
register: __network_ifcfg
notify: __network_restart
- name: Add static routes
copy:
content: "{{ item.value }}"
dest: "/etc/sysconfig/network-scripts/route-{{ item.key }}"
owner: root
group: root
mode: 0644
loop: "{{ network_routes | dict2items }}"
loop_control:
label: "{{ item.key }}"
when: item.key in ansible_interfaces
notify: __network_restart
- name: Set DNS servers
template:
src: etc/resolv.conf.j2
dest: /etc/resolv.conf
owner: root
group: root
mode: 0644
when: network_resolve_enabled | bool
become: True
become_user: root

View File

@ -1,11 +1,11 @@
#jinja2: lstrip_blocks: True #jinja2: lstrip_blocks: True
{{ ansible_managed | comment }} {{ ansible_managed | comment }}
127.0.0.1 localhost localhost.localdomain 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
{% if network_ipv6_enabled %} {% if network_ipv6_enabled %}
::1 localhost6 localhost6.localdomain6 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
{% endif %} {% endif %}
{% if network_hostname is defined and network_fqdn is defined and network_interfaces[network_defaultif].ipaddr is defined %} {% if network_hostname is defined and network_fqdn is defined and network_defaultif is defined %}
{{ network_interfaces[network_defaultif].ipaddr }} {{ network_fqdn }} {{ network_hostname }} {{ network_interfaces[network_defaultif].ipaddr | default(hostvars[inventory_hostname]["ansible_" + network_defaultif].ipv4.address) }} {{ network_fqdn }} {{ network_hostname }}
{% endif %} {% endif %}
{% if network_hosts_extra %} {% if network_hosts_extra %}
{% for item in network_hosts_extra %} {% for item in network_hosts_extra %}

2
vars/default.yml Normal file
View File

@ -0,0 +1,2 @@
---
__network_restart_command: sleep 2 && systemctl restart network

6
vars/redhat-9.yml Normal file
View File

@ -0,0 +1,6 @@
---
__network_restart_command: |-
nmcli connection reload &&\
{%- for file in __network_ifcfg | json_query('results[?changed==`true`].dest') %}
nmcli conn up {{ file }}
{%- endfor %}