Skip to Content

Quick Guide to Use set_fact in Ansible

The set_fact module in Ansible is a vital tool used in playbooks to define or alter the value of variables during playbook execution.

This dynamic nature of set_fact allows for greater flexibility and adaptability in managing configurations and automating tasks.

With set_fact, you can create new variables on the fly based on the output of previous tasks, the results of conditional tests, or any complex logic defined within your playbooks. It’s particularly useful in scenarios where the configuration or behavior of subsequent tasks depends on the results or state of earlier tasks.

For instance, you might use set_fact to store the result of a command, calculate a value, or determine the path of subsequent operations based on specific conditions.

The ability to dynamically set facts adds a powerful layer of decision-making and control to your automation workflows, making Ansible an even more effective and versatile tool for system administration and DevOps.

set_fact Basic Usage

Use set_fact within a task to define a new variable.


- hosts: all
  tasks:
    - name: Set a simple fact
      set_fact:
        my_var: "Hello, World"

    - name: Use the fact
      debug:
        msg: "{{ my_var }}"

Setting Facts Conditionally

Facts can be set based on conditions using the when statement.


- hosts: all
  tasks:
    - name: Set a fact based on condition
      set_fact:
        is_even: true
      when: ansible_facts['distribution_major_version'] | int is even

    - name: Check if the distribution version is even
      debug:
        msg: "The distribution version is even"
      when: is_even

Complex Structures

Create complex data structures like dictionaries or lists using set_fact.


- hosts: all
  tasks:
    - name: Set a complex fact
      set_fact:
        user_info:
          name: "John Doe"
          role: "Admin"

    - name: Display user name
      debug:
        msg: "{{ user_info.name }}"

Persisting Facts across Plays

To make a fact available to all hosts and persist across plays, use set_fact with delegation.


- hosts: all
  tasks:
    - name: Set a fact for all hosts
      set_fact:
        global_var: "Accessible everywhere"
      delegate_to: 127.0.0.1
      run_once: true

set_fact is a versatile tool in Ansible for dynamically creating and modifying variables, adjusting configurations, and managing data structures in playbooks.

More examples of set_fact in Ansible

Setting Multiple Variables:


- name: Set multiple variables
  set_fact:
    variable1: value1
    variable2: value2
    variable3: value3

This example demonstrates setting multiple variables concurrently.

Using Variables in Calculations:


- name: Calculate a value
  set_fact:
    total_servers: "{{ servers | length }}"

Calculates the number of servers and assigns it to total_servers.

Using Filters:


- name: Convert string to uppercase
  set_fact:
    uppercase_name: "{{ server_name | upper }}"

Converts server_name to uppercase.

Using Conditional Statements:


- name: Set variable based on condition
  set_fact:
    is_web_server: "{{ server_type == 'web' }}"

Assigns True or False to is_web_server based on a condition.

Combining with Other Modules:


- name: Set variable based on task output
  set_fact:
    server_version: "{{ lookup('pipe', 'cat /etc/version') }}"

Uses the pipe lookup module to assign the output to server_version.

Remember, set_fact is a powerful tool in Ansible, enhancing the flexibility and automation capabilities of your playbooks.