Playbook

A list of things to do with hosts from Inventory. Yes you can run using ansible -m command for each but playbook orchestra it for you by allowing the user defining a list of tasks will be running and then executing it.

Playbook can have multiple Play

Example

Assuming our Inventory is like this:
inventory.yml

all:
    vars:
        ansible_user: root
        ansible_password: test1

webserver:
    hosts:
        springboot:
            ansible_host: 172.20.0.2
        react:
            ansible_host: 172.20.0.3
        python:
            ansible_host: 172.20.0.4

database:
    hosts:
        mysql:
            ansible_host: 172.20.0.5
        mongodb:
            ansible_host: 172.20.0.6

app:
    children:
        database:
        webserver:

We have the following Playbook:
playbook.yml

- name: Prepare webserver
  hosts: webserver
  tasks:
  - name: Ping the webserver
    ansible.builtin.ping:

  - name: Install dependency
    ansible.builtin.debug:
        msg: installing some dependencies

- name: Prepare database
  hosts: database
  tasks:
  - name: Ping the database
    ansible.builtin.ping:

  - name: Install some depndencies
    ansible.builtin.debug:
        msg: installing some dependencies

- name: Start the app
  hosts: app
  tasks:
  - name: start
    ansible.builtin.debug:
        msg: starting the app

We can run this by using

ansible-playbook -i inventory.yml pb.yml

As a result it will run:

  1. Prepare webserver
    1. Gathering facts: (Auto run) to read the inventory file on the whole 3 (springboot, react, python)
    2. Ping the webserver on the whole 3 (springboot, react, python)
    3. Install dependency on the whole 3 (springboot, react, python)
  2. Prepare database
    1. Gathering facts: (Auto run) to read the inventory file on the whole 2 (mysql, mongodb)
    2. Ping the database on the whole 2 (mysql, mongodb)
    3. Install dependency on the whole 2 (mysql, mongodb)
  3. Start the app
    1. Gathering facts: (Auto run) to read the inventory file on the whole 5 (mysql, mongodb, springboot, react, python)
    2. Start the whole 5 (mysql, mongodb, springboot, react, python)