Get started with Ansible Playbooks

Discover the basics of creating Ansible playbooks using practical examples, including key components such as plays, tasks, modules, and more. 

Start your Ansible Automation Platform Trial

Now that you have learned more about Ansible’s directory structure and inventories let us have a look at the variables in Ansible and how Ansible facts can be used to retrieve detailed information of managed hosts. 

In this lesson, you will:

  • Learn about variables in Ansible.
  • Learn about Ansible facts.

Variables in Ansible

Variables in Ansible are YAML key-value pairs that store data for reuse in playbooks, making them more flexible and customizable. You can define variables in your playbooks, inventory, variable files, or at the command line.

Review the official Ansible documentation for information on variable precedence

Declaring variables in playbooks

Variables in playbooks are defined under the vars section in a play and can be used in the specific play they’re declared in.

Let’s add variables to our site.yml playbook:

---
- name: Install and start Apache
  hosts: web
  become: true
  vars:
    package_name: httpd
  tasks:
    - name: Ensure the httpd package is installed
      ansible.builtin.package:
        name: "{{ package_name }}"
        state: present

Ansible playbook variable example.

Code

Definition

package_name: httpd

The package_name variable is set to httpd.

name: "{{ package_name }}"

Instead of using httpd in the task, we use the package_name variable using {{ }} Jinja2 brackets.

Using variables in the Inventory

The previous lesson covered adding variables to Ansible inventory groups and hosts. Let’s use an example to illustrate using these variables in a playbook.

---
all:
 children:
   web:
     vars:
       package_name: httpd
     hosts:
       web1:
         ansible_host: 192.168.0.10
       web2:
         ansible_host: web2.example.com
   database:
     hosts:
       db1:
         ansible_user: rhel

Ansible YAML inventory variables example.

Code

Definition

vars:

There is a new vars section in the web inventory group.

package_name: httpd

We’ve created the package_name variable and set its value to httpd.

We can reuse the package_name variable in our playbook:

---
- name: Install and start Apache
  hosts: web
  become: true
  tasks:
    - name: Ensure the httpd package is installed
      ansible.builtin.package:
        name: "{{ package_name }}"
        state: present

Ansible playbook inventory variables example.

Code

Definition

name: "{{ package_name }}"

This playbook will install the httpd package by using the "{{ package_name }}" variable specified in the web inventory group.

Using variable files

Variable files are key-value pairs commonly created in YAML files. Using variable files could make your playbooks easier to reuse and maintain.

# vars.yml
                                        ---
                                        package_name: httpd

Variable file example.

You can include variable files in your playbook using the vars_files section or the ansible.builtin.include_vars module. 

Let’s use an example to include a variable file in a playbook:

---
- name: Install and start Apache
  hosts: web
  become: true
  vars_files:
    - vars.yml
  tasks:
    - name: Ensure the httpd package is installed
      ansible.builtin.package:
        name: "{{ package_name }}"
        state: present

Ansible playbook variable file example.

Code

Definition

 vars_files:

The vars_files section includes the vars.yml variable file in the play level of the playbook.

name: "{{ package_name }}"

The package_name variable created in vars.yml is used in the task.

Using host_vars and group_vars

Ansible automatically loads variable files into playbooks in the host_vars and group_vars folders based on the host or group name and the associated file name. 

For Ansible to automatically associate the host_vars and group_vars folders, they must be in a specific location relative to your Ansible inventory file or playbook file.

The folders can be adjacent to the inventory file:

project-name/
├── inventory/
│   ├── hosts                 # Custom inventory file
│   ├── group_vars/
│   │   └── web.yml           # Variables for 'web group'
│   └── host_vars/
│       └── web1.yml          # Variables for 'web1 host'
└── site.yml

Ansible inventory folder group_vars and host_vars.

Or the folders can be adjacent to the Ansible playbook:

project-name/
├── inventory                 # Inventory file
├── group_vars/
│   └── web.yml               # Variables for 'web' group
├── host_vars/
│   └── web1.yml            # Variables for 'web1' host
└── site.yml

Ansible playbook adjacent folder group_vars and host_vars.

For example, let’s create a web.yml YAML file in the ./project-name/group_vars/ folder:

# ./project-name/group_vars/web.yml
---
package_name: httpd

Ansible inventory group_vars example.  

Code

Definition

package_name: httpd

The package_name variable will automatically be applied to playbooks that use the web inventory group.

Ansible facts

You can retrieve detailed information about managed nodes or about Ansible itself. These data points are called Ansible facts. Facts let you adapt the playbook to the specific characteristics of managed nodes without complex, hard-coded logic.

Unless the gather_facts option is disabled, Ansible gathers facts at the beginning of each play execution and contains information about a host’s filesystem, network interfaces, and more.

Facts are stored in the ansible_facts variable and can be accessed in playbooks.

---
                - name: Install and start Apache
                  hosts: web
                  become: true
                  vars_files:
                    - vars.yml
                  tasks:
                    - name: Filter and return only selected facts
                      ansible.builtin.setup:
                        filter:
                          - 'os_family'
                          - 'ansible_distribution'
                    - name: Ensure the httpd package is installed
                      ansible.builtin.package:
                        name: "{{ package_name }}"
                        state: present
                      when: ansible_facts['os_family'] == "RedHat"
                

Ansible playbook facts example.

Code

Definition

ansible.builtin.setup

You can gather facts by using the ansible.builtin.setup module or running ansible web -m setup from the Control node terminal.

when: ansible_facts['os_family'] == "RedHat"

We are using a conditional statement to install the httpd package only if the os_family fact is RedHatWe will cover conditional statements in the next lesson.

 

Next, we’ll cover more advanced Ansible playbook topics: handlers, conditionals, and Jinja2 templates.

Previous resource
Ansible inventories
Next resource
Advanced Ansible Playbook features