Skip to content

Commit 39ac91d

Browse files
committed
refactor: handle INJECT_FACTS_AS_VARS=false by using ansible_facts instead
Ansible 2.20 has deprecated the use of Ansible facts as variables. For example, `ansible_distribution` is now deprecated in favor of `ansible_facts["distribution"]`. This is due to making the default setting `INJECT_FACTS_AS_VARS=false`. For now, this will create WARNING messages, but in Ansible 2.24 it will be an error. See https://docs.ansible.com/projects/ansible/latest/porting_guides/porting_guide_core_2.20.html#inject-facts-as-vars Signed-off-by: Rich Megginson <rmeggins@redhat.com>
1 parent bc67de4 commit 39ac91d

76 files changed

Lines changed: 199 additions & 201 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

defaults/main.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ __network_rh_distros:
1919
__network_rh_distros_fedora: "{{ __network_rh_distros + ['Fedora'] }}"
2020

2121
# Use this in conditionals to check if distro is Red Hat or clone
22-
__network_is_rh_distro: "{{ ansible_distribution in __network_rh_distros }}"
22+
__network_is_rh_distro: "{{ ansible_facts['distribution'] in __network_rh_distros }}"
2323

2424
# Use this in conditionals to check if distro is Red Hat or clone, or Fedora
25-
__network_is_rh_distro_fedora: "{{ ansible_distribution in __network_rh_distros_fedora }}"
25+
__network_is_rh_distro_fedora: "{{ ansible_facts['distribution'] in __network_rh_distros_fedora }}"
2626
# END - DO NOT EDIT THIS BLOCK - rh distros variables
2727

2828
# Use initscripts for RHEL/CentOS < 7, nm otherwise
2929
network_provider_os_default: "{{
30-
'initscripts' if ansible_distribution in __network_rh_distros and
31-
ansible_distribution_major_version is version('7', '<')
30+
'initscripts' if ansible_facts['distribution'] in __network_rh_distros and
31+
ansible_facts['distribution_major_version'] is version('7', '<')
3232
else 'nm' }}"
3333
# If NetworkManager.service is running, assume that 'nm' is currently in-use,
3434
# otherwise initscripts
@@ -76,7 +76,7 @@ __network_packages_default_wpa_supplicant: ["{%
7676
# - python-gobject-base on RHEL7 (no python2-gobject-base :-/)
7777
# - python3-gobject-base on Fedora 28+
7878
__network_packages_default_gobject_packages: ["python{{
79-
ansible_python['version']['major'] | replace('2', '') }}-gobject-base"]
79+
ansible_facts['python']['version']['major'] | replace('2', '') }}-gobject-base"]
8080

8181
__network_service_name_default_nm: NetworkManager
8282
__network_packages_default_nm: "{{ ['NetworkManager']
@@ -92,18 +92,18 @@ __network_service_name_default_initscripts: network
9292
__network_packages_default_initscripts_bridge: ["{%
9393
if network_connections | selectattr('type', 'defined') |
9494
selectattr('type', 'match', '^bridge$') | list | count > 0 and
95-
ansible_distribution in __network_rh_distros and
96-
ansible_distribution_major_version is version('7', '<=')
95+
ansible_facts['distribution'] in __network_rh_distros and
96+
ansible_facts['distribution_major_version'] is version('7', '<=')
9797
%}bridge-utils{% endif %}"]
9898
__network_packages_default_initscripts_network_scripts: ["{%
99-
if ansible_distribution in __network_rh_distros and
100-
ansible_distribution_major_version is version('7', '<=')
99+
if ansible_facts['distribution'] in __network_rh_distros and
100+
ansible_facts['distribution_major_version'] is version('7', '<=')
101101
%}initscripts{% else %}network-scripts{% endif %}"]
102102
# Initscripts provider requires `/sbin/dhclient` to obtain DHCP address,
103103
# which is provided by the dhcp client package
104104
__network_packages_default_initscripts_dhcp_client: ["{%
105-
if ansible_distribution in __network_rh_distros and
106-
ansible_distribution_major_version is version('7', '<=')
105+
if ansible_facts['distribution'] in __network_rh_distros and
106+
ansible_facts['distribution_major_version'] is version('7', '<=')
107107
%}dhclient{% else %}dhcp-client{% endif %}"]
108108
# convert _network_packages_default_initscripts_bridge to an empty list if it
109109
# contains only the empty string and add it to the default package list

tasks/main.yml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@
2323
supported since RHEL-8
2424
when:
2525
- network_state != {}
26-
- ansible_distribution_major_version | int < 8
26+
- ansible_facts["distribution_major_version"] | int < 8
2727

2828
- name: Abort applying teaming configuration if the system version
2929
of the managed host is EL10 or later
3030
fail:
3131
msg: >-
3232
Teaming is not supported in
33-
{{ ansible_distribution }}-{{ ansible_distribution_major_version }} -
33+
{{ ansible_facts["distribution"] }}-{{ ansible_facts["distribution_major_version"] }} -
3434
use bonding instead
3535
when:
36-
- ansible_distribution_major_version | int > 9
37-
- ansible_distribution in __network_rh_distros
36+
- ansible_facts["distribution_major_version"] | int > 9
37+
- ansible_facts["distribution"] in __network_rh_distros
3838
- network_connections | selectattr("type", "defined") |
3939
selectattr("type", "match", "^team$") | list | length > 0 or
4040
network_state.get("interfaces", []) | selectattr("type", "defined") |
@@ -49,8 +49,8 @@
4949
register: dnf_package_update_info
5050
check_mode: true
5151
when:
52-
- ansible_distribution == 'Fedora' or
53-
ansible_distribution_major_version | int > 7
52+
- ansible_facts["distribution"] == 'Fedora' or
53+
ansible_facts["distribution_major_version"] | int > 7
5454
- __network_wireless_connections_defined
5555
or __network_team_connections_defined
5656
- not __network_is_ostree
@@ -64,7 +64,7 @@
6464
register: yum_package_update_info
6565
check_mode: true
6666
when:
67-
- ansible_distribution_major_version | int < 8
67+
- ansible_facts["distribution_major_version"] | int < 8
6868
- __network_wireless_connections_defined
6969
or __network_team_connections_defined
7070
- not __network_is_ostree
@@ -112,10 +112,10 @@
112112
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
113113
when:
114114
- network_state != {}
115-
- ansible_distribution == 'Fedora' and
116-
ansible_distribution_major_version | int > 27 or
117-
ansible_distribution != 'Fedora' and
118-
ansible_distribution_major_version | int > 7
115+
- ansible_facts["distribution"] == 'Fedora' and
116+
ansible_facts["distribution_major_version"] | int > 27 or
117+
ansible_facts["distribution"] != 'Fedora' and
118+
ansible_facts["distribution_major_version"] | int > 7
119119

120120
- name: Install python3-libnmstate when using network_state variable
121121
package:
@@ -126,10 +126,10 @@
126126
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
127127
when:
128128
- network_state != {}
129-
- ansible_distribution == 'Fedora' and
130-
ansible_distribution_major_version | int > 34 or
131-
ansible_distribution != 'Fedora' and
132-
ansible_distribution_major_version | int > 8
129+
- ansible_facts["distribution"] == 'Fedora' and
130+
ansible_facts["distribution_major_version"] | int > 34 or
131+
ansible_facts["distribution"] != 'Fedora' and
132+
ansible_facts["distribution_major_version"] | int > 8
133133

134134
# If network packages changed and wireless or team connections are specified,
135135
# NetworkManager must be restarted, and the user needs to explicitly consent

tests/ensure_provider_tests.py

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
GET_NM_VERSION = """
1313
- name: Install NetworkManager and get NetworkManager version
1414
when:
15-
- ansible_distribution_major_version != '6'
15+
- ansible_facts['distribution_major_version'] != '6'
1616
tags:
1717
- always
1818
block:
@@ -58,24 +58,24 @@
5858
{comment}- name: Import the playbook '{test_playbook}'
5959
import_playbook: {test_playbook}
6060
when:
61-
- ansible_distribution_major_version != '6'
61+
- ansible_facts['distribution_major_version'] != '6'
6262
{minimum_nm_version_check}{extra_run_condition}"""
6363

6464
MINIMUM_VERSION = "minimum_version"
6565
EXTRA_RUN_CONDITION = "extra_run_condition"
6666
NM_ONLY_TESTS = {
6767
"playbooks/tests_802_1x_updated.yml": {
6868
EXTRA_RUN_CONDITION: (
69-
"(ansible_distribution != 'RedHat' and\n"
70-
" ansible_distribution_major_version | int > 7) or\n"
71-
" ansible_distribution_major_version | int == 8"
69+
"(ansible_facts['distribution'] != 'RedHat' and\n"
70+
" ansible_facts['distribution_major_version'] | int > 7) or\n"
71+
" ansible_facts['distribution_major_version'] | int == 8"
7272
),
7373
},
7474
"playbooks/tests_802_1x.yml": {
7575
EXTRA_RUN_CONDITION: (
76-
"(ansible_distribution != 'RedHat' and\n"
77-
" ansible_distribution_major_version | int > 7) or\n"
78-
" ansible_distribution_major_version | int == 8"
76+
"(ansible_facts['distribution'] != 'RedHat' and\n"
77+
" ansible_facts['distribution_major_version'] | int > 7) or\n"
78+
" ansible_facts['distribution_major_version'] | int == 8"
7979
),
8080
},
8181
"playbooks/tests_ignore_auto_dns.yml": {},
@@ -94,18 +94,18 @@
9494
MINIMUM_VERSION: "'1.20.0'",
9595
"comment": "# NetworKmanager 1.20.0 added support for forgetting profiles",
9696
EXTRA_RUN_CONDITION: (
97-
"(ansible_distribution == 'Fedora'\n"
98-
" and ansible_distribution_major_version | int < 41)\n"
99-
" or ansible_distribution not in ['RedHat', 'CentOS', 'Fedora']\n"
100-
" or ansible_distribution_major_version | int < 9"
97+
"(ansible_facts['distribution'] == 'Fedora'\n"
98+
" and ansible_facts['distribution_major_version'] | int < 41)\n"
99+
" or ansible_facts['distribution'] not in ['RedHat', 'CentOS', 'Fedora']\n"
100+
" or ansible_facts['distribution_major_version'] | int < 9"
101101
),
102102
},
103103
"playbooks/tests_eth_pci_address_match.yml": {
104104
MINIMUM_VERSION: "'1.26.0'",
105105
"comment": "# NetworkManager 1.26.0 added support for match.path setting",
106106
},
107107
"playbooks/tests_network_state.yml": {
108-
EXTRA_RUN_CONDITION: "ansible_distribution_major_version | int > 7",
108+
EXTRA_RUN_CONDITION: "ansible_facts['distribution_major_version'] | int > 7",
109109
},
110110
"playbooks/tests_reapply.yml": {},
111111
"playbooks/tests_route_table.yml": {},
@@ -117,32 +117,30 @@
117117
"playbooks/tests_routing_rules.yml": {},
118118
# teaming support dropped in EL10
119119
"playbooks/tests_team.yml": {
120-
EXTRA_RUN_CONDITION: "ansible_distribution not in ['RedHat', 'CentOS'] or\n ansible_distr\
121-
ibution_major_version | int < 10",
120+
EXTRA_RUN_CONDITION: "ansible_facts['distribution'] not in ['RedHat', 'CentOS'] or\n ansible_facts['distribution_major_version'] | int < 10",
122121
},
123122
"playbooks/tests_team_plugin_installation.yml": {
124-
EXTRA_RUN_CONDITION: "ansible_distribution not in ['RedHat', 'CentOS'] or\n ansible_distr\
125-
ibution_major_version | int < 10",
123+
EXTRA_RUN_CONDITION: "ansible_facts['distribution'] not in ['RedHat', 'CentOS'] or\n ansible_facts['distribution_major_version'] | int < 10",
126124
},
127125
# mac80211_hwsim (used for tests_wireless) only seems to be available
128126
# and working on RHEL/CentOS 7
129127
"playbooks/tests_wireless.yml": {
130-
EXTRA_RUN_CONDITION: "ansible_distribution_major_version == '7'",
128+
EXTRA_RUN_CONDITION: "ansible_facts['distribution_major_version'] == '7'",
131129
},
132130
"playbooks/tests_wireless_and_network_restart.yml": {},
133131
"playbooks/tests_wireless_plugin_installation.yml": {},
134132
"playbooks/tests_wireless_wpa3_owe.yml": {
135133
"comment": "# OWE has not been supported by NetworkManager 1.18.8 on \
136134
RHEL 7(dist-tag). Failed in setting up mock wifi on RHEL 8",
137-
EXTRA_RUN_CONDITION: "ansible_distribution_major_version > '7' and \
138-
ansible_distribution == 'CentOS' or\n ansible_distribution_major_version > '32' \
139-
and ansible_distribution == 'Fedora'",
135+
EXTRA_RUN_CONDITION: "ansible_facts['distribution_major_version'] > '7' and \
136+
ansible_facts['distribution'] == 'CentOS' or\n ansible_facts['distribution_major_version'] > '32' \
137+
and ansible_facts['distribution'] == 'Fedora'",
140138
},
141139
"playbooks/tests_wireless_wpa3_sae.yml": {
142140
"comment": "# SAE has not been supported by NetworkManager 1.18.8 on \
143141
RHEL 7. Failed in setting up mock wifi on RHEL 8",
144-
EXTRA_RUN_CONDITION: "ansible_distribution_major_version != '7' and \
145-
ansible_distribution != 'RedHat'",
142+
EXTRA_RUN_CONDITION: "ansible_facts['distribution_major_version'] != '7' and \
143+
ansible_facts['distribution'] != 'RedHat'",
146144
},
147145
}
148146
# NM_CONDITIONAL_TESTS is used to store the test playbooks which are demanding for NM
@@ -187,8 +185,8 @@
187185
188186
- name: Import the playbook '{test_playbook}'
189187
import_playbook: {test_playbook}
190-
when: (ansible_distribution in ['CentOS','RedHat'] and\n \
191-
ansible_distribution_major_version | int < 9)
188+
when: (ansible_facts['distribution'] in ['CentOS','RedHat'] and\n \
189+
ansible_facts['distribution_major_version'] | int < 9)
192190
"""
193191

194192

tests/playbooks/tests_ethernet.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
when:
5353
- network_provider == 'nm'
5454
# RHEL up to 8 uses initscripts backend
55-
- ansible_distribution_major_version | int >= 9
55+
- ansible_facts["distribution_major_version"] | int >= 9
5656

5757
- name: Assert settings in NM connection file
5858
assert:
@@ -64,7 +64,7 @@
6464
when:
6565
- network_provider == 'nm'
6666
# RHEL up to 8 uses initscripts backend
67-
- ansible_distribution_major_version | int >= 9
67+
- ansible_facts["distribution_major_version"] | int >= 9
6868

6969
- name: Get NM connection status
7070
command: "nmcli connection show {{ interface }}"
@@ -82,7 +82,7 @@
8282
slurp:
8383
src: "/etc/sysconfig/network-scripts/ifcfg-{{ interface }}"
8484
register: initscripts_connection_file
85-
when: network_provider == 'initscripts' or ansible_distribution_major_version | int < 9
85+
when: network_provider == 'initscripts' or ansible_facts["distribution_major_version"] | int < 9
8686

8787
- name: Assert settings in initscripts connection file
8888
assert:
@@ -91,7 +91,7 @@
9191
- "'DEVICE={{ interface }}' in initscripts_connection_file.content | b64decode"
9292
- "'IPADDR=192.0.2.1' in initscripts_connection_file.content | b64decode"
9393
- "'PREFIX=24' in initscripts_connection_file.content | b64decode"
94-
when: network_provider == 'initscripts' or ansible_distribution_major_version | int < 9
94+
when: network_provider == 'initscripts' or ansible_facts["distribution_major_version"] | int < 9
9595

9696
- name: Include the tasks 'down_profile+delete_interface.yml'
9797
include_tasks: tasks/down_profile+delete_interface.yml

tests/playbooks/tests_ipv6.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
ip netns exec ns1 ip link set peer{{ interface }} up
2121
when:
2222
# netns not available on RHEL/CentOS 6
23-
- ansible_distribution_major_version != '6'
23+
- ansible_facts['distribution_major_version'] != '6'
2424
changed_when: false
2525
- name: Test IPv6 config
2626
block:
@@ -87,7 +87,7 @@
8787
- name: Test gateway can be pinged
8888
command: ping6 -c1 2001:db8::1
8989
when:
90-
- ansible_distribution_major_version != '6'
90+
- ansible_facts['distribution_major_version'] != '6'
9191
changed_when: false
9292
always:
9393
- name: "TEARDOWN: remove profiles."
@@ -109,7 +109,7 @@
109109
- name: Clean up namespace
110110
command: ip netns delete ns1
111111
when:
112-
- ansible_distribution_major_version != '6'
112+
- ansible_facts['distribution_major_version'] != '6'
113113
changed_when: false
114114
- name: Verify network state restored to default
115115
include_tasks: tasks/check_network_dns.yml

tests/playbooks/tests_ipv6_dns_search.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,14 @@
126126
state: up
127127
ignore_errors: true # noqa ignore-errors
128128
changed_when: false
129-
when: ansible_distribution_major_version | int > 7
129+
when: ansible_facts["distribution_major_version"] | int > 7
130130

131131
- name: Assert that reconfiguring network connection is failed
132132
assert:
133133
that:
134134
- __network_connections_result.failed
135135
msg: reconfiguring network connection is not failed
136-
when: ansible_distribution_major_version | int > 7
136+
when: ansible_facts["distribution_major_version"] | int > 7
137137

138138
- name: Assert that configuring DNS search setting is not allowed when
139139
both IPv4 and IPv6 are disabled
@@ -145,7 +145,7 @@
145145
msg: Reconfiguring network connection is not failed with the error
146146
"Setting 'dns_search', 'dns_options', and 'dns_priority' are not
147147
allowed when both IPv4 and IPv6 are disabled."
148-
when: ansible_distribution_major_version | int > 7
148+
when: ansible_facts["distribution_major_version"] | int > 7
149149

150150
always:
151151
- name: Clean up the test device and the connection profile

0 commit comments

Comments
 (0)