Mastering Dynamic Variable Creation with set_fact
Unlocking the Secrets of Ansible's Dynamic Arsenal: Amplify Your Playbooks with set_fact's Enchanting Capabilities!


Exploring Ansible’s set_fact
Module with Example
Ansible is an open-source automation tool widely used for configuration management, application deployment, and orchestration. It simplifies the process of managing IT infrastructure by allowing you to define desired state configurations in a declarative manner. One powerful feature of Ansible is the set_fact
module, which enables you to create or modify variables dynamically during playbook execution. It is part of the ansible.builtin
collection.
Links
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
Demo
In this article, we will explore the set_fact
module and its usage through a practical example. We will demonstrate how to retrieve the latest kernel package from a CentOS repository using Ansible’s uri
module and store it in a variable called kernel
. Let’s dive into the code example:
---
- name: Regex demo
hosts: all
vars:
centos_repo: http://mirror.centos.org/centos/7/os/x86_64/Packages/
tasks:
- name: Get Latest Kernel
ansible.builtin.uri:
url: "{{ centos_repo }}"
method: GET
return_content: true
body_format: json
register: available_packages
- name: Save
ansible.builtin.set_fact:
kernel: "{{ available_packages.content | ansible.builtin.regex_replace('<.*?>') | regex_findall('kernel-[0-9].*rpm') }}"
- name: Print
ansible.builtin.debug:
var: kernel
Let’s break down this playbook step by step to understand its functionality:
The
name
directive gives our playbook a descriptive title, “regex demo.”The
hosts
directive specifies the target hosts on which the playbook will be executed. In this case, it’s set toall
, meaning it will apply to all hosts in the inventory.The
vars
section allows us to define variables used within the playbook. Here, we set thecentos_repo
variable to the URL of the CentOS 7 package repository.The
tasks
section contains the actual work to be performed. We have three tasks defined.The first task, named “Get Latest Kernel,” uses Ansible’s
uri
module to send an HTTP GET request to thecentos_repo
URL. It retrieves the content of the repository in JSON format and stores it in theavailable_packages
variable using theregister
directive.The second task, named “Save,” utilizes the
set_fact
module. It takes the content stored inavailable_packages
and applies two filters consecutively. First, it uses theregex_replace
filter from theansible.builtin
plugin to remove any HTML tags from the content. Then, it applies theregex_findall
filter from the same plugin to extract the kernel package names matching the pattern “kernel-[0-9].*rpm.” The resulting list of kernel package names is stored in thekernel
variable using theansible.builtin.set_fact
module.The third task, named “Print,” uses Ansible’s
debug
module to display the value of thekernel
variable.
By leveraging the set_fact
module, we dynamically extract the kernel package names from the repository content and assign them to the kernel
variable. This approach allows us to utilize the extracted data for further operations within our playbook or inventory.
Recap
In summary, the set_fact
module is a powerful tool in Ansible for creating or modifying variables dynamically during playbook execution. It enables us to extract, transform, and store data from various sources, making it available for subsequent tasks or playbooks. In our example, we used set_fact
to extract the latest kernel package name from a CentOS.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and AWX System Administrator and DevOps
Donate
Want to keep this project going? Please donate