Skip to content

refactor: handle INJECT_FACTS_AS_VARS=false by using ansible_facts instead#213

Merged
richm merged 1 commit intolinux-system-roles:mainfrom
richm:inject-facts-as-vars
Jan 8, 2026
Merged

refactor: handle INJECT_FACTS_AS_VARS=false by using ansible_facts instead#213
richm merged 1 commit intolinux-system-roles:mainfrom
richm:inject-facts-as-vars

Conversation

@richm
Copy link
Contributor

@richm richm commented Jan 7, 2026

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

Summary by Sourcery

Update Ansible role and documentation to use the ansible_facts dictionary instead of legacy fact variables for compatibility with INJECT_FACTS_AS_VARS=false.

Bug Fixes:

  • Fix failures and warnings when INJECT_FACTS_AS_VARS is disabled by switching to ansible_facts-based lookups for distribution, OS family, and date/time metadata.

Enhancements:

  • Standardize distro detection helpers and variable file selection logic on ansible_facts-based keys across tasks and vars files.

Documentation:

  • Refresh README-ostree guidance on DISTRO-VERSION to reference ansible_facts-based distribution variables.

Tests:

  • Align test vars for Red Hat distro detection with ansible_facts-based distribution lookups.

…stead

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>
@richm richm requested a review from spetrosi as a code owner January 7, 2026 22:14
@sourcery-ai
Copy link

sourcery-ai bot commented Jan 7, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors the role to stop using deprecated top-level Ansible fact variables and instead reference facts via the ansible_facts dict, updating tasks, tests, and documentation accordingly.

Sequence diagram for manage_config backup using ansible_facts

sequenceDiagram
    participant AnsibleController
    participant PostfixRole
    participant TargetHost

    AnsibleController->>PostfixRole: run task Apply_changes
    PostfixRole->>PostfixRole: check __postfix_has_config_changed
    alt config_changed
        PostfixRole->>PostfixRole: evaluate postfix_backup_multiple
        alt postfix_backup_multiple_true
            PostfixRole->>TargetHost: setup filter=ansible_date_time
            TargetHost-->>PostfixRole: ansible_facts.date_time
        end
        PostfixRole->>PostfixRole: evaluate postfix_backup or postfix_backup_multiple
        alt backup_condition_true
            PostfixRole->>TargetHost: copy /etc/postfix/main.cf
            Note over PostfixRole,TargetHost: dest=/etc/postfix/main.cf.ansible_facts[date_time][iso8601] or backup
            TargetHost-->>PostfixRole: backup_result
        end
    end
Loading

Flow diagram for distro specific vars selection using ansible_facts

flowchart TD
    A["Start role execution"] --> B["Read ansible_facts"]
    B --> C["Compute os_family = ansible_facts['os_family']"]
    B --> D["Compute distribution = ansible_facts['distribution']"]
    B --> E["Compute distribution_major_version = ansible_facts['distribution_major_version']"]
    B --> F["Compute distribution_version = ansible_facts['distribution_version']"]

    C --> G["Try vars file os_family.yml"]
    D --> H["Try vars file distribution.yml"]
    D --> I["Try vars file distribution_distribution_major_version.yml"]
    D --> J["Try vars file distribution_distribution_version.yml"]

    G --> K["Include file if __postfix_vars_file is file"]
    H --> K
    I --> K
    J --> K

    K --> L["Set distro flags using ansible_facts['distribution'] in __postfix_rh_distros"]
    L --> M["Set distro flags using ansible_facts['distribution'] in __postfix_rh_distros_fedora"]
    M --> N["End"]
Loading

File-Level Changes

Change Details Files
Replace deprecated top-level distribution/family fact variables with ansible_facts-based lookups when selecting vars files.
  • Change include_vars loop items from ansible_os_family/ansible_distribution variants to ansible_facts['os_family'] and ansible_facts['distribution']-derived names
  • Preserve existing file naming scheme by only swapping the variable references while keeping the .yml suffixes and concatenation logic
tasks/set_facts.yml
Update backup timestamp handling in manage_config to use ansible_facts['date_time'] instead of ansible_date_time/implicit facts variable injection.
  • Adjust task description to reference ansible_facts['date_time'] instead of ansible_date_time
  • Keep setup module usage but rely on ansible_facts['date_time']['iso8601'] when building postfix main.cf backup filenames
tasks/manage_config.yml
Align Red Hat distribution helper variables with ansible_facts['distribution'] instead of ansible_distribution in both vars and test vars.
  • Switch __postfix_is_rh_distro and __postfix_is_rh_distro_fedora expressions to test membership using ansible_facts['distribution']
  • Ensure tests use the same fact access pattern as production vars
tests/vars/rh_distros_vars.yml
vars/main.yml
Update README documentation to describe DISTRO-VERSION in terms of ansible_facts-based distribution values.
  • Replace references to ansible_distribution and ansible_distribution_version with ansible_facts['distribution'] and ansible_facts['distribution_version'] in the ostree README
README-ostree.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@richm
Copy link
Contributor Author

richm commented Jan 7, 2026

[citest]

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • In vars and tests where you now use ansible_facts['distribution'] in conditionals, consider using ansible_facts.get('distribution') (optionally with a default) to avoid hard failures if facts are not gathered for some reason.
  • The updated include_vars loop in tasks/set_facts.yml repeats ansible_facts['distribution'] several times; you could assign this to a temporary var via set_fact or vars to improve readability and reduce duplication.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In vars and tests where you now use `ansible_facts['distribution']` in conditionals, consider using `ansible_facts.get('distribution')` (optionally with a default) to avoid hard failures if facts are not gathered for some reason.
- The updated `include_vars` loop in `tasks/set_facts.yml` repeats `ansible_facts['distribution']` several times; you could assign this to a temporary var via `set_fact` or `vars` to improve readability and reduce duplication.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@richm richm merged commit d00b666 into linux-system-roles:main Jan 8, 2026
29 checks passed
@richm richm deleted the inject-facts-as-vars branch January 8, 2026 15:00
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.

1 participant