Skip to main content

Variables, Conditionals, and Loops

Updated Dec 17, 2020 ·

Overview

Variables make playbooks reusable. Conditionals decide whether a task should run. Loops repeat a task for multiple values.

Variables

Variables can be defined in several places.

vars:
package_name: httpd
service_name: httpd

Use variables with Jinja2 syntax.

- name: Install package
ansible.builtin.yum:
name: "{{ package_name }}"
state: present

Variable Locations

LocationPurpose
varsDefines values inside a play.
vars_filesLoads variables from a separate file.
group_varsApplies variables to an inventory group.
host_varsApplies variables to one host.
registerStores task output for later tasks.

Registered Variables

Register command output, then use it later.

- name: Check uptime
ansible.builtin.command: uptime
register: uptime_result

- name: Show uptime
ansible.builtin.debug:
var: uptime_result.stdout

Conditionals

Use when to run a task only when a condition is true.

- name: Install Apache on Red Hat hosts
ansible.builtin.yum:
name: httpd
state: present
when: ansible_facts['os_family'] == "RedHat"

Loops

Use loop to repeat a task.

- name: Create users
ansible.builtin.user:
name: "{{ item }}"
state: present
loop:
- alice
- bob
- carol

Loops with Dictionaries

- name: Create users with groups
ansible.builtin.user:
name: "{{ item.name }}"
groups: "{{ item.groups }}"
state: present
loop:
- name: alice
groups: wheel
- name: bob
groups: developers

Note: Use clear variable names. A readable playbook is easier to troubleshoot than a clever one.