AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible set_fact Module: Create Dynamic Variables at Runtime (Guide)

By Luca Berton · Published 2024-01-01 · Category: collections-galaxy

How to create variables dynamically with Ansible set_fact module (ansible.builtin.set_fact). Set facts from task output, conditionals, loops. Practical YAML playbook examples.

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.

Linksansible.builtin.set_fact

Demo

In this article, we will explore the set_fact module and its usage through a practical example. We will Playbooknstrate 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:

Let's break down this playbook step by step to understand its functionality: The name directive gives our playbook a descriptive title, "regex Playbook." The hosts directive specifies the target hosts on which the playbook will be executed. In this case, it's set to all, 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 the centos_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 the centos_repo URL. It retrieves the content of the repository in JSON format and stores it in the available_packages variable using the register directive. The second task, named "Save," utilizes the set_fact module. It takes the content stored in available_packages and applies two filters consecutively. First, it uses the regex_replace filter from the ansible.builtin plugin to remove any HTML tags from the content. Then, it applies the regex_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 the kernel variable using the ansible.builtin.set_fact module. The third task, named "Print," uses Ansible's debug module to display the value of the kernel 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.

Conclusion

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.

Related ArticlesAnsible Inventory GuideAnsible set_fact Guide

Category: collections-galaxy

Browse all Ansible tutorials · AnsiblePilot Home