The include_tasks module imports a list of tasks to the current playbook for execution. It is one way for organizing tasks in separate files and it also helps in reusing the tasks in other playbooks.
The import_playbook imports the whole playbook that includes tasks, variables, etc from another file to the calling playbook.
The below is an example where tasks and playbooks are included/imported from separate files.
[root@centos9vm ~]# cat environment.yml
==== ====
– – –
– name: Install the {{ package }} package
ansible.builtin.dnf:
name: “{{ package }}”
state: latest
– name: Start the {{ service }} service
ansible.builtin.service:
name: “{{ service }}”
state: started
enabled: true
==== ====
[root@centos9vm ~]# cat test.yml
==== ===
– – –
– name: Test web service
hosts: testGRP
become: false
tasks:
– name: connect to internet web server
ansible.builtin.uri:
url: “{{ url }}”
status_code: 200
==== ===
[root@centos9vm ~]# cat firewall.yml
===== ===
– – –
– name: Install the firewall
ansible.builtin.dnf:
name: “{{ firewall_pkg }}”
state: latest
– name: Start the firewall
ansible.builtin.service:
name: “{{ firewall_svc }}”
state: started
enabled: true
– name: Open port for {{ rule }}
ansible.posix.firewalld:
service: “{{ item }}”
state: enabled
permanent: true
immediate: true
loop: “{{ rule }}”
======
Now let us put his into a main playbook, to manage the node 192.168.0.129.
[root@centos9vm ~]# cat playbook.yml
===== ===
– – –
– name: Configure web server
hosts: 192.168.0.129
tasks:
– name: Include the environment task file and set the variable
include_tasks: environment.yml
vars:
package: httpd
service: httpd
– name: Include the firewall task file and set the variables
include_tasks: firewall.yml
vars:
firewall_pkg: firewalld
firewall_svc: firewalld
rule:
– http
– https
– name: Import test play and set the variable
import_playbook: test.yml
vars:
url: ‘http://192.168.0.129’
===== ===
[root@centos9vm ~]# ansible-navigator run -m stdout playbook.yml
==== ===
PLAY [Configure web server] ****************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.0.129]
TASK [Include the environment task file and set the variable] ******************
included: /root/environment.yml for 192.168.0.129
TASK [Install the httpd package] ***********************************************
ok: [192.168.0.129]
TASK [Start the httpd service] *************************************************
ok: [192.168.0.129]
TASK [Include the firewall task file and set the variables] ********************
included: /root/firewall.yml for 192.168.0.129
TASK [Install the firewall] ****************************************************
ok: [192.168.0.129]
TASK [Start the firewall] ******************************************************
ok: [192.168.0.129]
TASK [Open port for [‘http’, ‘https’]] *****************************************
ok: [192.168.0.129] => (item=http)
ok: [192.168.0.129] => (item=https)
TASK [Include the placeholder task file and set the variable] ******************
included: /root/placeholder.yml for 192.168.0.129
TASK [Create placeholder file] *************************************************
ok: [192.168.0.129]
PLAY [Test web service] ********************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.0.129]
TASK [connect to internet web server] ******************************************
ok: [192.168.0.129]
PLAY RECAP *********************************************************************
192.168.0.129 : ok=12 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
=== ====