42

I'm working on several Ansible playbooks to spin up a new server instance. There are approximately 15 different playbooks I need to run in a specific order to successfully spin up a server.

My initial thought was to write a shell script that executes ansible-playbook playbook_name.yml and duplicate it one entry for each playbook I need to run.

Is there a smarter/better way to do this using a master playbook and if so what would it look like (examples are appreciated).

I could write one monolithic playbook that does it all but there are some plays that run as root first then as a sudo user later.

030
  • 6,085
nulltek
  • 1,331

3 Answers3

35

Build many sub-playbooks and aggregate them via include statements.

- include: playbook-one.yml
- include: playbook-two.yml

If your playbooks must run in order and if all of them are mandatory, build a main playbook and include files with tasks. A playbook should always be a closed process.

hmallett
  • 2,485
flxPeters
  • 569
35

For newer versions of Ansible, you can build many sub-playbooks and aggregate them via import_playbook statements:

---
- import_playbook: A-systemd-networkd.yml
- import_playbook: B-fail2ban-ssh.yml
- import_playbook: C-enable-watchdog.yml
Jakuje
  • 10,363
Peter
  • 919
6

From: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/import_playbook_module.html

- hosts: localhost
  tasks:
    - debug:
        msg: play1
  • name: Include a play after another play import_playbook: otherplays.yaml