Read this in other languages:
English, 日本語, Portugues do Brasil, Française, Español.
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.
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.
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.
The creation and usage of variables involve a specific syntax:
vars
section of a playbook or in separate files for larger projects."{{ 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
.
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
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:
register: user_check:
This captures the output of the id command into the variable user_check.when: user_check.rc == 0:
This line is a conditional statement. It checks if the return code (rc) of the previous task (stored in user_check) is 0, indicating success. The debug message will only be displayed if this condition is met.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
Navigation
Previous Exercise - Next Exercise
Click here to return to the Ansible for Red Hat Enterprise Linux Workshop