While you can use vars_file, vars, or include_var to set up variables in your playbooks, the set_fact module is one of the best ways to do so, primarily because of its flexibility.
This tutorial will explain what ansible set_fact is, how it works, and how you can use it.
Understanding Ansible Set_fact
Unlike other modules that allow you to set ansible variables—think vars_file, vars, or include_var—where you need to know the variable values beforehand, the Ansible set_facts module enables you to set variables off-the-cuff as required, often on a host-to-host case.
Variables set using set_facts are available for playbook execution within the same play. However, using the cachable parameter set to yes, you can turn these variables into facts in the fact cache, making them executable across playbook runs—with the “cached fact” precedence.
Ansible set_fact parameters
Ansible set_fact supports the following parameters:
- Cacheable: This boolean parameter has two options: yes and no. This parameter turns a set_fact variable into a “fact” stored in the fact cache, but only if you have fact caching enabled.
- Key_value: Variables set using the set_fact module use the “key=value“ —or “key: value“ for YAML — string pairs, where the key is the variable name and value defines the variable’s value.
The following are some essential features of the set_fact module.
- The key=value—or key: value parameter for YAML—only creates Booleans and strings, but you can use var: [val20, val30] to create dictionaries or arrays.
- Set_fact creates static variable values
- Variables set using set_fact follow Standard Ansible variable precedence rules; thus, other variable types with a higher priority may override the valuable value set using set_fact
- You can’t use ‘cacheable’ as a valid fact name because Ansible version 2.4 and later have it as a module parameter
- Set-fact variables are host-to-host based and are available for subsequent Ansible playbook runs
- The set_fact module also supports Windows targets
Ansible set_fact example
Let us look at a few examples to illustrate using the set_fact.
In the example below, we use the set_fact to assign a specific value to a variable and create a user.
- hosts: all
become: true
tasks:
- name: create user.
set_fact:
username: linuxhint
user:
name: "{{username}}"
group: "{{username}}"
shell: /bin/bash
In the example above, we use the set_fact module to create a user on a remote host.
Another example is to use the set_fact with conditionals. For example:
- hosts: all
become: true
tasks:
- name: apache
set_fact:
pkg: "apache2"
when: ansible_distribution == "Debian" or ansible_distribution == "Ubuntu"
- name: httpd
set_fact:
pkg: "httpd"
when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux'
In the example playbook above, we set the package to apache2 when the distribution is Debian or Ubuntu and httpd for CentOS and REHL.
Closing
The Ansible set_fact module allows you to add flexibility to your playbooks based on the information gathered from the host.
Thank you for reading!