Mainframe Path Start learning free
Beginner7 min readLesson 4 of 5

Run it twice: idempotence, roles and Vault

Well-written Ansible tasks are idempotent: running them a second time changes nothing, because the system is already in the described state. Roles package related tasks, templates and defaults for reuse, collections distribute modules and roles, and Ansible Vault encrypts secrets kept beside the playbooks.

Idempotence

A task is idempotent when running it again gives the same result without doing anything new. 'Make sure nginx is installed' installs it the first time and does nothing the second time. That is what makes it safe to rerun a playbook after a half-finished run, or every night to correct drift.

The same playbook, run a second time
PLAY RECAP *********************************************************
web01.example.com : ok=4  changed=0  unreachable=0  failed=0  skipped=0  rescued=0  ignored=0

changed=0 on a second run is the sign of a well-written playbook. If a task reports 'changed' on every run, it is probably a command or shell task. Make it honest with creates: (skip the task if a file already exists) or changed_when: (say what counts as a change), or replace it with a proper module.

Making a command task idempotent
- name: Unpack the release once
  ansible.builtin.command:
    cmd: tar -xzf /tmp/payroll-1.4.2.tar.gz -C /opt/payroll
    creates: /opt/payroll/VERSION

Roles: reusable building blocks

A role packages everything for one job, such as 'nginx' or 'payroll app', in a standard folder layout, so that many playbooks can reuse it. A playbook then lists roles instead of long task lists.

Folder in the roleHolds
tasks/main.ymlThe role's tasks
handlers/main.ymlIts handlers
defaults/main.ymlDefault variables, the easiest to override
templates/, files/Jinja2 templates and plain files to copy
meta/main.ymlInformation about the role and its dependencies

Collections and Galaxy

Modules, roles and plugins are distributed as collections, named namespace.collection, such as ansible.builtin, community.general or ibm.ibm_zos_core. You install them with ansible-galaxy collection install, from Ansible Galaxy or, for certified content, from Red Hat's Automation Hub.

Secrets with Ansible Vault

Playbooks often need passwords. Ansible Vault encrypts a variables file so it can be kept in Git safely: ansible-vault encrypt group_vars/prod/secrets.yml. At run time you supply the vault password, for example with --ask-vault-pass, or a pipeline provides it from its credentials store. Never commit secrets in plain text.

TRY IT YOURSELF

What word describes a task that changes nothing when it is run a second time?

Show a hint

It starts with idem-, Latin for 'the same'.

Show the solution

Idempotent.

Examples are for learning. Run commands and jobs only on a system you are authorised to use, such as a training or test system, and never on production without approval.

Common mistakes

Shell tasks that always report changed

They hide real changes in the noise and trigger handlers needlessly. Use modules, or creates: and changed_when:.

Copying the same tasks into every playbook

Copies drift apart. Put shared steps in a role and reuse it.

Plain-text passwords in group_vars

Anyone with access to the repository can read them. Encrypt them with Vault or fetch them from a secrets manager.

What you will see at work

Key terms

Check your understanding.
Take this lesson's quiz and save your progress. Free.

Take the lesson quiz
← Writing and running a playbookAnsible for z/OS →