Pages

Thursday, February 11, 2016

Ansible – Include

Share it Please

We have how we can use variable which working with playbooks. However, it's not just variables that we include from other files; it can also be common tasks or handlers. This is a feature in Ansible to reduce duplicity while writing tasks. This also allows us to have smaller playbooks by including reusable code in separate tasks using Don't Repeat Yourself (DRY).

The below playbook is the most basic example of using the include construct

[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.

As said above, include not just allows variable files to be included but it also allows to include tasks and handlers. Lets see another example of using the Include. Lets see the basic playbooks

[root@vx111a include]# cat ./tasks/include_tasks.yml
---
- name: "{{ service_state }} {{ service_name }} is being started"
  service: name={{ service_name}} state={{ service_state }}


[root@vx111a include]# cat main.yml
---
- hosts: cent
  tasks:
    - include: tasks/include_tasks.yml service_name="httpd" service_state="stopped"

The include_tasks.yml is created under the tasks folder which contains a task with service module to stop the Httpd service. Now we will define another playbook file which includes the include_tasks.yml file as in main.yml. In the include_task.tml file I have not just defined the service but I also passed the service name and state as arguments. Now once we execute the playbook we see,

[root@vx111a include]# ansible-playbook main.yml

PLAY [cent] *******************************************************************

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

TASK: [stopped httpd is being started] ****************************************
ok: [172.16.202.96]

PLAY RECAP ********************************************************************
172.16.202.96              : ok=2    changed=0    unreachable=0    failed=0 

Hope this gives you a basic understating of using Include in Ansible

1 comment :

  1. I have been surfing on-line more than 3 hours today, yet I never found any interesting article
    like yours. It's beautiful value enough for me. In my view, if all webmasters and bloggers made good content material
    as you probably did, the internet shall be much more
    helpful than ever before.

    ReplyDelete