Skip to content

Conversation

@ricardoleal
Copy link

@ricardoleal ricardoleal commented Jan 19, 2026

Migrate to ansible_facts dictionary format

What

Updated fact references from ansible_* variables to ansible_facts['*'] dictionary format.

Why

The old ansible_distribution, ansible_os_family etc. format is being phased out. Ansible 2.20 deprecated the INJECT_FACTS_AS_VARS setting, and it'll default to False in version 2.24. After that, the old format won't work anymore unless you explicitly enable it.

Changes

Changed things like:

ansible_os_familyansible_facts['os_family']
ansible_distributionansible_facts['distribution']
ansible_distribution_major_versionansible_facts['distribution_major_version']

Testing

Tested locally against Ubuntu instance. Can't run molecule on Mac, so feel free to test it and update if needed.

Notes

This keeps the role working with future Ansible versions while still being compatible with current ones. The new format has been available since Ansible 2.5, so there shouldn't be any compatibility issues.

- change ansible_facts usage from dot notation to bracket notation
- ensure compatibility with newer Ansible versions
@geerlingguy
Copy link
Owner

Technically the dot syntax should still work... was it giving you errors?

@ricardoleal
Copy link
Author

It was giving warnings, since i had to fix all my roles using the recommended approach seen on Ansible documentation

6 - name: Include OS-specific variables for Amazon.
7   include_vars: "{{ ansible_distribution }}-{{ ansible_distribution_version}}.yml"
8   when: ansible_distribution == 'Amazon'
          ^ column 9

Use `ansible_facts["fact_name"]` (no `ansible_` prefix) instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants