Understanding Playbooks in Ansible

Ansible is a configuration management tool that automates the configuration on bunch of servers by the use of playbooks. Ansible plays are written in YAML. YAML stands for Yet Another Markup Language. Ansible playbook is the file where any engineer is write the code or in other words Playbook is the main feature of Ansible where we tell Ansible what to execute.

Playbook contains the commands which the users want to execute on bunch of servers. These commands are define in way of tasks which are basically module calls.. These tasks are written in simple text language(YAML), which is much easier to understand as compared to XML, JSON, etc.

ansible playbook

See Also:

YAML Basic Rules:

YAML is designed to be human friendly and works perffectly with any programming language. It is useful to manage data and includes Unicode printable characters. When you are creating a file in YAML, you should remember the following basic rules. YAML files should end in .yaml whenever possible in Grav. YAML is case sensitive. YAML does not allow the use of tabs. Spaces are used instead as tabs are not universally supported. A YAML starts with — (3 hyphens)

A basic Ansible Playbook:

To use Ansible, you only need to know minimal YAML structure. Consider the following simple playbook:

# vim /etc/ansible/roles/foo.yml
---
- name: Ansible Playbook to install telnet package on CentOS/RHEL
hosts: all
become: yes
tasks:
- name: Installing telnet on Server
yum:
name: telnet
state: installed
update_cache: yes

The Different YAML Tags
name: This tag will define that what playbook is going to do.
hosts: This tag specifies the list of hosts or host group where we are going to execute the playbook. The hosts field is very important for the playbook.
become: This tag run operations with sudo access but it does not imply password prompting.
tasks: Task are the list of action which you are going to perform on remote node.

Running an Ansible Playbook:
Once you are ready with playbook, just execute mention command to run playbook.

# ansible-playbook /etc/ansible/roles/foo.yml
PLAY [Ansible Playbook to install telnet package on CentOS/RHEL] ****************************

TASK [Gathering Facts] **********************************************************************
ok: [192.168.1.10]

TASK [Installing telnet on Server] **********************************************************
changed: [192.168.1.10]

PLAY RECAP **********************************************************************************
192.168.1.10 : ok=2 changed=1 unreachable=0 failed=0

Note: Also before executing the playbook check the syntax of playbook.

# ansible-playbook /etc/ansible/roles/foo.yml --syntax-check
playbook: /etc/ansible/roles/foo.yml

Reference: Ansible Official Website

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.