Workshop Exercise - Using Variables

Read this in other languages:
uk English, japan日本語, brazil Portugues do Brasil, france Française,Español Español.

Table of Contents

Objective

Extending our playbooks from Exercise 1.3, the focus turns to the creation and usage of variables in Ansible. You’ll learn the syntax for defining and using variables, an essential skill for creating dynamic and adaptable playbooks.

Guide

Variables in Ansible are powerful tools for making your playbooks flexible and reusable. They allow you to store and reuse values, making your playbooks more dynamic and adaptable.

Step 1 - Understanding Variables

A variable in Ansible is a named representation of some data. Variables can contain simple values like strings and numbers, or more complex data like lists and dictionaries.

Step 2 - Variable Syntax and Creation

The creation and usage of variables involve a specific syntax:

  1. Defining Variables: Variables are defined in the vars section of a playbook or in separate files for larger projects.
  2. Variable Naming: Variable names should be descriptive and adhere to rules such as:
    • Starting with a letter or underscore.
    • Containing only letters, numbers, and underscores.
  3. Using Variables: Variables are referenced in tasks using the double curly braces in quotes "{{ variable_name }}" . This syntax tells Ansible to replace it with the variable’s value at runtime.

Update the system_setup.yml playbook to include and use a variable:

---
- name: Basic System Setup
  hosts: node1
  become: true
  vars:
    user_name: 'Roger'
  tasks:
    - name: Update all security-related packages
      ansible.builtin.dnf:
        name: '*'
        state: latest
        security: true

    - name: Create a new user
      ansible.builtin.user:
        name: "{{ user_name }}"
        state: present
        create_home: true

Run this playbook with ansible-navigator.

Step 3 - Running the Modified Playbook

Execute the updated playbook:

[student@ansible-1 lab_inventory]$ ansible-navigator run system_setup.yml -m stdout
PLAY [Basic System Setup] ******************************************************

TASK [Gathering Facts] *********************************************************
ok: [node1]

TASK [Update all security-related packages] ************************************
ok: [node1]

TASK [Create a new user] *******************************************************
changed: [node1]

PLAY RECAP *********************************************************************
node1                      : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Notice how the updated playbook shows a status of changed in the Create a new user task. The user, ‘Roger’, specified within the vars section has been created.

Verify the user creation via:

[student@ansible-1 lab_inventory]$ ssh node1 id Roger

Step 4 - Advanced Variable Usage in Checks Playbook

Enhance the system_checks.yml playbook to check for the ‘Roger’ user within the system using the register variable and when conditional statement.

The register keyword in Ansible is used to capture the output of a task and save it as a variable.

Update the system_checks.yml playbook:

---
- name: System Configuration Checks
  hosts: node1
  become: true
  vars:
    user_name: 'Roger'
  tasks:
    - name: Check user existence
      ansible.builtin.command:
        cmd: "id {{ user_name }}"
      register: user_check

    - name: Report user status
      ansible.builtin.debug:
        msg: "User {{ user_name }} exists."
      when: user_check.rc == 0

Playbook Details:

This setup provides a practical example of how variables can be used to control the flow of tasks based on the outcomes of previous steps.

Run the checks playbook:

[student@ansible-1 lab_inventory]$ ansible-navigator run system_checks.yml -m stdout

Output:

PLAY [System Configuration Checks] *********************************************

TASK [Gathering Facts] *********************************************************
ok: [node1]

TASK [Check user existence] ****************************************************
changed: [node1]

TASK [Report user status] ******************************************************
ok: [node1] => {
    "msg": "User Roger exists."
}

PLAY RECAP *********************************************************************
node1                      : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Review the output to confirm the user existence check is correctly using the variable and conditional logic.

Navigation

Previous Exercise - Next Exercise



Click here to return to the Ansible for Red Hat Enterprise Linux Workshop