DEV Community

Andres
Andres

Posted on • Edited on

Multiple Playbooks in Ansible

Ansible is a simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.

Ansible works by connecting to your nodes and pushing out small programs, called "Ansible modules" to them. These programs are written to be resource models of the desired state of the system. Ansible then executes these modules (over SSH by default), and removes them when finished.


TL;DR

Ansible has the option to import multiple playbooks into a single one using the method: import_playbook:

/playbooks/main.yml
---
- name: Playbook_1
    import_playbook: 00-playbook-1.yml
- name: Playbook_2
    import_playbook: 01-playbook-2.yml

When you execute ansible-playbook playbooks/main.yml Ansible will execute first the Playbook1, and then Playbook 2.

We're going through an example to see it in action


The first playbook executes a ping into your servers:

/playbooks/00-ping.yml
---
- hosts: all
  tasks:
    - name: Ping All Servers
      action: ping
    - debug: msg="Success"

The second playbook upgrades all your yum packages:

/playbooks/01-upgrade.yml
---
- hosts: all
  tasks:
    - name: upgrade all packages
      become: yes
      yum:
        name: '*'
        state: latest

The third playbook download a node binary distribution:

/playbooks/02-download-node.yml
---
- hosts: all
  tasks:
    - name: Download node binary distro
      shell: curl sL https://rpm.nodesource.com/setup_12.x | sudo bash -

The fourth playbook install node:

/playbooks/03-install-node.yml
---
- hosts: all
  tasks:
    - name: Install Node
      become: yes
      yum:
        name: nodejs
        state: present

The fifth playbook copy your app into the remote servers:

/playbooks/04-provisioning.yml
---
- hosts: all
  tasks:
    - name: Copy app files
      copy:
        src: ~/documents/my-app-folder/
        dest: /home/ec2-user/my-app
        force: no
    - debug: msg="Success"

And finally you need to install dependencies based on your package-json:

/playbooks/05-install-dependencies.yml
---
- hosts: all
  tasks:
    - name: Install packages based on package.json.
      npm:
        path: /home/ec2-user/my-app

Once you have all your playbooks ready, if you want to execute all of them in the desired order you need to create a main playbook, and import all of them as follow:

/playbooks/main.yml
---
- name: Ping
  import_playbook: 00-ping.yml

- name: Update
  import_playbook: 01-update.yml

- name: Download Node
  import_playbook: 02-download-node.yml

- name: Install Node
  import_playbook: 03-install-node.yml

- name: Provisioning
  import_playbook: 04-provisioning.yml

- name: Install Dependencies
  import_playbook: 05-install-dependencies.yml

That's it. You are ready to execute ansible-playbook playbooks/main.yml and you'll upgrade all your packages, download and install node, provision all your selected servers, and install your dependencies.

Top comments (0)