Skip to main content

Playbook

Let's create our first simple playbook. We're going to install Nginx for testing.

AD-HOC vs Playbooks

Ad-hoc commands are lost, and that's why we put everything in playbooks. Generally, Ansible usage should always be done on top of playbooks.

Have you thought about having to execute command by command for everything you did and not even being able to remember it again or reuse what was done? With a file you can have better management of your commands and provide better maintenance to your machine fleet. In case of configuration always use a playbook, in case of queries you can use ad-hoc.

Let's modify the host by forming groups now. In this case, every time we reference webservers, Ansible will enter both machines, and db only on robot-2.

[webservers]
robot-1
robot-2

[db]
robot-2

First Playbook

See my_playbook to view the file we're going to apply.

Notice that we've already included the hosts where the task will be executed, so we don't even need to pass the hosts. Another detail is that we passed the remote_user and when this variable is passed it replaces the one defined in the .cfg file.

~/projects/ansible/study-ansible/Aula 3  - Playbook main !3 ?3                                                                                                                     1.1.7 18:07:41
❯ ansible-playbook my_playbook.yml

PLAY [Install nginx] ******************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************
ok: [robot-2]
ok: [robot-1]

TASK [Installing Nginx] *************************************************************************************************************************************************************************
changed: [robot-1]
changed: [robot-2]

PLAY RECAP ****************************************************************************************************************************************************************************************
robot-1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
robot-2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0


~/projects/ansible/study-ansible/Aula 3 - Playbook main !3 ?3 1.1.7 18:10:13
curl http://robot-1:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>


~/projects/ansible/study-ansible/Aula 3 - Playbook main !3 ?3 1.1.7 18:11:18
❯ ansible webservers -m shell -a "ps -ef | grep nginx"
robot-2 | CHANGED | rc=0 >>
root 13122 1 0 21:08 ? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 13125 13122 0 21:08 ? 00:00:00 nginx: worker process
ubuntu 13314 13313 0 21:11 pts/0 00:00:00 /bin/sh -c ps -ef | grep nginx
ubuntu 13316 13314 0 21:11 pts/0 00:00:00 grep nginx
robot-1 | CHANGED | rc=0 >>
root 13755 1 0 21:08 ? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 13758 13755 0 21:08 ? 00:00:00 nginx: worker process
ubuntu 13947 13946 0 21:11 pts/0 00:00:00 /bin/sh -c ps -ef | grep nginx
ubuntu 13949 13947 0 21:11 pts/0 00:00:00 grep nginx

Second Playbook

See my_playbook2

In this case, we're going to use the service module to ensure that the service will be active and started.

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/service_module.html

~/projects/ansible/study-ansible/Aula 3  - Playbook main !3 ?3                                                                                                                     1.1.7 18:20:51
❯ ansible-playbook my_playbook\ 2.yml

PLAY [Install nginx] ******************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************
ok: [robot-2]
ok: [robot-1]

TASK [Installing Nginx] *************************************************************************************************************************************************************************
ok: [robot-1]
ok: [robot-2]

TASK [Enabling nginx service] ******************************************************************************************************************************************************************
ok: [robot-1]
ok: [robot-2]

TASK [Starting nginx service] ********************************************************************************************************************************************************************
ok: [robot-2]
ok: [robot-1]

PLAY RECAP ****************************************************************************************************************************************************************************************
robot-1 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
robot-2 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

Third Playbook Version

See my_playbook3 We're going to use the copy module and a handler that is notified to restart nginx when a configuration file is modified.

Where it's ok means it didn't change because it was done by previous commands, that is, it doesn't re-execute. This is idempotency.

❯ ansible-playbook my_playbook3.yml

PLAY [Install nginx] ******************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************
ok: [robot-1]
ok: [robot-2]

TASK [Installing Nginx] *************************************************************************************************************************************************************************
ok: [robot-2]
ok: [robot-1]

TASK [Enabling nginx service] ******************************************************************************************************************************************************************
ok: [robot-1]
ok: [robot-2]

TASK [Starting nginx service] ********************************************************************************************************************************************************************
ok: [robot-2]
ok: [robot-1]

TASK [Copying the index] ***************************************************************************************************************************************************************************
changed: [robot-2]
changed: [robot-1]

TASK [Copying nginx.conf] **********************************************************************************************************************************************************************
changed: [robot-2]
changed: [robot-1]

RUNNING HANDLER [Restarting nginx] *************************************************************************************************************************************************************
changed: [robot-2]
changed: [robot-1]

PLAY RECAP ****************************************************************************************************************************************************************************************
robot-1 : ok=7 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
robot-2 : ok=7 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

Extras

A playbook execution can be done with some extras.

Can execute with several verbosity levels.

ansible-playbook main.yml
ansible-playbook main.yml -v
ansible-playbook main.yml -vv
ansible-playbook main.yml -vvv
ansible-playbook main.yml -vvvv

Serves to rehearse changes and what would happen, good for a destructive situation.

ansible-playbook main.yml --check

You can use diff to see the differences.

ansible-playbook main.yml --diff

You can execute by calling a specific tag for a role or tag to avoid running the entire file.

ansible-playbook main.yml --tag "install_role"

or adding everything together.

ansible-playbook main.yml --diff -vv --tag"install_role"