From 63ad4cffacd48f430ca1a9b5bd5fc03ed9f4581e Mon Sep 17 00:00:00 2001 From: Jonathan Rosser Date: Mon, 22 Feb 2021 16:26:30 +0000 Subject: [PATCH] Use ansible_facts[] instead of fact variables See https://github.com/ansible/ansible/issues/73654 Change-Id: I36a098e4b028bdf09b38e2f2c99f68f311ce9d32 --- handlers/main.yml | 14 ++++++------ tasks/main.yml | 34 ++++++++++++++--------------- tests/test.yml | 54 +++++++++++++++++++++++------------------------ 3 files changed, 51 insertions(+), 51 deletions(-) diff --git a/handlers/main.yml b/handlers/main.yml index 1f2068a..a606825 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -45,10 +45,10 @@ - name: Update initramfs command: "{{ systemd_networkd_update_initramfs }}" when: - - ansible_virtualization_type != "systemd-nspawn" - - ansible_virtualization_type != "lxc" - - ansible_virtualization_type != "lxd" - - ansible_virtualization_type != "podman" - - ansible_virtualization_type != "docker" - - ansible_virtualization_type != "systemd_container" - - ansible_virtualization_type != "container" + - ansible_facts['virtualization_type'] != "systemd-nspawn" + - ansible_facts['virtualization_type'] != "lxc" + - ansible_facts['virtualization_type'] != "lxd" + - ansible_facts['virtualization_type'] != "podman" + - ansible_facts['virtualization_type'] != "docker" + - ansible_facts['virtualization_type'] != "systemd_container" + - ansible_facts['virtualization_type'] != "container" diff --git a/tasks/main.yml b/tasks/main.yml index 82a1bbf..10bf03a 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -17,11 +17,11 @@ include_vars: "{{ item }}" with_first_found: - files: - - "{{ ansible_distribution | lower }}-{{ ansible_distribution_version | lower }}.yml" - - "{{ ansible_distribution | lower }}-{{ ansible_distribution_major_version | lower }}.yml" - - "{{ ansible_os_family | lower }}-{{ ansible_distribution_major_version | lower }}.yml" - - "{{ ansible_distribution | lower }}.yml" - - "{{ ansible_os_family | lower }}.yml" + - "{{ ansible_facts['distribution'] | lower }}-{{ ansible_facts['distribution_version'] | lower }}.yml" + - "{{ ansible_facts['distribution'] | lower }}-{{ ansible_facts['distribution_major_version'] | lower }}.yml" + - "{{ ansible_facts['os_family'] | lower }}-{{ ansible_facts['distribution_major_version'] | lower }}.yml" + - "{{ ansible_facts['distribution'] | lower }}.yml" + - "{{ ansible_facts['os_family'] | lower }}.yml" skip: true tags: - always @@ -35,16 +35,16 @@ mode: '0644' with_items: "{{ systemd_networkd_package_repos_keys | selectattr('keyfile','defined') | list }}" when: - - ansible_os_family | lower == 'redhat' - - ansible_distribution_major_version is version('8', '>=') + - ansible_facts['os_family'] | lower == 'redhat' + - ansible_facts['distribution_major_version'] is version('8', '>=') - name: Ensure GPG keys have the correct SELinux contexts applied command: restorecon -Rv /etc/pki/rpm-gpg/ # TODO(evrardjp): Be more idempotent changed_when: false when: - - ansible_os_family | lower == 'redhat' - - ansible_distribution_major_version is version('8', '>=') + - ansible_facts['os_family'] | lower == 'redhat' + - ansible_facts['distribution_major_version'] is version('8', '>=') # Handle gpg keys manually - name: Install gpg keys @@ -60,21 +60,21 @@ retries: 5 delay: 2 when: - - ansible_os_family | lower == 'redhat' - - ansible_distribution_major_version is version('8', '>=') + - ansible_facts['os_family'] | lower == 'redhat' + - ansible_facts['distribution_major_version'] is version('8', '>=') - name: Install the EPEL repository yum_repository: name: epel-networkd - baseurl: "{{ systemd_networkd_epel_mirror ~ '/' ~ ansible_distribution_major_version ~ '/Everything/' ~ ansible_architecture }}" + baseurl: "{{ systemd_networkd_epel_mirror ~ '/' ~ ansible_facts['distribution_major_version'] ~ '/Everything/' ~ ansible_facts['architecture'] }}" description: 'Extra Packages for Enterprise Linux $releasever - $basearch' gpgcheck: yes enabled: yes state: present includepkgs: 'systemd-networkd' when: - - ansible_os_family | lower == 'redhat' - - ansible_distribution_major_version is version('8', '>=') + - ansible_facts['os_family'] | lower == 'redhat' + - ansible_facts['distribution_major_version'] is version('8', '>=') register: install_epel_repo until: install_epel_repo is success retries: 5 @@ -84,8 +84,8 @@ package: name: "{{ systemd_networkd_distro_packages }}" state: "present" - update_cache: "{{ (ansible_pkg_mgr == 'apt') | ternary('yes', omit) }}" - cache_valid_time: "{{ (ansible_pkg_mgr == 'apt') | ternary(600, omit) }}" + update_cache: "{{ (ansible_facts['pkg_mgr'] == 'apt') | ternary('yes', omit) }}" + cache_valid_time: "{{ (ansible_facts['pkg_mgr'] == 'apt') | ternary(600, omit) }}" enablerepo: "{{ systemd_networkd_enablerepo | default(omit) }}" when: - systemd_networkd_distro_packages | length > 0 @@ -205,5 +205,5 @@ reload: yes with_indexed_items: "{{ systemd_networks }}" when: - - (ansible_os_family | lower) == 'redhat' + - (ansible_facts['os_family'] | lower) == 'redhat' - item.1.ipforward | default(false) | bool diff --git a/tests/test.yml b/tests/test.yml index 6fa68cc..1e85f56 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -70,7 +70,7 @@ - interface: "br-dummy" address: "10.0.0.100" netmask: "255.255.255.0" - gateway: "{{ ansible_default_ipv4.gateway | default('10.0.0.1') }}" + gateway: "{{ ansible_facts['default_ipv4']['gateway'] | default('10.0.0.1') }}" mtu: 9000 usedns: true static_routes: @@ -101,34 +101,34 @@ - name: Interface check assert: that: - - ansible_dummy0['active'] - - ansible_dummy0['type'] == 'ether' - - ansible_dummy0['mtu'] == 9000 - - ansible_dummy1['active'] - - ansible_dummy1['type'] == 'ether' - - ansible_dummy1['mtu'] == 9000 - - ansible_dummy2['active'] - - ansible_dummy2['type'] == 'ether' + - ansible_facts['dummy0']['active'] + - ansible_facts['dummy0']['type'] == 'ether' + - ansible_facts['dummy0']['mtu'] == 9000 + - ansible_facts['dummy1']['active'] + - ansible_facts['dummy1']['type'] == 'ether' + - ansible_facts['dummy1']['mtu'] == 9000 + - ansible_facts['dummy2']['active'] + - ansible_facts['dummy2']['type'] == 'ether' - name: Bond check assert: that: - - ansible_bond0['active'] - - ansible_bond0['type'] == 'bonding' - - ansible_bond0['mtu'] == 9000 + - ansible_facts['bond0']['active'] + - ansible_facts['bond0']['type'] == 'bonding' + - ansible_facts['bond0']['mtu'] == 9000 - name: Bridge check assert: that: - - ansible_br_dummy['active'] - - ansible_br_dummy['type'] == 'bridge' - - ansible_br_dummy['ipv4']['address'] == '10.0.0.100' - - ansible_br_dummy['ipv4']['netmask'] == '255.255.255.0' + - ansible_facts['br_dummy']['active'] + - ansible_facts['br_dummy']['type'] == 'bridge' + - ansible_facts['br_dummy']['ipv4']['address'] == '10.0.0.100' + - ansible_facts['br_dummy']['ipv4']['netmask'] == '255.255.255.0' - name: Bridge check assert: that: - - ansible_br_test['active'] - - ansible_br_test['type'] == 'bridge' - - ansible_br_test['ipv4']['address'] == '10.1.0.1' - - ansible_br_test['ipv4']['netmask'] == '255.255.255.0' + - ansible_facts['br_test']['active'] + - ansible_facts['br_test']['type'] == 'bridge' + - ansible_facts['br_test']['ipv4']['address'] == '10.1.0.1' + - ansible_facts['br_test']['ipv4']['netmask'] == '255.255.255.0' - name: Check link config overrides shell: 'grep -wo "Alias" /etc/systemd/network/*br-dummy.link' changed_when: false @@ -158,16 +158,16 @@ - name: Interface check assert: that: - - ansible_br_test is defined - - ansible_dummy2['active'] - - ansible_dummy2['type'] == 'ether' + - ansible_facts['br_test'] is defined + - ansible_facts['dummy2']['active'] + - ansible_facts['dummy2']['type'] == 'ether' - name: Bridge check assert: that: - - ansible_br_test['active'] - - ansible_br_test['type'] == 'bridge' - - ansible_br_test['ipv4']['address'] == '10.1.0.1' - - ansible_br_test['ipv4']['netmask'] == '255.255.255.0' + - ansible_facts['br_test']['active'] + - ansible_facts['br_test']['type'] == 'bridge' + - ansible_facts['br_test']['ipv4']['address'] == '10.1.0.1' + - ansible_facts['br_test']['ipv4']['netmask'] == '255.255.255.0' vars: systemd_interface_cleanup: true systemd_run_networkd: yes