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 Dynamic Data Construction: Build Variables at Runtime (Guide)

By Luca Berton · Published 2024-01-01 · Category: installation

How to dynamically construct data structures in Ansible. Build lists, dictionaries, and complex data at runtime using set_fact, combine, and Jinja2 expressions.

Dynamically constructing and managing data structures is a crucial skill in Ansible automation, especially for tasks that require flexible and reusable configurations. In this article, we’ll explore this concept using a practical example: managing user accounts and groups on Linux systems.

Scenario: Dynamic User and Group Management

Imagine you have a user_list variable defining multiple users and their associated groups:

Your goal is to: • Dynamically construct a user and group data structure. • Use this structure to manage user accounts and assign them to the appropriate groups.

The Ansible Playbook

Here’s how you can achieve this using set_fact and Jinja2 templating:

Key Components Data Construction with set_fact: • The set_fact task dynamically builds user_data as a list of dictionaries, each containing: • name: The username. • groups: The list of groups.

Example output: Dynamic User Management: • The ansible.builtin.user module iterates through user_data, creating users and assigning them to their respective groups. The groups field is converted to a comma-separated string using join(','). Debugging: • The debug task ensures that the user_data structure is correct before applying it.

Benefits of This ApproachDynamic and Reusable: • Adapts to changes in the input user_list without modifying the playbook logic. • Centralized Logic: • Data construction is isolated in the set_fact task, making the playbook easier to read and maintain. • Scalable: • Handles any number of users and groups effortlessly. • Error Detection: • Intermediate debugging ensures correctness before execution.

Conclusion

This approach demonstrates how to dynamically manage data in Ansible using Jinja2 templates and set_fact. Whether you're managing users, configuring networks, or handling other complex automation tasks, the principles here can be applied broadly to improve scalability and maintainability.

Ready to master more Ansible automation techniques? Dive into the world of flexible and efficient configuration management today!

Build Lists Dynamically

Accumulate in a loop

Conditional list building

From command output

Build Dictionaries Dynamically

Combine in a loop

Merge defaults with overrides

From structured data

Complex Nested Structures

Filter Transformations

JSON Query (JMESPath)

Template-Based Construction

FAQ

Why does my list reset in each loop iteration?

Use default([]) to initialize:

How do I remove duplicates?

Can I build data across plays?

Use cacheable: true with set_fact, or write to a file and read it back.

Related ArticlesAnsible Template GuideAnsible set_fact GuideAnsible Loops Guide

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home