Ansible set_fact: Create & Set Runtime Variables (Complete Guide)
By Luca Berton · Published 2024-01-01 · Category: linux-administration
How to use ansible.builtin.set_fact to create variables at runtime. Set facts from task output, conditionals, loops, and registered variables. Cacheable facts and dynamic variable examples.
Ansible set_fact: Create & Set Runtime Variables (Complete Guide)
The ansible.builtin.set_fact module creates or modifies variables during playbook execution. Unlike static variables defined in vars files, set_fact variables are computed at runtime based on task results, conditions, and gathered facts.
Basic Usage
Set Facts from Registered Output
Set Facts from Gathered Facts
Conditional set_fact
Set Facts with Loops
Accumulate Values in a Loop
Cacheable Facts
By default, set_fact variables only live during the current play. Use cacheable: true to persist them across plays (requires fact caching):
Set Facts from API Responses
Set Complex Data Structures
set_fact vs vars vs register
| Feature | set_fact | vars | register | |---------|----------|------|----------| | When set | During task execution | Before play starts | After task runs | | Dynamic | ✅ Yes | ❌ No (Jinja2 evaluated once) | ✅ Yes | | Scope | Host-level, rest of play | Play/role level | Task-level, rest of play | | Cacheable | ✅ With cacheable: true | ❌ No | ❌ No | | Overrides vars | ✅ Higher precedence | — | ❌ No | | Use case | Computed values | Static config | Task output |
FAQ
What is ansible set_fact?
ansible.builtin.set_fact creates variables at runtime during playbook execution. Unlike vars, set_fact values are computed dynamically and can depend on task results, gathered facts, and conditions.
What is the difference between set_fact and register?
register captures the output of a specific task (stdout, rc, etc.). set_fact creates arbitrary variables with any value. Use register to capture command output, then set_fact to extract and transform what you need.
Do set_fact variables persist across plays?
By default, no — they're available for the rest of the current play only. Use cacheable: true with fact caching enabled to persist across plays and even playbook runs.
Can I use set_fact with loops?
Yes. set_fact executes for each loop iteration. To accumulate values, append to a list: set_fact: my_list: "{{ my_list + [item] }}". Initialize the list first with an empty [].
What precedence does set_fact have?
set_fact has high precedence (level 19 of 22) — it overrides vars, vars_files, group_vars, and role defaults. Only include_params, role params, and extra_vars (-e) override set_fact.
Conclusion
ansible.builtin.set_fact is essential for dynamic playbooks that adapt based on runtime data. Use it to compute variables from command output, API responses, and system facts. Combine with register, conditionals, and loops for powerful automation logic.
Related Articles • Ansible Variables: Complete Guide • Ansible register: Capture Task Output • Ansible Magic Variables: Complete Reference • Ansible debug Module: Print Variables
Category: linux-administration