Saturday, 6 December 2025

Ansible 13. Variables - dictionary some real time example

 

REAL-TIME EXAMPLE 1: Using a dictionary named user

You store user details inside a structured dictionary:

--- - name: Create Linux user using dictionary hosts: db_servers vars: user: name: oracle uid: 54321 shell: /bin/bash tasks: - name: Create the user ansible.builtin.user: name: "{{ user.name }}" uid: "{{ user.uid }}" shell: "{{ user.shell }}" - name: Show details debug: msg: "Created user {{ user.name }} with UID {{ user.uid }}"

📌 When to use?

✔ When the data belongs to one object
✔ When you may later have many such objects (user, db, app, etc.)


REAL-TIME EXAMPLE 2: Using variables inside vars: (flat dictionary)

You store variables directly at the play level:

--- - name: Create Linux user using play-level vars hosts: db_servers vars: name: oracle uid: 54321 shell: /bin/bash tasks: - name: Create the user ansible.builtin.user: name: "{{ name }}" uid: "{{ uid }}" shell: "{{ shell }}" - name: Show details debug: msg: "Created user {{ name }} with UID {{ uid }}"

📌 When to use?

✔ When you have only one set of values
✔ When no grouping is needed



✅ REAL-TIME USE CASE: Dictionary of Dictionaries (Nested Dictionary)

🟦 Use Case: Managing multiple Linux users with different attributes

--- - name: Create multiple Linux users hosts: db_servers vars: users: oracle: uid: 54321 shell: /bin/bash groups: dba grid: uid: 54322 shell: /bin/bash groups: oinstall appuser: uid: 60001 shell: /bin/sh groups: appgrp tasks: - name: Create all users ansible.builtin.user: name: "{{ item.key }}" uid: "{{ item.value.uid }}" shell: "{{ item.value.shell }}" groups: "{{ item.value.groups }}" loop: "{{ users | dict2items }}"

✔ Why this is used?

  • Perfect for many items identified by names (oracle, grid, appuser)

  • Each item has its own dictionary

  • Clean structure

  • Easy looping with dict2items

dict2items converts a dictionary into a list of items, where each item has:

🟦 Example: What dict2items Produces

When you apply:

users | dict2items

The output becomes:

[ { "key": "oracle", "value": { "uid": 54321, "shell": "/bin/bash", "groups": "dba" } }, { "key": "grid", "value": { "uid": 54322, "shell": "/bin/bash", "groups": "oinstall" } }, // ... and so on for 'appuser'
].

🟦 Meaning of item.key and item.value

During each loop iteration, item looks like this:

Iteration 1

item.key = oracle item.value = { uid: 54321, shell: /bin/bash, groups: dba }

Iteration 2

item.key = grid item.value = { uid: 54322, shell: /bin/bash, groups: oinstall }

etc.

✅  REAL-TIME USE CASE: List of Dictionaries (Array of Objects)

🟦 Use Case: Create multiple tablespaces in Oracle DB

--- - name: Create multiple Oracle tablespaces hosts: db_servers vars: tablespaces: - name: USERS size: 5G autoextend: on - name: TEMP size: 3G autoextend: off - name: DATA size: 10G autoextend: on tasks: - name: Create tablespaces using SQL shell: | sqlplus / as sysdba <<EOF CREATE TABLESPACE {{ item.name }} DATAFILE SIZE {{ item.size }} AUTOEXTEND {{ item.autoextend }}; EOF loop: "{{ tablespaces }}"

✔ Why this is used?

  • Perfect for ordered data (step 1, step 2, step 3)

  • Best format for looping tasks

  • Each list item is an object (a dictionary)

Here tablespaces is a List of Dictionaries

Each item in the list is a dictionary:

Example:

1st dictionary:

{name: USERS, size: 5G, autoextend: on}

2nd dictionary:

{name: TEMP, size: 3G, autoextend: off}

3rd dictionary:

{name: DATA, size: 10G, autoextend: on}

So Ansible sees this as a list with 3 objects.



✅  REAL-TIME USE CASE: Dictionary with List of Dictionaries

🟦 Use Case: Application deployment with environments + services

--- - name: Deploy application stack hosts: app_servers vars: app_config: env: production version: 4.2 services: - name: web port: 8080 state: started - name: api port: 9000 state: started - name: background_worker port: none state: stopped tasks: - name: Start/stop services ansible.builtin.service: name: "{{ item.name }}" state: "{{ item.state }}" loop: "{{ app_config.services }}" - name: Print deployment info debug: msg: "Deployed v{{ app_config.version }} to {{ app_config.env }}"

✔ Why this is used?

  • The root dictionary contains metadata (env, version)

  • Inside it, services is a list of dictionaries

  • A common pattern in complex systems and DevOps workflows


No comments:

Post a Comment

Building a Safer PostgreSQL CI/CD Pipeline with GitHub Actions: Dev → PR Review → Test Promotion

In my previous post, we explored a simple push-to-main deployment strategy . While functional, that model is not considered an industry best...