Skip to content
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

Ansible style guide: INJECT_FACTS_AS_VARS = false-compatible fact usage? #18

Open
andreashaerter opened this issue Jan 26, 2025 · 0 comments
Labels
documentation Improvements or additions to documentation help wanted Extra attention is needed question Further information is requested

Comments

@andreashaerter
Copy link
Contributor

To improve consistency and compatibility with INJECT_FACTS_AS_VARS = false, update the style guide to recommend using ansible_facts['<keyname>'] instead of the shorthand {{ ansible_<keyname> }} for fact references.


Current Behavior:

The shorthand {{ ansible_<keyname> }} is commonly used to reference facts, which depends on the INJECT_FACTS_AS_VARS setting being enabled. However, for clarity, portability, and future-proofing playbooks, it is better to reference facts explicitly using ansible_facts['<keyname>'].

Example of the shorthand (current, not preferred):

- ansible.builtin.debug:  
    msg: "{{ ansible_distribution }}"  

Proposed Change:

Recommend the explicit usage of ansible_facts in the style guide for accessing facts. This ensures compatibility with INJECT_FACTS_AS_VARS = false, which disables the automatic injection of facts as top-level variables.

Example of the preferred method:

- ansible.builtin.debug:  
    msg: "{{ ansible_facts['distribution'] }}"  

Rationale:

  • Compatibility: The shorthand relies on behavior tied to INJECT_FACTS_AS_VARS = true
  • Explicit and readable: Using ansible_facts['<keyname>'] makes it clear where the data originates.
@andreashaerter andreashaerter added documentation Improvements or additions to documentation question Further information is requested labels Jan 26, 2025
@andreashaerter andreashaerter changed the title Update Ansible Style Guide: Encourage INJECT_FACTS_AS_VARS = false-compatible fact usage Ansible style guide: INJECT_FACTS_AS_VARS = false-compatible fact usage? Jan 26, 2025
@andreashaerter andreashaerter added the help wanted Extra attention is needed label Jan 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation help wanted Extra attention is needed question Further information is requested
Development

No branches or pull requests

1 participant