How to Use Variables in Ansible Playbook (Simple Guide)

In this blog post, we will show you how to use variables in Ansible playbook.

If you have done programming or written code before, then the concept of variables should ring a bell. Just like in any programming language, in Ansible a variable is a placeholder that holds a value that can, thereafter, be referenced in a playbook YAML file or on the command line.

Rules that govern the naming of variables

In Ansible, the following rules apply when naming variables:

  • A variable name should begin with either a lowercase or uppercase character. It cannot start with an underscore, space, digit, or special character.
  • A variable name should only contain alphanumeric characters, underscores, and integers.
  • Some reserved keywords cannot be assigned to a variable. These include Python keywords and Ansible playbook parameters.

There are different ways of using Ansible variables in Playbooks. Let’s dive in.

Use Variables in a Ansible playbook

At the most basic level, you can define a variable name using a single value inside a playbook using the “vars” parameter.  Here’s a simple playbook example.—

- hosts: server1
    salute: Hey guys!
  - name: Ansible Simple Variable Example Usage
      msg: "{{ salute }}. Welcome to Linuxtechi."

In this YAML file, the ‘vars’ block starts off the variable definition. The variable ‘salute’ stores the string value ‘Hey guys’. The variable is then called and accessed using the ‘msg’ module on the last line.

During playbook runtime, the variable ‘salute’ is called and the value is printed to stdout.


List and Dictionary Variables

Variables can also take advanced forms such as lists and dictionaries. You can create a variable with multiple values using the following list syntax.

       - item
       - item
       - item

For example,  here’s a playbook containing a list of three students:

- hosts: server1
       - Alice
       - Mike
       - Sophie
  - name: Ansible lists Variable Example Usage
      msg: " Our students are {{ students }}."

When the playbook is executed, all the student names which, essentially list items, are printed out to the terminal.


In addition, you can reference a specific list item using square brackets. For example, {{ students[1] }} references the second list item. In the following example, the student ‘Mike ’is printed out.


Variable items can also be referenced as dictionaries using key-value pairs as shown.

       - key1: value 1
       - key2: value2

In the following playbook example, the student dictionary is formatted in key-value pairs.

- hosts: server1
       - student_1: Alice
       - student_2: Mike
       - student_3: Sophie
  - name: Ansible dictionary  Variable Example Usage
      msg: " Our students are {{ students }}."


Just like lists, you can reference specific items inside a dictionary using square brackets.  For example, {{ students[1] }} references the second key-value pair item in the dictionary.


Special Variables

In Ansible, special variables are reserved variables that contain information about the inventory, system, or information about the playbook. These are predefined variables and cannot be explicitly set or defined by a user.

Special variables include magic variables, ansible facts, and connection variables.

Magic Variables

Magic variables cannot the set or defined by a user and always represent the internal state of the host system or Ansible. The playbook below prints out the Ansible version defined by the variable name {{ ansible_version }} and the hostname of the system as defined in the Ansible inventory using  {{ ansible_version }}.

-  hosts: Server1
    - name: Ansible Magic variable example
          - "You are running Ansible version {{ ansible_version }} "
          - "This is running on {{ inventory_hostname }}"


Ansible Facts

Ansible facts are special variables that provide information about the system and hardware information of the host during playbook execution. The data obtained is mostly used for making decisions when you want to execute a specific host task. Such information includes the IP address, operating system, system architecture, etc.

To collect facts about a specific host, you can run the following command:

$ ansible -m setup <hostname>

The command prints out the facts in JSON format.


You can filter specific facts using the following syntax:

$ ansible -m setup <hostname> -a "filter=<fact_name>"

In the following Ansible command, we are displaying OS information about the remote host using the ansible_lsb fact


Connection Variables

Connection variables spell out how the Ansible control node connects to remote hosts during playbook execution. They provide flexibility in handling various types of connections.

Consider the playbook file below.

- hosts: server1
  remote_user: root
  become: true
  become_method: sudo
    - name: Install Apache webserver
        name: apache2
        state: latest

The become and  become_method connection variables turn on privilege escalation and allow you to run the playbook as a root user on the remote host. During playbook runtime, the –ask-become-pass parameter prompts for the root user’s password.


That’s all from this post, I believe you have found it informative and useful. Feel free to post your queries and feedback in below comments section.

Also Read: How to Execute Ansible Ad-Hoc Commands

Leave a Comment

nineteen − six =