Introduction to Ansible Set Fact
In Ansible, we have various modules that work with variables and are used to get or set variable values. One such important module is set_fact. This module is used to set new variables. These variables are set on a host-by-host basis which is just like ansible facts, discovered by setup module. These variables are available to subsequent plays in a playbook.
These variables are set with precedence but as per the standard ansible precedence, many other types of variables have a higher priority. So this value may be overridden. The same behavior and features are available when using this module in Microsoft Windows.
What is Ansible Set_Fact?
Ansible set_fact is different from vars, vars_file, or include_var where you know the variable value beforehand, whereas when using set_fact, we can store the value after preparing it on the fly using certain task like using filters or taking subpart of another variable. When used with parameter cacheable equal to yes, then the variable can be used across executions using a fact cache.
The variable assignment under set_fact is done by using key-pair values where the key is the variable name and value is the assignment to it. A simple example will be like below: –
- set_fact:
one_fact: value1
second_fact:
value2
How Does Ansible Set_Fact Work?
The work behavior of Ansible set_fact is the same as Ansible vars. But which makes it more relevant for dynamic environments is the flexibility to store data after processing. We have below available parameters and acceptable value.
- cacheable: This accepts value either ‘yes’ or ‘no’. When set to yes, then the variable and its value are stored in fact cache and this variable can be used across execution. This parameter, when set to ‘yes’, converts the variable into an actual ‘fact’ Which will be added to fact cache, if fact caching is enabled. By default, this is set to ‘no’
- key-value: This module takes the key=value pairs as variables, to set in the playbook.
Also below are some points, which we should note before using set_fact in practical examples.
- The key=value pair only creates Booleans or strings. If you want to create lists/arrays or dictionaries, we shall use like below.
Var: [var1, var2]
- We can use ansible filters to create values for.
- We can use ansible conditions using when to set a variable based on.
Examples of Ansible Set Fact
Now by using examples, we will try to learn about Ansible set_fact, which you might have to use in day to day operations. We will take some examples, but before going there, we first understand our lab, we used for testing purpose.
Here we have an Ansible control server named ansible-controller and two remotes hosts named host- one and host-two. We will create playbooks and run Ansible commands on the ansible-controller node and see the results on remote hosts.
Example #1
In this example, we will create a playbook like below where we use set_fact for assigning value to a variable. Then use the same variable to create a file on the target.
---
- hosts:
all tasks:
- name: Here we are using set_fact
module. set_fact:
file_name: sample.txt
- name: Here we are using file
module file:
path: /tmp/{{ file_name
}} state: touch
After executing it with verbose like below:
ansible-playbook ansible_set_fact.yaml -v
We get the below output, where we can see that the files are created with the value of the variable on target nodes.
Example #2
In this example, we will create a playbook like below where we use set_fact for assigning values to variables. Then use the same variables to create files on target nodes.
---
- hosts: all tasks:
- name: Here we are using set_fact to set variables set_fact:
file1: test1.txt file2: test2.ini file3: test3.txt
- name: Here we use file module to create files with variable values as names file:
path: /tmp/{{ item }} state: touch with_items:
- "{{ file1 }}"
- "{{ file2 }}"
- "{{ file3 }}"
After executing it like below:
ansible-playbook ansible_set_facts.yaml -v
We get the below output, where we can see that multiple files got created and the file’s name is the same as the value of the variable which we set under set_fact module.
Example #3
In this example, we want to filter a list, so we used a select filter, this is used with a keyword match. Other filters like list can be used along with it. Then assign result value to a variable and print variable value. We can make a playbook with content like below: –
---
- hosts: all tasks:
- set_fact:
interfaces_list: "{{ ansible_interfaces | select('match', '^(eth|wlan)[0-9]+') | list
}}"
- debug:
msg: The interface's list on "{{ ansible_hostname }}" is "{{ interfaces_list }}"
The output will be like below:
ansible-playbook filter_select_match.yaml
Example #4
In this example we will create two playbooks, wherein one playbook we use set_fact for assigning value to a variable and use cacheable: yes option to make this variable usable across executions. Then we import this playbook in the second playbook and use that variable from the first playbook in the second playbook. The first playbook is like below: –
---
- hosts: localhost tasks:
- name: here we are using set_fact to set a variable with cacheable parameter set_fact:
var1: apple cacheable: yes
The second playbook, where the first playbook is imported, is like below:
---
- hosts: localhost gather_facts: no tasks:
- name: here we check the variables. debug:
var: "{{ var1 }}" ignore_errors: yes
- name: here we are including another playbook import_playbook: var_task.yaml
- hosts: localhost
gather_facts: no tasks:
- name: here we check the variables again. debug:
msg: variable value is "{{ var1 }}"
After executing it like below:
ansible-playbook ansible_set_fact_cacheable.yaml
We get the below output, where we can see that first when the debug module tried to print the variable var1, it gave an error, but when we imported the first playbook in which variable was set using set_fact and made usable across executions by mentioning cacheable: yes, then on again using debug to print the variable value, we can print it.
Conclusion
Ansible set_fact is a useful tool. This helps you to gather information from your remote hosts and adds the flexibility of modifying it before assigning it. Also, you may acknowledge that if you have a lot of hosts to be managed by Ansible, then working with set_fact is quite complex. So we first need to analyze that whether we need to set facts or not and then use these accordingly.
Recommended Articles
This is a guide to Ansible Set Fact. Here we also discuss the Introduction and how does ansible set fact work along with different examples and its code implementation. You may also have a look at the following articles to learn more –