What is Ansible Set Fact?& how its works?

What is Ansible Set Fact?& how its works?
Techiio-author
Written by Nilima PaulFebruary 2, 2022
12 min read
Ansible
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

In this blog post, We will discuss Ansible Set Fact.

Introduction to Ansible Set Fact

In Ansible, we have different modules that work with factors and are utilized to get or set variable qualities. One such significant module is set_fact. This module is utilized to set new factors. These factors are set on a host-by-have premise which is very much like ansible realities, found by the arrangement module. These factors are accessible to ensuing plays in a playbook.

These factors are set with priority however according to the standard ansible priority, numerous different sorts of factors have a higher need. So this worth might be superseded. Similar conduct and elements are accessible while involving this module in Microsoft Windows.

What is Ansible Set_Fact?

Ansible set_fact is unique about vars, vars_file, or include_var where you know the variable worth in advance, though while utilizing set_fact, we can store the worth after setting it up on the fly utilizing specific undertaking like utilizing channels or taking subpart of another variable. When utilized with boundary cacheable equivalent to indeed, then, at that point, the variable can be utilized across executions utilizing a reality store.

The variable task under set_fact is finished by utilizing key-pair esteems where the key is the variable name and worth is the task to it. A straightforward model will resemble underneath: -

- set_fact:
one_fact: value1
second_fact:
value2

How Does Ansible Set_Fact Work?

The work conduct of Ansible set_fact is equivalent to Ansible vars. Yet, which makes it more pertinent for dynamic conditions is the adaptability to store information in the wake of handling. We have underneath accessible boundaries and adequate worth. The work conduct of Ansible set_fact is equivalent to Ansible vars. Yet, which makes it more pertinent for dynamic conditions is the adaptability to store information in the wake of handling. We have underneath accessible boundaries and adequate worth.

  • cacheable: This acknowledges esteem either 'yes' or 'no'. Whenever set to indeed, then, at that point, the variable and its worth are put away truth be told reserve and this variable can be utilized across execution. This boundary, when set to 'yes', changes over the variable into an established truth Which will be added to reality reserve if reality storage is empowered. Of course, this is set to 'no'
  • key-esteem: This module takes the key=value sets as factors, to set in the playbook.

Likewise underneath are a few focuses, which we should note before utilizing set_fact in useful models.

  • The key=value pair just makes Booleans or strings. To make records/exhibits or word references, we will utilize like beneath.
Var: [var1, var2]

  • We can use ansible filters to create values.
  • We can use ansible conditions using when to set a variable based on.

Examples of Ansible Set Fact

Presently by utilizing models, we will attempt to find out with regards to Ansible set_fact, which you may need to use in everyday tasks. We will take a few models, however, priobeforeng there, we initially comprehend our lab, which we utilized for testing reasons.

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.

blogpost

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 the set_fact module.

blogpost

Example #3

In this example, we want to filter a list, so we used a select filter, which is used with a keyword match. Other filters like the list can be used along with it. Then assign the result value to a variable and print the 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
blogpost

Example #4

In this model we will make two playbooks, wherein one playbook we use set_fact for allocating worth to a variable and utilize cacheable: yes choice to make this variable usable across executions. Then, at that point, we import this playbook in the subsequent playbook and utilize that variable from the principal playbook in the subsequent playbook. The principal playbook resembles underneath: -

---
- 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 underneath yield, where we can see that first when the investigate module attempted to print the variable var1, it gave a blunder, however, when we imported the primary playbook wherein variable was set utilizing set_fact and made usable across executions by referencing cacheable: indeed, then, at that point, on-again utilizing troubleshoot to print the variable worth, we can print it.

blogpost

Conclusion

Ansible set_fact is a valuable instrument. This assists you with social event data from your remote has and adds the adaptability of changing it before relegating it. Likewise, you might recognize that if you have a lot of hosts to be overseen by Ansible, working with set_fact is very perplexing. So we first need to dissect whether or not we want to set realities and afterward utilize these in a like manner.

Ansible
Ansible Set Fact
Python
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Nilima Paul
Technology Security Analyst
Techiio-followerTechiio-followerTechiio-follower
201 Blog Posts
0 Discussion Threads
Trending Technologies
15
Software40
DevOps48
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance