Ansible – Experiment 2: Using variables in Inventory and Playbook file : Host_vars and Group_vars

 

This is my second experiment in the ansible series. You can click here to read the first one

This learning nugget / experiment deals with how to parameterize the hard-coded data within inventory file and playbook.

Why Variables

Most programming languages have mechanism to parametrize the data in the form of variables and then using the variables across the length of the program. Ansible has a neat way of defining the variables too.

Hard coded data in any form becomes difficult to maintain. It is always good to separate the same.

My second experiment deals with separating out the user name and password from the inventory file and keepting the same in a separate file in the form of variables which are consumed by inventory file

Building blocks used

There are three building blocks which I have used in this experiment

  1. Playbook1.yaml file
  2. Inventory.txt file
  3. Group1.yml file (this is the one which has the variables and its values – which are consumed in the inventory file)

Code is as below

 

#playbook1.yaml
# sample playbook to copy a file and ping the target host
#
-
  name: Copy files on remote servers part of group1
  hosts: Host1
  gather_facts: True
  tasks:
    -
      name: copy inventory file
      copy:
        src: ~/bhagyashree/testfile.txt
        dest: ~/bhagyashree/testfile1.txt
    -
      name: check connection to target host
      ping:
 Inventory.txt
--------------------------------
# Inventory file with host variables for connection string to be moved to separate file

Host1 ansible_host=101.53.137.53 ansible_connection="{{ans_conn}}" ansible_user="{{ans_user}}" ansible_password="{{ans_ssh_pass}}"
#Group1.yml file
[Group1]
    ans_conn: ssh
    ans_user: username
    ans_ssh_pass: pwd
# Host1.yml to declare the variables 

    ans_conn: ssh
    ans_user: username
    ans_ssh_pass: pwd

 

Steps executed

 

Step 1 – Identify and analyze the data to be parameterized (Host-specific or Group level variables)

It is important to decide if one target host or multiple hosts within a group will require the data that is to be parameterized. Based on this the location of the variable that will hold the data is finalized.

In the above code snippet, there are target host specific variables that can be stored outside the inventory file for ease of maintenance.

Step 2 – Create variables within the inventory file for the connection parameters

Step 3 – Create a folder named ‘host_vars’ at the same level as that of inventory file. Within this folder, create a YML file whose name must be same as the hostname i.e. if the hostname is Host1, then create the file Host1.yml.

Step 4 – Define the variables within Host1.yml

As per requirement, provide the values to the variables within the host YML file.

Similarly within the Group1.yml, variables can be defined which would be available to all hosts within the group.

Step 5 – Execute the playbook

Output of the connection test is as follows.

The controller has successfully pinged the remote target host.

===========================================================================

Hope you enjoyed my second experiment . I would like to mention that this blog is supported by Agile Testing Alliance and DevOps++ Alliance community initiative of helping folks like me and many others.

You can reach out to me on the following URL

https://www.linkedin.com/in/bhagyashree-patil-b914857/

and ATA Linked in page is

https://www.linkedin.com/company/agile-testing-alliance

 


Note from DevOps++ Alliance

If you want to learn practical devops and experiment with DevOps Pipeline – do not miss our CP-DOF programs

https://cpdof.devopsppalliance.org/

Leave a Reply