X
22Jun

Ansible Playbooks

Playbooks are nothing but files consisting of your written code, and they are written in YAML language, which defines the tasks and executes them through the Ansible. Playbooks may include one or more plays. Plays defines a set of activities or tasks to be run on hosts of inventory file.

First, we'll see the basics of ansible playbook contents in order to understand more about ansible playbooks through some of the samples below. Next, we will also distinguish the YAML language with other languages such as XML and Json, through some sample code.

                The Playbook always begins with the three dashes (i.e. '- -') and finishes with three dots (i.e. '...'). Items beginning with a single dash (i.e. '-') are considered list items. Ansible playbook filename has always the 'yml' or 'yaml' extension. Ansible Playbook includes tags such as name, hosts, variables, tasks and handlers etc. Now, let us talk one by one about the tags below.

Name: This tag specifies Ansible Playbooks name.

Hosts: It specifies the list or group of servers to run the tasks. Host tag is compulsory. This tells the ansible that the tasks that are to be performed on the specified host list and it may be the same machine or remote machine.

Variables: ‘Vars’ tag defines the variables which can be used on your playbook. Variables in ansible are defined in different ways. The simplest way to do this is to use the ‘vars’ section in the playbook.

Tasks: Tasks are the lists of the actions that are performed on the hosts. Tasks shall have the task name. All playbooks will include the tasks which are to be performed. Examples of the tasks are executing a command, run a shell script, install a package, shutdown or restart the hosts etc.

Handlers: Handlers are special tasks which run when notified by another task at the end of a play. if there is any change in the configuration file, for example, notify a service to restart task it needs to run. ‘Notify’ tag is used to call handlers.

Let's see below are the sample code of the XML, JSON and YAML languages to differentiate between them.

 

XML Language:

<emprecord>
<employee>
<name>Canarian</name>
<job>software_engineer</job>
<skills>devops</skills>
</employee>
</emprecord>

 

JSON Language:

{
"EmpRecord": {
"Employee": [
{
"name": "Canarian",
"job": "software_engineer",
"skills": "devops"
}
]
}
}

YAML Language:

 # Employee Records
-	Employee:
      name: canarian
      job: software_engineer
      skills: devops

Let me show you how to create an ansible playbook to install and start ‘nginx’ on a host and start it. Create a ‘nginx.yml’ file where you will write your YAML code for creating an ansible playbook.

Filename: nginx.yml

---
- hosts: app_servers
  sudo: yes
  vars:
    - server_port: 8080

  tasks:
    - name: Installs nginx web server
      apt: pkg=nginx state=installed update_cache=true
      notify:
        - start nginx

  handlers:
    - name: start nginx
      service: name=nginx state=started

The above YAML file starts with hosts, so that you want to run this playbook on the app_servers machine. The hosts IP address is already saved in /etc/ansible/hosts file. let’s run the playbook. Below is the syntax to run an ansible playbook.

$ ansible-playbook (playbook_filename)

E.g.: $ ansible-playbook nginx.yml

That was about Ansible playbook. In this article, I explained and discussed how to create and run an ansible playbook via one of the ansible sample playbooks (i.e. installing nginx server), and also discussed the basics of ansible playbook that will help you learn more about ansible.

 

 

 

Related

TFS Build for .Net 2.0 Projects

Clients have strange requirements. The other day I had been to a client for a discussion. These guys...

Read More >

Access Control in Swift 3

End of this blog you will be able to understand the Access control specifiers open, public,internal,...

Read More >

VSTS Area Uploader

VSTS Area Uploader helps you to upload Areas (with multiple level) to your VSTS Team Projects from a...

Read More >

Technical Consultant | Visual Studio

Should be able to perform independently and with the team. Experience in creating reports using SQL...

Read More >

NAV – Connecting External SQL

This blog is intended to see how Microsoft Dynamics NAV (here onwards NAV) can be connected to exter...

Read More >

Canarys has partnered with Openbravo a Spain based ERP solution provider

We are happy to announce our newly acquired partnership with Openbravo, Spain. Openbravo is the prov...

Read More >

How to Install TestNG framework (Step by Step installation process)

Steps to Install Eclipse using install new Software:Step 1: In Eclipse, on top menu bar, Under Help ...

Read More >

How to print Header and Details (Lines) in different pages of a RDLC Report in NAV 2013 R2

We usually print header and details section of a document on same page, what if we want to print on ...

Read More >

Share

Comments

Hello!I explained and discussed how to create and run an ansible playbook via one of the ansible sample playbooks. installing Nginx server), and also discussed the basics of ansible-playbook that will help you learn more about ansible. This tells the ansible that the tasks that are to be performed on the specified host list and it may be the same machine or remote machine.
1/5/2021 5:07:22 PM | Reply
I explained and discussed how to create and run an ansible playbook via one of the ansible sample playbooks. installing Nginx server), and also discussed the basics of ansible-playbook that will help you learn more about ansible. This tells the ansible that the tasks that are to be performed on the specified host list and it may be the same machine or remote machine.ornamentsbyelves
1/5/2021 5:05:36 PM | Reply
This post is a valuable, which makes me to visit this again and again for the knowledge.
9/23/2020 4:57:13 PM | Reply
Its a great article with meaningful content. Thanks for sharing your knowledge
9/23/2020 2:46:39 PM | Reply
kgyJTfMKVj
7/29/2020 5:32:25 AM | Reply
mhZbxStfLu
7/28/2020 7:05:35 AM | Reply
vcPRAHaGUWxrmjF
7/25/2020 12:48:32 AM | Reply
VkefpjzSZ
7/2/2020 3:57:49 AM | Reply

Post a Comment

Try DevOpSmartBoard Ultimate complete Azure DevOps End-to end reporting tool

Sign Up

  • Recent
  • Popular
  • Tag
  • Key Metrics to Measure DevOps Success

    DevOps words means many things to many people and all the definitions might be partly right. DevOps is all about continuously seeking feedback, understanding challenges, and improving the way the soft...
  • Bulk Export Import Test Cases to Azure DevOps Test Plans using CSV file

    As a user of Azure DevOps Test Plans or TFS Test Management for manual testing, from a long time everyone including me had a wish, that copy of test cases from excel or csv file along with test steps ...
  • Azure DevOps Overview

    Now a days Azure DevOps is very famous with IT and Operations team along with developers, so what is Azure DevOps? It is a Software as a service (SaaS) platform from Microsoft that provides features r...
  • Volume Management in Kubernetes

    Since pods created in k8s are ephemeral, we are able to get the data as long as pods are alive, but if pods are terminated data stored in it completely lost and it cannot get back, for that we need to...
Tags
Monthly Archive
Subscribe
Name

Text/HTML
Text/HTML
Contact Us
  • *
  • *