Skip to main content

set_fact

set_fact module

Set variables (facts) that can be used in subsequent tasks. This module allows you to define new variables or modify existing ones during playbook execution.

Examples

- name: Set simple facts
set_fact:
my_var: "hello world"
another_var: 42

- name: Set fact with templating
set_fact:
hostname_upper: "{{ ansible_hostname | upper }}"

- name: Set complex data structures
set_fact:
app_config:
name: "myapp"
version: "1.0.0"
ports: [8080, 8443]

- name: Set fact based on condition
set_fact:
environment: "{{ 'prod' if ansible_hostname.startswith('prod') else 'dev' }}"

- name: Use set facts in later tasks
debug:
msg: "Application {{ app_config.name }} version {{ app_config.version }}"

Note: Facts set with this module are available to all subsequent tasks in the playbook and can be used in templates and conditions.

Module Capabilities

  • Has Revert: false
  • Provides Variables: None

Inputs

No input parameters.

Outputs

No outputs.