Playbook
The main entry to run your ansible script.
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:
- Prepare webserver
- Gathering facts: (Auto run) to read the inventory file on the whole 3 (
springboot
,react
,python
) - Ping the webserver on the whole 3 (
springboot
,react
,python
) - Install dependency on the whole 3 (
springboot
,react
,python
)
- Gathering facts: (Auto run) to read the inventory file on the whole 3 (
- Prepare database
- Gathering facts: (Auto run) to read the inventory file on the whole 2 (
mysql
,mongodb
) - Ping the database on the whole 2 (
mysql
,mongodb
) - Install dependency on the whole 2 (
mysql
,mongodb
)
- Gathering facts: (Auto run) to read the inventory file on the whole 2 (
- Start the app
- Gathering facts: (Auto run) to read the inventory file on the whole 5 (
mysql
,mongodb
,springboot
,react
,python
) - Start the whole 5 (
mysql
,mongodb
,springboot
,react
,python
)
- Gathering facts: (Auto run) to read the inventory file on the whole 5 (