Skip to content

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

@andreashaerter

Description

@andreashaerter

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationhelp wantedExtra attention is neededquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions