From e42883e2c65fa9efe93950ff734e929294d662ac Mon Sep 17 00:00:00 2001 From: Rich Megginson Date: Tue, 6 Jan 2026 14:56:38 -0700 Subject: [PATCH] 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 --- CHANGELOG.md | 2 +- README-ostree.md | 4 ++-- tests/inventory.yaml.j2 | 14 ++++++++++---- tests/tests_ssh.yml | 20 ++++++++++---------- tests/tests_ssh_reboot.yml | 18 +++++++++--------- tests/vars/rh_distros_vars.yml | 4 ++-- vars/main.yml | 4 ++-- 7 files changed, 36 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 302760ec..11141d31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -305,7 +305,7 @@ Changelog ### Bug Fixes -- Use ansible_os_family in template (#133) +- Use ansible_facts["os_family"] in template (#133) ### Other Changes diff --git a/README-ostree.md b/README-ostree.md index a9f0185b..af5bcdcd 100644 --- a/README-ostree.md +++ b/README-ostree.md @@ -20,8 +20,8 @@ Usage: .ostree/get_ostree_data.sh packages runtime DISTRO-VERSION FORMAT ``` -`DISTRO-VERSION` is in the format that Ansible uses for `ansible_distribution` -and `ansible_distribution_version` - for example, `Fedora-38`, `CentOS-8`, +`DISTRO-VERSION` is in the format that Ansible uses for `ansible_facts["distribution"]` +and `ansible_facts["distribution_version"]` - for example, `Fedora-38`, `CentOS-8`, `RedHat-9.4` `FORMAT` is one of `toml`, `json`, `yaml`, `raw` diff --git a/tests/inventory.yaml.j2 b/tests/inventory.yaml.j2 index 7b463727..41852403 100644 --- a/tests/inventory.yaml.j2 +++ b/tests/inventory.yaml.j2 @@ -5,10 +5,16 @@ all: ansible_connection: local ansible_host: localhost {% else %} -{% for key in ["ansible_all_ipv4_addresses", "ansible_all_ipv6_addresses", - "ansible_default_ipv4", "ansible_default_ipv6", "ansible_host", - "ansible_port", "ansible_ssh_common_args", - "ansible_ssh_private_key_file","ansible_user"] %} +{# Facts - these are accessed via ansible_facts and written with legacy key names #} +{% for key in ['all_ipv4_addresses', 'all_ipv6_addresses', + 'default_ipv4', 'default_ipv6'] %} +{% if key in hostvars[inventory_hostname]['ansible_facts'] %} + ansible_{{ key }}: {{ hostvars[inventory_hostname]['ansible_facts'][key] }} +{% endif %} +{% endfor %} +{# Connection variables - these are accessed directly #} +{% for key in ['ansible_host', 'ansible_port', 'ansible_ssh_common_args', + 'ansible_ssh_private_key_file', 'ansible_user'] %} {% if key in hostvars[inventory_hostname] %} {{ key }}: {{ hostvars[inventory_hostname][key] }} {% endif %} diff --git a/tests/tests_ssh.yml b/tests/tests_ssh.yml index cf304fe7..6faa74fb 100644 --- a/tests/tests_ssh.yml +++ b/tests/tests_ssh.yml @@ -15,8 +15,8 @@ # to itself. kdump_test_ssh_server_outside: "{{ inventory_hostname }}" kdump_test_ssh_source: "{{ - ansible_env['SSH_CONNECTION'].split()[0] - if 'SSH_CONNECTION' in ansible_env + ansible_facts['env']['SSH_CONNECTION'].split()[0] + if 'SSH_CONNECTION' in ansible_facts['env'] else '127.0.0.1' }}" # this is the address at which the ssh dump server can be reached @@ -24,17 +24,17 @@ kdump_test_ssh_server_inside: >- {{ kdump_test_ssh_source if kdump_test_ssh_source in - hostvars[kdump_test_ssh_server_outside]['ansible_all_ipv4_addresses'] - + hostvars[kdump_test_ssh_server_outside]['ansible_all_ipv6_addresses'] + hostvars[kdump_test_ssh_server_outside]['ansible_facts']['all_ipv4_addresses'] + + hostvars[kdump_test_ssh_server_outside]['ansible_facts']['all_ipv6_addresses'] else - hostvars[kdump_test_ssh_server_outside]['ansible_default_ipv4']['address'] + hostvars[kdump_test_ssh_server_outside]['ansible_facts']['default_ipv4']['address'] }} # This is the outside address. Ansible will connect to it to # copy the ssh key. kdump_ssh_server: "{{ kdump_test_ssh_server_outside }}" kdump_ssh_user: >- - {{ hostvars[kdump_test_ssh_server_outside]['ansible_user_id'] }} + {{ hostvars[kdump_test_ssh_server_outside]['ansible_facts']['user_id'] }} kdump_path: /tmp/tests_ssh kdump_target: type: ssh @@ -65,8 +65,8 @@ debug: msg: Skipping the test on EL 6 when storing logs over ssh to localhost when: - - ansible_distribution in ['CentOS','RedHat'] - - ansible_distribution_major_version == '6' + - ansible_facts['distribution'] in ['CentOS','RedHat'] + - ansible_facts['distribution_major_version'] == '6' - kdump_test_ssh_server_outside == inventory_hostname # The skip is required on EL 6 because mkdumprd on EL 6 does not work when @@ -74,8 +74,8 @@ - name: Skip the test on EL 6 when control node == managed node meta: end_host when: - - ansible_distribution in ['CentOS','RedHat'] - - ansible_distribution_major_version == '6' + - ansible_facts['distribution'] in ['CentOS','RedHat'] + - ansible_facts['distribution_major_version'] == '6' - kdump_test_ssh_server_outside == inventory_hostname - name: >- diff --git a/tests/tests_ssh_reboot.yml b/tests/tests_ssh_reboot.yml index 6c1d3b70..5e890969 100644 --- a/tests/tests_ssh_reboot.yml +++ b/tests/tests_ssh_reboot.yml @@ -15,8 +15,8 @@ # to itself. kdump_test_ssh_server_outside: "{{ inventory_hostname }}" kdump_test_ssh_source: "{{ - ansible_env['SSH_CONNECTION'].split()[0] - if 'SSH_CONNECTION' in ansible_env + ansible_facts['env']['SSH_CONNECTION'].split()[0] + if 'SSH_CONNECTION' in ansible_facts['env'] else '127.0.0.1' }}" # this is the address at which the ssh dump server can be reached @@ -24,10 +24,10 @@ kdump_test_ssh_server_inside: >- {{ kdump_test_ssh_source if kdump_test_ssh_source in - hostvars[kdump_test_ssh_server_outside]['ansible_all_ipv4_addresses'] - + hostvars[kdump_test_ssh_server_outside]['ansible_all_ipv6_addresses'] + hostvars[kdump_test_ssh_server_outside]['ansible_facts']['all_ipv4_addresses'] + + hostvars[kdump_test_ssh_server_outside]['ansible_facts']['all_ipv6_addresses'] else - hostvars[kdump_test_ssh_server_outside]['ansible_default_ipv4']['address'] + hostvars[kdump_test_ssh_server_outside]['ansible_facts']['default_ipv4']['address'] }} # This is the outside address. Ansible will connect to it to @@ -82,8 +82,8 @@ debug: msg: Skipping the test on EL 6 because control node == managed node when: - - ansible_distribution in ['CentOS','RedHat'] - - ansible_distribution_major_version == '6' + - ansible_facts['distribution'] in ['CentOS','RedHat'] + - ansible_facts['distribution_major_version'] == '6' - kdump_test_ssh_server_outside == inventory_hostname # The skip is required on EL 6 because mkdumprd on EL 6 does not work when @@ -91,8 +91,8 @@ - name: Skip the test on EL 6 when control node == managed node meta: end_host when: - - ansible_distribution in ['CentOS','RedHat'] - - ansible_distribution_major_version == '6' + - ansible_facts['distribution'] in ['CentOS','RedHat'] + - ansible_facts['distribution_major_version'] == '6' - kdump_test_ssh_server_outside == inventory_hostname - name: Determine if system is ostree and set flag diff --git a/tests/vars/rh_distros_vars.yml b/tests/vars/rh_distros_vars.yml index 43430381..183fa59b 100644 --- a/tests/vars/rh_distros_vars.yml +++ b/tests/vars/rh_distros_vars.yml @@ -14,7 +14,7 @@ __kdump_rh_distros: __kdump_rh_distros_fedora: "{{ __kdump_rh_distros + ['Fedora'] }}" # Use this in conditionals to check if distro is Red Hat or clone -__kdump_is_rh_distro: "{{ ansible_distribution in __kdump_rh_distros }}" +__kdump_is_rh_distro: "{{ ansible_facts['distribution'] in __kdump_rh_distros }}" # Use this in conditionals to check if distro is Red Hat or clone, or Fedora -__kdump_is_rh_distro_fedora: "{{ ansible_distribution in __kdump_rh_distros_fedora }}" +__kdump_is_rh_distro_fedora: "{{ ansible_facts['distribution'] in __kdump_rh_distros_fedora }}" diff --git a/vars/main.yml b/vars/main.yml index 34927a4b..163eb1d9 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -38,10 +38,10 @@ __kdump_rh_distros: __kdump_rh_distros_fedora: "{{ __kdump_rh_distros + ['Fedora'] }}" # Use this in conditionals to check if distro is Red Hat or clone -__kdump_is_rh_distro: "{{ ansible_distribution in __kdump_rh_distros }}" +__kdump_is_rh_distro: "{{ ansible_facts['distribution'] in __kdump_rh_distros }}" # Use this in conditionals to check if distro is Red Hat or clone, or Fedora -__kdump_is_rh_distro_fedora: "{{ ansible_distribution in __kdump_rh_distros_fedora }}" +__kdump_is_rh_distro_fedora: "{{ ansible_facts['distribution'] in __kdump_rh_distros_fedora }}" # END - DO NOT EDIT THIS BLOCK - rh distros variables __kdump_service: kdump