A practical example of an Ansible playbook

Sandeep Kumar Patel
4 min readJun 23, 2021

--

Ansible Playbooks

Ansible is a confirmation management tool

Playbooks are the files where the Ansible code is written. Playbooks are written in YAML format. YAML means “Yet Another Markup Language,” so there is not much syntax needed. Playbooks are one of the core features of Ansible and tell Ansible what to execute, and it is used in complex scenarios. They offer increased flexibility.

Playbooks contain the steps which the user wants to execute on a particular machine. And playbooks are run sequentially. Playbooks are the building blocks for all the use cases of Ansible.

Ansible playbooks tend to be more configuration language than a programming language.

Through a playbook, you can designate specific roles to some of the hosts and other roles to other hosts. By doing this, you can orchestrate multiple servers in very different scenarios, all in one playbook.

Playbook Structure

Each playbook is a collection of one or more plays. Playbooks are structured by using Plays. There can be more than one play inside a playbook.

The function of the play is to map a set of instructions which is defined against a particular host.

Ansible playbooks help IT staff program applications, services, server nodes, or other devices without the manual overhead of creating everything from scratch. And Ansible playbooks — as well as the conditions, variables, and tasks within them — can be saved, shared, or reused indefinitely

Ansible Workflow

Ansible works by connecting to your nodes and pushing out a small program called Ansible modules to them. Then Ansible executed these modules and removed them after finished. The library of modules can reside on any machine, and there are no daemons, servers, or databases required.

In the above image, the Management Node is the controlling node that controls the entire execution of the playbook. The inventory file provides the list of hosts where the Ansible modules need to be run. The Management Node makes an SSH connection and executes the small modules on the host’s machine and install the software.

Ansible removes the modules once those are installed so expertly. It connects to the host machine executes the instructions, and if it is successfully installed, then remove that code in which one was copied on the host machine.

A practical example of an Ansible playbook

Sample task to make playbook to stop server copy the code and exception of script and last restart the server

PlayBook - Sandeep.yml

Exception of Playbook -

Output on Node 1-

Output on Node 2-

--

--

Sandeep Kumar Patel
Sandeep Kumar Patel

Written by Sandeep Kumar Patel

Passionate about AI and ML, I see research as purposeful curiosity. Eager for feedback, email -" patelsandeep88@gmail.com"

No responses yet