From 233cf83f87d75e5d5c2acc1fd7f27f49d96a89fb Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Fri, 11 Nov 2022 21:51:36 +0100 Subject: [PATCH] feat: add option to manage networkmanager config files --- defaults/main.yml | 7 +++++++ handlers/main.yml | 15 ++++++++++----- tasks/main.yml | 2 ++ tasks/nm.yml | 32 ++++++++++++++++++++++++++++++++ tasks/setup.yml | 8 ++------ vars/default.yml | 1 + vars/redhat-9.yml | 1 + 7 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 tasks/nm.yml diff --git a/defaults/main.yml b/defaults/main.yml index e5775e7..becf151 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -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* diff --git a/handlers/main.yml b/handlers/main.yml index 5857357..2fc6d7e 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -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 diff --git a/tasks/main.yml b/tasks/main.yml index cec409b..b1312fb 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -12,4 +12,6 @@ - "vars" when: var_files +- include_tasks: nm.yml + when: __network_nmcli | bool - include_tasks: setup.yml diff --git a/tasks/nm.yml b/tasks/nm.yml new file mode 100644 index 0000000..c893dd3 --- /dev/null +++ b/tasks/nm.yml @@ -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 diff --git a/tasks/setup.yml b/tasks/setup.yml index 4f5327c..1a35310 100644 --- a/tasks/setup.yml +++ b/tasks/setup.yml @@ -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: diff --git a/vars/default.yml b/vars/default.yml index bb84a2f..537b736 100644 --- a/vars/default.yml +++ b/vars/default.yml @@ -1,2 +1,3 @@ --- +__network_nmcli: False __network_restart_command: systemctl restart network diff --git a/vars/redhat-9.yml b/vars/redhat-9.yml index ef597dc..36719af 100644 --- a/vars/redhat-9.yml +++ b/vars/redhat-9.yml @@ -1,4 +1,5 @@ --- +__network_nmcli: True __network_restart_command: |- nmcli connection reload {%- for file in __network_ifcfg | json_query('results[?changed==`true`].dest') %}