Ansible Include

Ansible Include
Techiio-author
Written by Nilima PaulJanuary 31, 2022
10 min read
Ansible
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

We will know in this article, what's the Ansible Include.

Definition of Ansible Include

Ansible Include module which is a piece of the ansible-base and the default module accessible with the ansible establishment used to incorporate a record with a rundown of plays or assignments to be executed in the current playbook and it tends to be a solitary incorporate document the different incorporate documents, meaning it calls different playbooks errands or plays and this module likewise works for the windows targets.

Syntax:

This module uses the syntax “Include”.

Parameters:

free-form: optional

This module allows you to specify the name of the file directly without any other options.

How does the Include module work in Ansible?

In the different programming dialects, to work on the program we partition the projects into various documents and modules, and to involve them in the content we utilize the various orders to import them so we don't have to compose the program once more.

The Include module in the Ansible works comparably. We make a playbook to run numerous errands together bringing about long playbooks once in a while which can be befuddling to comprehend and change. All things being equal, we compose the various playbooks joining one or numerous undertakings, and afterward, add them to the principle playbook.

For instance, Let's say, we want to compose the playbook which downloads the compressed document from the web, duplicates it to the objective servers, and unfasten the record at the objective envelope. Consider this all assignments are composed under the main.yml record. Our main.yml record will resemble this.

Main.yml

– Task1: Download the zip file from the internet.

– Task2: Copy the zip file on the target server.

– Task3: Extract the zip file at the target location.

Once we divide the above tasks into the playbooks, for example, Downloadfile.yml, copyfile.yml, and extracfile.yml respectively then we can add them into the main.yml playbook as shown below.

Main.yml

include: Downloadfile.yml

include: copyfile.yml

include: extractfile.yml

We can now utilize the above XML documents in numerous playbooks assuming we have a similar necessity. You can consolidate numerous choices like you can gather task1 and task2 into a single unit in the single playbook and task3 in the other playbook or task1 and task2 in the various playbooks and task3 in the primary playbook according to your necessity.

So Ansible include module has several benefits.

  • Helps to make the overall playbook small and organized.
  • Helps to get rid of the repetitive tasks by using the earlier created playbooks.
  • Helps when the target servers have a dependency, for example, we need to put the server into maintenance and take the servers out of maintenance which are in separate playbooks in the main playbook then you can add both the files with include module at the start and the end of the main playbook respectively.

Disadvantage

It runs the whole playbook so if different playbooks have a similar arrangement of servers, it consumes the time and assembles the realities on numerous occasions if the gather_facts isn't incapacitated.

The underneath model shows how we can utilize the incorporate capacity to add one more playbook in the fundamental playbook.

Includepb.yml

---
- name: Ansible playbook to test Include module
include: servicepb.yml

Servicepb.yml

---
- name: Ansible playbook to stop the spooler service
hosts: winservers
twin servers
- name: Stop the spooler service
win_service:
name: spooler
start_mode: manual
state: stopped

Right, when we run includepb.yml playbook,

ansible-playbook includepb.yml

You will get the condemned module advised considering the way that anything the Ansible interpretation you are running at present, the fuse module will be dispensed with in transformation 2.12, and a substitute strategy for using this request is import_playbook.

---
- name: Ansible playbook to test Include module
Import_playbook: servicepb.yml

Examples

Let us discuss examples of Ansible Include.

Example #1: Ansible “Include” example for adding multiple playbooks.

Suppose you have the below main playbook called includepb.yml

---
- name: Ansible playbook for the include module
hosts: wins servers
tasks:
- name: Task to stop the spooler service
win_service:
name: spooler
start_mode: manual
state: stopped
- name: Copy ansible.cfg file to the c:\temp
win_copy:
src: /etc/ansible/ansible.cfg
dest: c:\temp\

Now we divide both the tasks into two files called servicepb.yml and copypb.yml respectively. To include both the playbooks in the includepb.yml file,

---
- name: Calling service playbook
include: servicepb.yml
- name: calling copy playbook
include: copypb.yml

Output:

ansible-playbook includepb.yml

blogpost

On the off chance that you have seen, every playbook runs exclusively and accumulates current realities. So assuming you are utilizing the incorporate module for a similar arrangement of servers then it isn't so helpful. The other substitute choice is to import the undertakings rather than the playbooks utilizing include_tasks.

To run the singular undertakings then your playbooks ought to contain just the assignments.

servicepb.yml

---
- name: Task to stop the spooler service
win_service:
name: spooler
start_mode: manual
state: started

copyob.yml

---
- name: Copying ansible.cfg file
win_copy:
src: /etc/ansible/ansible.cfg
dest: c:\temp\

includepb.yml

---
- name: Ansible playbook for Include_tasks
hosts: winservers
tasks:
- include_tasks: servicepb.yml
- include_tasks: copypb.yml

ansible-playbook includepb.yml

Output:

blogpost

You can notice that the different tasks are imported and run against the specified hosts in the main playbook (includepb.yml).

Example #2: Adding another playbook with the different tasks.

In this example, we will import the ansible-playbook into the current playbook along with the other tasks.

Servicepb.yml

---
- name: Ansible playbook to stop the spooler service
hosts: win servers
tasks:
- name: Stop the spooler service
win_service:
name: spooler
start_mode: manual
state: stopped

inludepb.yml

---
- name: Ansible playbook for importing playbook
hosts: win servers
tasks:
- debug:
msg: "Task1 to display"
- name: Task to copy ansible.cfg to windows servers
win_copy:
src: /etc/ansible/ansible.cfg
dest: c:\temp\
- name: import servicepb.yml
include: servicepb.yml

ansible-playbook includepb.yml

Output:

blogpost

You can also add the include task at the top and you can also specify the condition to run the include task with when.

Conclusion

Ansible "incorporate" module is helpful when we want to run numerous errands on the different climates or the different arrangement of servers however if you have a similar arrangement of servers, the incorporate module isn't that valuable since it runs the various assignments on similar servers, all things considered, we can utilize the include_tasks to simply import the undertakings.

Ansible
ansib facts
ansible inventory
Ansible Include
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Nilima Paul
Technology Security Analyst
Techiio-followerTechiio-followerTechiio-follower
201 Blog Posts
0 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance