-
Notifications
You must be signed in to change notification settings - Fork 35
refactor: handle INJECT_FACTS_AS_VARS=false by using ansible_facts instead #314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…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. Add the role facts to __firewall_required_facts and __firewall_no_subsets_facts as needed. 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]>
Reviewer's GuideRefactors firewall role and its tests to stop using deprecated top-level Ansible fact variables, instead reading from ansible_facts and ensuring the required facts are gathered correctly when INJECT_FACTS_AS_VARS is false. File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey - I've found 2 issues, and left some high level feedback:
- In tasks/main.yml, using
ansible_facts['discovered_interpreter_python']inside thed()default chain can raise a template error if that key is missing; consider guarding it withansible_facts.get('discovered_interpreter_python')or adefaultfilter on the facts lookup itself. - Several test playbooks repeat the same distribution/major_version conditionals that now also exist as
__firewall_is_rh_distro(_fedora); consider reusing these helper vars in tests to avoid duplication and keep future distro logic changes in one place.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In tasks/main.yml, using `ansible_facts['discovered_interpreter_python']` inside the `d()` default chain can raise a template error if that key is missing; consider guarding it with `ansible_facts.get('discovered_interpreter_python')` or a `default` filter on the facts lookup itself.
- Several test playbooks repeat the same distribution/major_version conditionals that now also exist as `__firewall_is_rh_distro(_fedora)`; consider reusing these helper vars in tests to avoid duplication and keep future distro logic changes in one place.
## Individual Comments
### Comment 1
<location> `vars/main.yml:14-21` </location>
<code_context>
- python_version
- service_mgr
+__firewall_no_subsets_facts:
+ - discovered_interpreter_python
+
# the subsets of ansible_facts that need to be gathered in case any of the
# facts in required_facts is missing; see the documentation of
# the 'gather_subset' parameter of the 'setup' module
__firewall_required_facts_subsets: "{{ ['!all', '!min'] +
- __firewall_required_facts }}"
+ __firewall_required_facts | difference(__firewall_no_subsets_facts) }}"
__firewall_packages_base: [firewalld]
</code_context>
<issue_to_address>
**nitpick:** The `difference` filter plus list concatenation relies on Jinja operator precedence; consider making it explicit for readability.
This expression depends on Jinja evaluating it as `(['!all', '!min'] + __firewall_required_facts) | difference(...)`. To avoid ambiguity and make future changes safer, consider adding parentheses:
```yaml
__firewall_required_facts_subsets: "{{ (['!all', '!min'] + __firewall_required_facts)
| difference(__firewall_no_subsets_facts) }}"
```
</issue_to_address>
### Comment 2
<location> `tasks/main.yml:42-43` </location>
<code_context>
list | length > 0 }}"
__firewall_python_cmd: "{{ ansible_python_interpreter |
- d(discovered_interpreter_python) }}"
+ d(ansible_facts['discovered_interpreter_python']) }}"
__firewall_report_changed: true
</code_context>
<issue_to_address>
**suggestion:** Indexing `ansible_facts['discovered_interpreter_python']` directly may raise an error if the fact is unexpectedly missing.
Inside `d()`, this will fail outright if `discovered_interpreter_python` isn’t in `ansible_facts`. To make it more robust while preserving the new behavior, you could use:
```yaml
__firewall_python_cmd: "{{ ansible_python_interpreter |
d(ansible_facts.get('discovered_interpreter_python')) }}"
```
```suggestion
__firewall_python_cmd: "{{ ansible_python_interpreter |
d(ansible_facts.get('discovered_interpreter_python')) }}"
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| __firewall_no_subsets_facts: | ||
| - discovered_interpreter_python | ||
|
|
||
| # the subsets of ansible_facts that need to be gathered in case any of the | ||
| # facts in required_facts is missing; see the documentation of | ||
| # the 'gather_subset' parameter of the 'setup' module | ||
| __firewall_required_facts_subsets: "{{ ['!all', '!min'] + | ||
| __firewall_required_facts }}" | ||
| __firewall_required_facts | difference(__firewall_no_subsets_facts) }}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: The difference filter plus list concatenation relies on Jinja operator precedence; consider making it explicit for readability.
This expression depends on Jinja evaluating it as (['!all', '!min'] + __firewall_required_facts) | difference(...). To avoid ambiguity and make future changes safer, consider adding parentheses:
__firewall_required_facts_subsets: "{{ (['!all', '!min'] + __firewall_required_facts)
| difference(__firewall_no_subsets_facts) }}"| __firewall_python_cmd: "{{ ansible_python_interpreter | | ||
| d(discovered_interpreter_python) }}" | ||
| d(ansible_facts['discovered_interpreter_python']) }}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Indexing ansible_facts['discovered_interpreter_python'] directly may raise an error if the fact is unexpectedly missing.
Inside d(), this will fail outright if discovered_interpreter_python isn’t in ansible_facts. To make it more robust while preserving the new behavior, you could use:
__firewall_python_cmd: "{{ ansible_python_interpreter |
d(ansible_facts.get('discovered_interpreter_python')) }}"| __firewall_python_cmd: "{{ ansible_python_interpreter | | |
| d(discovered_interpreter_python) }}" | |
| d(ansible_facts['discovered_interpreter_python']) }}" | |
| __firewall_python_cmd: "{{ ansible_python_interpreter | | |
| d(ansible_facts.get('discovered_interpreter_python')) }}" |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #314 +/- ##
==========================================
- Coverage 61.09% 58.26% -2.83%
==========================================
Files 2 2
Lines 910 1294 +384
==========================================
+ Hits 556 754 +198
- Misses 354 540 +186
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
fixes #312 |
|
[citest] |
|
[citest_bad] |
Ansible 2.20 has deprecated the use of Ansible facts as variables. For
example,
ansible_distributionis now deprecated in favor ofansible_facts["distribution"]. This is due to making the defaultsetting
INJECT_FACTS_AS_VARS=false. For now, this will create WARNINGmessages, but in Ansible 2.24 it will be an error.
Add the role facts to __firewall_required_facts and __firewall_no_subsets_facts as needed.
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]
Summary by Sourcery
Update firewall role and tests to use structured ansible_facts instead of deprecated fact variables and ensure required facts are gathered explicitly.
Enhancements: