Pages

Wednesday, February 3, 2016

Variable in Ansible


Variables play a important role in every programming language. Ansible also provides various ways of setting variables. They are used to store values that can be later used in the playbook. Facts regarding the remote machine can also be fetched as variables and used.

Declaration
Variable names in Ansible should start with a letter. The variable can have letter, numbers and underscore. Invalid variable declaration comes when we use dot (.), a hyphen (-), a number or variable separated by mutlple words.

Varaibles in Ansible can be set in different ways. They are
Passing a Varaible file
Declaring them in the playbook
Passing them to the Ansible-playbook command using the –e/--extra-vars argument
Declaring them in the inventory file

Lets see how we can define variables in Ansible and use them.

Passing a Varaible file - A Varaible can be defined in a variable file and can be passed to a playbook using the include

Lets create a install_apache.yml file with the below content,

[root@vx111a 2test]# cat install_apache.yml
- set_fact: package_name=httpd
- name: install Apache
  yum: name=httpd state=present

In the above yml file, we have defines a variable “package_name” which was given a value of httpd. After that we are installing the Apache Package. Now we will use this yml file into our playbook as,
 
---
- hosts: dev
  tasks:
    - include: install_apache.yml
    - name: check Apache Service
      service: name={{ package_name }} state=restarted

If you check the above playbook, we can see that we have used a include element for including the install_apache.yml file. We also defined a task for restarting the httpd using the Service module. We have defined to access the package_name using the service: name={{ package_name }} state=restarted . Now lets run the playbook

[root@vx111a 2test]# ansible-playbook test.yml

PLAY [dev] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [172.16.202.96]

TASK: [set_fact package_name=httpd] *******************************************
ok: [172.16.202.96]

TASK: [install Apache] ********************************************************
ok: [172.16.202.96]

TASK: [check Apache Service] **************************************************
changed: [172.16.202.96]

PLAY RECAP ********************************************************************
172.16.202.96              : ok=4    changed=1    unreachable=0    failed=0  

We can see that the play book ran perfectly fine with accessing the variable name package_name from the install_apache.yml file that we included.

Another way of passing variable file to the playbook is by using the vars_file declaration as

[root@vx111a 2test]# cat test1.yml
---
- hosts: dev
  vars_files:
    - var1.yml
 
  tasks:
   - name: install httpd Package
     yum: name=httpd state=present

   - name: restart Apache
     service: name="{{ package_name }}" state=restarted

Now the var1.yml file contains the variables that we need to define as
 [root@vx111a 2test]# cat var1.yml
---
package_name: "httpd"

Declaring them in the playbook – Ansible allows variables to be defined in the Playbook it self. Variables in the playbook can be defined using the vars declaration as

[root@vx111a 2test]# cat apache.yml
---
- hosts: dev
  vars:
   - package_name: "httpd"

  tasks:
   - name: install httpd Package
     yum: name=httpd state=present

   - name: restart Apache
     service: name="{{ package_name }}" state=restarted
If we run the playbook we can see the  below output which is fine

[root@vx111a 2test]# ansible-playbook test1.yml

PLAY [dev] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [172.16.202.96]

TASK: [install httpd Package] *************************************************
ok: [172.16.202.96]

TASK: [restart Apache] ********************************************************
changed: [172.16.202.96]

PLAY RECAP ********************************************************************
172.16.202.96              : ok=3    changed=1    unreachable=0    failed=0  

Passing them to the Ansible-playbook command using the –e/--extra-vars argument – Another way of passing arguments is by passing them to the command line while running using the –extra-vars parameter.

Lets use the same above playbook,
[root@vx111a 2test]# cat apache.yml
---
- hosts: dev
  vars:
   - package_name: "httpd"

  tasks:
   - name: install httpd Package
     yum: name=httpd state=present

   - name: restart Apache
     service: name="{{ package_name }}" state=restarted

Now while running the playbook we can pass the argument with values as

[root@vx111a 2test]# ansible-playbook apache.yml --extra-vars "package_name=httpd"

PLAY [dev] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [172.16.202.96]

TASK: [install httpd Package] *************************************************
ok: [172.16.202.96]

TASK: [restart Apache] ********************************************************
changed: [172.16.202.96]

PLAY RECAP ********************************************************************
172.16.202.96              : ok=3    changed=1    unreachable=0    failed=0  

We have passed the –extra-vars “package_name=httpd” to the command line argument which will replace the values available.

Declaring them in the inventory file - All of the preceding variables are applied globally to all hosts against which you are running your Ansible playbook. You might sometimes need to use a specific list of variables for a specific host. Ansible supports this by declaring your variables inside an inventory file.

[root@vx111a inventory]# cat hosts
172.16.202.96 db_name=test

Using the following inventory file, you can access the variable db_name for the 172.16.2302.96 host.

That’s all about the Variables in Ansbile. More to Come. Happy Learning J

2 comments :