feat: add option to manage networkmanager config files
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Robert Kaussow 2022-11-11 21:51:36 +01:00
parent f3f3b46f1e
commit 233cf83f87
Signed by: xoxys
GPG Key ID: 4E692A2EAECC03C0
7 changed files with 55 additions and 11 deletions

View File

@ -39,3 +39,10 @@ network_hosts_extra: []
# fqdn: myhost.rknet.org
# address: 1.2.3.4
# @end
network_nm_config: {}
# @var network_nm_config:example: >
# network_nm_config:
# unmanaged: |
# [keyfile]
# unmanaged-devices=interface-name:ens*

View File

@ -1,12 +1,17 @@
---
- name: Set flag require_reboot
set_fact:
__network_require_reboot: True
listen: __network_host_reboot
- block:
- name: Restart network
shell: "{{ __network_restart_command }}"
async: 1
poll: 0
when:
- (not __network_require_reboot | bool) or (not network_reboot_enabled | bool)
- network_restart_enabled | bool
- not require_reboot | bool
listen: __network_restart
tags:
- skip_ansible_later
@ -17,8 +22,8 @@
async: 1
poll: 0
ignore_errors: yes
when: require_reboot | bool
listen: __host_reboot
when: network_reboot_enabled | bool
listen: __network_host_reboot
become: True
become_user: root
@ -26,7 +31,7 @@
wait_for_connection:
delay: 5
timeout: 300
when: (require_reboot | bool) or (network_restart_enabled | bool)
when: (network_reboot_enabled | bool) or (network_restart_enabled | bool)
listen:
- __network_restart
- __host_reboot
- __network_host_reboot

View File

@ -12,4 +12,6 @@
- "vars"
when: var_files
- include_tasks: nm.yml
when: __network_nmcli | bool
- include_tasks: setup.yml

32
tasks/nm.yml Normal file
View File

@ -0,0 +1,32 @@
---
- block:
- name: Add NetworkManager configuration
copy:
content: "{{ item.value }}"
dest: "/etc/NetworkManager/conf.d/{{ item.key }}.conf"
owner: root
group: root
mode: 0640
loop: "{{ network_nm_config | dict2items }}"
loop_control:
label: "{{ item.key }}"
notify: __network_host_reboot
- name: Register active NetworkManager configuration
find:
paths: /etc/NetworkManager/conf.d/
file_type: file
patterns: "*.conf"
register: __network_nm_config_active
changed_when: False
failed_when: False
- name: Remove unmanaged NetworkManager configuration
file:
path: "{{ item }}"
state: absent
loop: "{{ __network_nm_config_active.files | map(attribute='path') | list }}"
notify: __network_host_reboot
when: (item | basename | splitext | first) not in (network_nm_config | dict2items | map(attribute='key') | list)
become: True
become_user: root

View File

@ -1,14 +1,10 @@
---
- 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
notify: __network_host_reboot
- name: Write hosts file
template:
@ -17,7 +13,7 @@
owner: root
group: root
mode: 0644
notify: __host_reboot
notify: __network_host_reboot
- name: Add network interface configuration
template:

View File

@ -1,2 +1,3 @@
---
__network_nmcli: False
__network_restart_command: systemctl restart network

View File

@ -1,4 +1,5 @@
---
__network_nmcli: True
__network_restart_command: |-
nmcli connection reload
{%- for file in __network_ifcfg | json_query('results[?changed==`true`].dest') %}