Slide 1
Most trusted JOB oriented professional program
DevOps Certified Professional (DCP)

Take your first step into the world of DevOps with this course, which will help you to learn about the methodologies and tools used to develop, deploy, and operate high-quality software.

Slide 2
DevOps to DevSecOps – Learn the evolution
DevSecOps Certified Professional (DSOCP)

Learn to automate security into a fast-paced DevOps environment using various open-source tools and scripts.

Slide 2
Get certified in the new tech skill to rule the industry
Site Reliability Engineering (SRE) Certified Professional

A method of measuring and achieving reliability through engineering and operations work – developed by Google to manage services.

Slide 2
Master the art of DevOps
Master in DevOps Engineering (MDE)

Get enrolled for the most advanced and only course in the WORLD which can make you an expert and proficient Architect in DevOps, DevSecOps and Site Reliability Engineering (SRE) principles together.

Slide 2
Gain expertise and certified yourself
Azure DevOps Solutions Expert

Learn about the DevOps services available on Azure and how you can use them to make your workflow more efficient.

Slide 3
Learn and get certified
AWS Certified DevOps Professional

Learn about the DevOps services offered by AWS and how you can use them to make your workflow more efficient.

previous arrow
next arrow

Ansible Include and Import with differences explanined!

Spread the Knowledge

This is one of the most frequest questions being asked in my workshop is that differences between using import or include in Ansible. For example import_role or include_role, what should be behaviour and what to expect?

We know that include could be applied to tasks, plays and handlers. The bare include task (which was used for both Task files and Playbook-level includes) is still available, however it is now considered deprecated and introduced with include_tasks, include_role and include_role.

With filename variable substitution, the code could become much more clean.

include: “{{ codename }}.yml”

We also know that include using(include and import) could be dynamic or static.

Differences Between Static and Dynamic

Ansible has two modes of operation for reusable content with Including and Importing and Roles: dynamic and static.

The main difference is:

All import* statements are pre-processed at the time playbooks are parsed.
All include* statements are processed as they encountered during the execution of the playbook.
So import is static, include is dynamic.

In Ansible 2.0, the concept of dynamic includes was introduced. Due to some limitations with making all includes dynamic in this way, the ability to force includes to be static was introduced in Ansible 2.1.

Static import
If you use any import* Task (import_playbook, import_tasks, etc.), it will be static.

Dynamic Include
If you use any include* Task (include_tasks, include_role, etc.), it will be dynamic.

The two modes of operation are pretty simple:

  • Ansible pre-processes all static imports during Playbook parsing time.
  • Dynamic includes are processed during runtime at the point in which that task is encountered.

When it comes to Ansible task options like tags and conditional statements (when:):

  • For static imports, the parent task options will be copied to all child tasks contained within the import.
  • For dynamic includes, the task options will only apply to the dynamic task as it is evaluated, and will not be copied to child tasks.

The primary advantage of using include* statements is looping. When a loop is used with an include, the included tasks or role will be executed once for each item in the loop.

- import_tasks: x.yml 
  with_items: [1,2,3] 

This willi import x.yml one time and every imported task will now run 
and loop over 1,2,3 

- include_tasks: x.yml 
  with_items: [1,2,3] 

This will include x.yml 3 times and set the 'item' variable to 1, 2 
and 3 respectively 

In order to conclude this, Basically import tasks will be parsed at the beginning when you run the playbook, but include tasks will only be parsed at the moment Ansible hits them.

Means, with include Running the Playbook which has list of tasks will execute until the error tasks are found, which is in included file.

On other end, Wait, with import, it will flag a error to begin with only. The reason is because the import_role task is pre-processed at the time playbooks are parsed. So it found the syntax error much earlier and so it never was able to even run our first task with the debug module.

This is an example that really emphasizes the fact that import is parsed quite early. This is what is referred to as static.

Ansible include and import has following module which must be explored



Rajesh Kumar
Latest posts by Rajesh Kumar (see all)
0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x