Ubuntu 24.04
Sponsored Link

Ansible : Use Playbook (include)2024/07/31

 

It's possible to include tasks or Playbooks from other Playbooks.

[1] If you'd like to include other tasks, write [include: ***] in [tasks] section.
ubuntu@dlp:~$
vi playbook_sample.yml
# include [included.yml] under the [tasks] directory

- hosts: target_servers
  become: yes
  become_method: sudo
  tasks:
    - include_tasks: tasks/included.yml

ubuntu@dlp:~$
mkdir tasks

ubuntu@dlp:~$
vi tasks/included.yml
# OK to set task definition only

- name: General packages are installed
  apt:
    name: "{{ packages }}"
    state: present
  vars:
    packages:
    - tar
    - wget
    - unzip
  tags: General_Packages

ubuntu@dlp:~$
ansible-playbook playbook_sample.yml --ask-become-pass

BECOME password:

PLAY [target_servers] **********************************************************

TASK [Gathering Facts] *********************************************************
ok: [10.0.0.51]
ok: [10.0.0.52]

TASK [include_tasks] ***********************************************************
included: /home/ubuntu/tasks/included.yml for 10.0.0.51, 10.0.0.52

TASK [General packages are installed] ******************************************
ok: [10.0.0.52]
ok: [10.0.0.51]

PLAY RECAP *********************************************************************
10.0.0.51                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
10.0.0.52                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
[2] For import other Playbooks, use [import_playbook].
In addition to [1], include a Playbook that [httpd] is running.
ubuntu@dlp:~$
vi playbook_sample.yml
- hosts: target_servers
  become: yes
  become_method: sudo
  tasks:
    - include_tasks: tasks/included.yml
# include Playbook
- import_playbook: httpd.yml

ubuntu@dlp:~$
vi httpd.yml
- hosts: target_servers
  become: yes
  become_method: sudo
  tasks:
  - name: apache2 is installed
    apt:
      name: apache2
      state: present
  - name: apache2 is running and enabled
    service:
      name: apache2
      state: started
      enabled: yes

ubuntu@dlp:~$
ansible-playbook playbook_sample.yml --ask-become-pass

BECOME password:

PLAY [target_servers] **********************************************************

TASK [Gathering Facts] *********************************************************
ok: [10.0.0.51]
ok: [10.0.0.52]

TASK [include_tasks] ***********************************************************
included: /home/ubuntu/tasks/included.yml for 10.0.0.51, 10.0.0.52

TASK [General packages are installed] ******************************************
ok: [10.0.0.52]
ok: [10.0.0.51]

PLAY [target_servers] **********************************************************

TASK [Gathering Facts] *********************************************************
ok: [10.0.0.51]
ok: [10.0.0.52]

TASK [apache2 is installed] ****************************************************
ok: [10.0.0.52]
ok: [10.0.0.51]

TASK [apache2 is running and enabled] ******************************************
ok: [10.0.0.52]
ok: [10.0.0.51]

PLAY RECAP *********************************************************************
10.0.0.51                  : ok=6    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
10.0.0.52                  : ok=6    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
Matched Content