Skip to content

Commit a1d6274

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 <[email protected]>
1 parent 0481172 commit a1d6274

File tree

7 files changed

+36
-30
lines changed

7 files changed

+36
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ Changelog
305305

306306
### Bug Fixes
307307

308-
- Use ansible_os_family in template (#133)
308+
- Use ansible_facts["os_family"] in template (#133)
309309

310310
### Other Changes
311311

README-ostree.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ Usage:
2020
.ostree/get_ostree_data.sh packages runtime DISTRO-VERSION FORMAT
2121
```
2222

23-
`DISTRO-VERSION` is in the format that Ansible uses for `ansible_distribution`
24-
and `ansible_distribution_version` - for example, `Fedora-38`, `CentOS-8`,
23+
`DISTRO-VERSION` is in the format that Ansible uses for `ansible_facts["distribution"]`
24+
and `ansible_facts["distribution_version"]` - for example, `Fedora-38`, `CentOS-8`,
2525
`RedHat-9.4`
2626

2727
`FORMAT` is one of `toml`, `json`, `yaml`, `raw`

tests/inventory.yaml.j2

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ all:
55
ansible_connection: local
66
ansible_host: localhost
77
{% else %}
8-
{% for key in ["ansible_all_ipv4_addresses", "ansible_all_ipv6_addresses",
9-
"ansible_default_ipv4", "ansible_default_ipv6", "ansible_host",
10-
"ansible_port", "ansible_ssh_common_args",
11-
"ansible_ssh_private_key_file","ansible_user"] %}
8+
{# Facts - these are accessed via ansible_facts and written with legacy key names #}
9+
{% for key in ['all_ipv4_addresses', 'all_ipv6_addresses',
10+
'default_ipv4', 'default_ipv6'] %}
11+
{% if key in hostvars[inventory_hostname]['ansible_facts'] %}
12+
ansible_{{ key }}: {{ hostvars[inventory_hostname]['ansible_facts'][key] }}
13+
{% endif %}
14+
{% endfor %}
15+
{# Connection variables - these are accessed directly #}
16+
{% for key in ['ansible_host', 'ansible_port', 'ansible_ssh_common_args',
17+
'ansible_ssh_private_key_file', 'ansible_user'] %}
1218
{% if key in hostvars[inventory_hostname] %}
1319
{{ key }}: {{ hostvars[inventory_hostname][key] }}
1420
{% endif %}

tests/tests_ssh.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,26 @@
1515
# to itself.
1616
kdump_test_ssh_server_outside: "{{ inventory_hostname }}"
1717
kdump_test_ssh_source: "{{
18-
ansible_env['SSH_CONNECTION'].split()[0]
19-
if 'SSH_CONNECTION' in ansible_env
18+
ansible_facts['env']['SSH_CONNECTION'].split()[0]
19+
if 'SSH_CONNECTION' in ansible_facts['env']
2020
else '127.0.0.1' }}"
2121

2222
# this is the address at which the ssh dump server can be reached
2323
# from the managed host. Dumps will be uploaded there.
2424
kdump_test_ssh_server_inside: >-
2525
{{
2626
kdump_test_ssh_source if kdump_test_ssh_source in
27-
hostvars[kdump_test_ssh_server_outside]['ansible_all_ipv4_addresses']
28-
+ hostvars[kdump_test_ssh_server_outside]['ansible_all_ipv6_addresses']
27+
hostvars[kdump_test_ssh_server_outside]['ansible_facts']['all_ipv4_addresses']
28+
+ hostvars[kdump_test_ssh_server_outside]['ansible_facts']['all_ipv6_addresses']
2929
else
30-
hostvars[kdump_test_ssh_server_outside]['ansible_default_ipv4']['address']
30+
hostvars[kdump_test_ssh_server_outside]['ansible_facts']['default_ipv4']['address']
3131
}}
3232
3333
# This is the outside address. Ansible will connect to it to
3434
# copy the ssh key.
3535
kdump_ssh_server: "{{ kdump_test_ssh_server_outside }}"
3636
kdump_ssh_user: >-
37-
{{ hostvars[kdump_test_ssh_server_outside]['ansible_user_id'] }}
37+
{{ hostvars[kdump_test_ssh_server_outside]['ansible_facts']['user_id'] }}
3838
kdump_path: /tmp/tests_ssh
3939
kdump_target:
4040
type: ssh
@@ -65,17 +65,17 @@
6565
debug:
6666
msg: Skipping the test on EL 6 when storing logs over ssh to localhost
6767
when:
68-
- ansible_distribution in ['CentOS','RedHat']
69-
- ansible_distribution_major_version == '6'
68+
- ansible_facts['distribution'] in ['CentOS','RedHat']
69+
- ansible_facts['distribution_major_version'] == '6'
7070
- kdump_test_ssh_server_outside == inventory_hostname
7171

7272
# The skip is required on EL 6 because mkdumprd on EL 6 does not work when
7373
# configuring ssh to localhost
7474
- name: Skip the test on EL 6 when control node == managed node
7575
meta: end_host
7676
when:
77-
- ansible_distribution in ['CentOS','RedHat']
78-
- ansible_distribution_major_version == '6'
77+
- ansible_facts['distribution'] in ['CentOS','RedHat']
78+
- ansible_facts['distribution_major_version'] == '6'
7979
- kdump_test_ssh_server_outside == inventory_hostname
8080

8181
- name: >-

tests/tests_ssh_reboot.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@
1515
# to itself.
1616
kdump_test_ssh_server_outside: "{{ inventory_hostname }}"
1717
kdump_test_ssh_source: "{{
18-
ansible_env['SSH_CONNECTION'].split()[0]
19-
if 'SSH_CONNECTION' in ansible_env
18+
ansible_facts['env']['SSH_CONNECTION'].split()[0]
19+
if 'SSH_CONNECTION' in ansible_facts['env']
2020
else '127.0.0.1' }}"
2121

2222
# this is the address at which the ssh dump server can be reached
2323
# from the managed host. Dumps will be uploaded there.
2424
kdump_test_ssh_server_inside: >-
2525
{{
2626
kdump_test_ssh_source if kdump_test_ssh_source in
27-
hostvars[kdump_test_ssh_server_outside]['ansible_all_ipv4_addresses']
28-
+ hostvars[kdump_test_ssh_server_outside]['ansible_all_ipv6_addresses']
27+
hostvars[kdump_test_ssh_server_outside]['ansible_facts']['all_ipv4_addresses']
28+
+ hostvars[kdump_test_ssh_server_outside]['ansible_facts']['all_ipv6_addresses']
2929
else
30-
hostvars[kdump_test_ssh_server_outside]['ansible_default_ipv4']['address']
30+
hostvars[kdump_test_ssh_server_outside]['ansible_facts']['default_ipv4']['address']
3131
}}
3232
3333
# This is the outside address. Ansible will connect to it to
@@ -82,17 +82,17 @@
8282
debug:
8383
msg: Skipping the test on EL 6 because control node == managed node
8484
when:
85-
- ansible_distribution in ['CentOS','RedHat']
86-
- ansible_distribution_major_version == '6'
85+
- ansible_facts['distribution'] in ['CentOS','RedHat']
86+
- ansible_facts['distribution_major_version'] == '6'
8787
- kdump_test_ssh_server_outside == inventory_hostname
8888

8989
# The skip is required on EL 6 because mkdumprd on EL 6 does not work when
9090
# configuring ssh to localhost
9191
- name: Skip the test on EL 6 when control node == managed node
9292
meta: end_host
9393
when:
94-
- ansible_distribution in ['CentOS','RedHat']
95-
- ansible_distribution_major_version == '6'
94+
- ansible_facts['distribution'] in ['CentOS','RedHat']
95+
- ansible_facts['distribution_major_version'] == '6'
9696
- kdump_test_ssh_server_outside == inventory_hostname
9797

9898
- name: Determine if system is ostree and set flag

tests/vars/rh_distros_vars.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ __kdump_rh_distros:
1414
__kdump_rh_distros_fedora: "{{ __kdump_rh_distros + ['Fedora'] }}"
1515

1616
# Use this in conditionals to check if distro is Red Hat or clone
17-
__kdump_is_rh_distro: "{{ ansible_distribution in __kdump_rh_distros }}"
17+
__kdump_is_rh_distro: "{{ ansible_facts['distribution'] in __kdump_rh_distros }}"
1818

1919
# Use this in conditionals to check if distro is Red Hat or clone, or Fedora
20-
__kdump_is_rh_distro_fedora: "{{ ansible_distribution in __kdump_rh_distros_fedora }}"
20+
__kdump_is_rh_distro_fedora: "{{ ansible_facts['distribution'] in __kdump_rh_distros_fedora }}"

vars/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ __kdump_rh_distros:
3838
__kdump_rh_distros_fedora: "{{ __kdump_rh_distros + ['Fedora'] }}"
3939

4040
# Use this in conditionals to check if distro is Red Hat or clone
41-
__kdump_is_rh_distro: "{{ ansible_distribution in __kdump_rh_distros }}"
41+
__kdump_is_rh_distro: "{{ ansible_facts['distribution'] in __kdump_rh_distros }}"
4242

4343
# Use this in conditionals to check if distro is Red Hat or clone, or Fedora
44-
__kdump_is_rh_distro_fedora: "{{ ansible_distribution in __kdump_rh_distros_fedora }}"
44+
__kdump_is_rh_distro_fedora: "{{ ansible_facts['distribution'] in __kdump_rh_distros_fedora }}"
4545
# END - DO NOT EDIT THIS BLOCK - rh distros variables
4646

4747
__kdump_service: kdump

0 commit comments

Comments
 (0)