Ansible variable types and methods of variable definition in Ansible are the subject of this video. We have already used variables in our Playbooks. For example, we have defined the folder name in which the backup files were stored as a variable or the credentials for connecting to devices also as variables in our backup playbooks. In this section we would like to get to know different variable types and how they are defined in ansible.

access ansible codes of this course in github

this is a video-based training therefore the output of running commands are not show in the text.

We can double-check the contents of backup playbooks to remember how we have already defined and used variables in those playbooks.

In Ansible, as in many other programming languages, we can define different types of variables such as integers and strings. but the types of variables that are most commonly used in Ansible, in addition to these two types, are List and Dictionary. With list you can define a list of something like IP addresses, host names etc. With Dictionary you can define a variable as a key-value pair.

In order to better understand different types of variables, I have prepared a Playbook with variables of different types. Then I created a task to print these variables or part of them. I’ve defined these variables using the “vars” keyword like we’ve done in previous playbooks.

root@debian:~/ansible-project1# cat vars.yaml
---
- hosts: csr1
  gather_facts: false
  connection: network_cli
  vars_files:
    - "./vars/credential.yml"
    - "./vars/blacklist.yml"
  vars:
    var_int: 4
    var_str: "a string text"
    var_list1: [1, 3, 5, 7, 9, 11]
    var_list2:
      - city1
      - city3
      - city5
      - city7
      - city9
    var_dic1:
      name: Majid
      family: Asadpoor
      age: 43

  tasks:
    - name: include vars
      include_vars: var1.yml

    - name: print vars
      debug:
        msg:
          - "var_int * 2: {{var_int*2}}"
          - "var_str: {{var_str}}"
          - "var_list1: {{var_list1}}"
          - "var_list2: {{ item }}"
          - "var_list2[1]: {{var_list2[1]}}"
          - "var_dic1: {{var_dic1}}"
          - "var_dic1.name: {{var_dic1.name}}"

          - "I like {{pizza_name}} pizza in {{pizza_shop_name}}"

          - "username:{{username}} , password: {{password}}"
          - "list of IP Addresses in black list: {{blacklist_ip}}"

          - "domain: {{domain}}"

          - "hostname: {{hostname}}"

      loop:
        - "{{var_list2}}"

The first variable is an integer with the value 4. To make sure it’s an integer value, I’ll print the multiplied 2 of the variable. To read a variable, we use two curly braces.

The second variable is a string variable that is printed in the task by being called within two curly braces.

Then I defined two list variables to show that there are two methods of defining list variables, which you can see here. I printed the first list by just calling the name of the variable. The second list is printed with a loop definition and then calling every item of the loop. This way we can manipulate every element of a list. We’ll learn the loop definition in a separate video. Then I print the second component from List2. This is to show you how we can access a particular component of a list.

The last variable is a dictionary. In the task , dictionary variable is printed with just calling it with two curly braces. Then I print the value of a specific key in the dictionary this is to show you how we can access to the value of a specific key in dictionary.

Using Variables in Ansible in ansible website

Ansible include_vars in Ansible Website

Using “vars” keyword in not the only method of defining variables in ansible. There are more than 20 methods to define variables in ansible with different priorities. Here I would like to introduce some of these methods that are more practical.

One method is to define your variables in different files in the format of key-value pairs and then look up the names of the files as a list in the “vars_files” keyword in the header of play in the playbook.

For example, I’ve defined two YAML files named Credential and Blacklist here to store the user credentials that will be used to connect to the devices, as well as the list of IP addresses that are blacklisted. Then in Task, I have printed the value of variables just with calling the key of the variable.

Using the “include_vars” module is another way of defining variables that is very flexible and extensive. For example, you can define variables based on a condition like the type of operating system, or you can define a folder name to define multiple variable files at once so that you don’t have to write the name of all files and many other features I’m not talking about here.

Here I have defined a new task with the “include_vars” module to call a file name that defines all the variables like “vars_files” that I introduced a few minutes ago.

Another method and one of the most practical method of defining variables is to create the “group_vars” and “host_vars” folders in an ansible project folder. The group level variables defined in the “group_vars” folder and host level variables are defined in the “host_vars” folder.

root@debian:~/ansible-project1# ls ./host_vars/
csr1       .csr1.swp  csr2

root@debian:~/ansible-project1# ls ./group_vars/
all  csr

To define a variable at the group level, we create a file with the name of the group defined in the inventory file in “group_vars” folder. For example, we have a group named “csr” in our inventory and so we have defined a file named “csr” in “group_vars” folder and variables that are common to all hosts within this group are defined here. I have defined domain variablewith the value of “rayka-co.com”

root@debian:~/ansible-project1# cat ./group_vars/csr
---
as: 1
domain: rayka-co.com

To define a variable at the host level, we create a file with the name of every host defined in the inventory file in “host_vars” folder. For example, we have two hosts named “csr1” and “csr2” in our inventory file and so we define two file named “csr1” and “csr2”in “host_vars” folder and variables specific to every host are defined in their own file. I have defined hostname variable with their own value.

root@debian:~/ansible-project1# cat ./host_vars/csr2
---
hostname: csr2

root@debian:~/ansible-project1# cat ./host_vars/csr1
---
hostname: csr1

 

Back to: Ansible for Network Engineers > Ansible Progamming Features

Leave a Reply

Your email address will not be published. Required fields are marked *


Post comment