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

DevOpSmartBoard - Azure Pipelines

DevOpSmartBoard -The Ultimate End-To-End and One Stop Dashboard solution for all the reporting needs...

Read More >

SAP Intelligent RPA 2.0: End to End working scenario of Supplier Invoice Status Checks Bot. 

Introduction: These days for checking the Invoice and Payment status, most ...

Read More >

How to make your software demos effective.

 I have been involved in Software demonstrations for the last 15+ years. I have done a lot of s...

Read More >

MIGRATION OF ACTIVE DIRECTORY USERS TO FORMS BASED AUTHENTICATION USERS

This document explains step by step information on configuring Form Based Authentication (FBA) in Sh...

Read More >

Upcoming Webinar US - Plugging for Cloud, Right or Wrong

Plugging for Cloud, Right or Wrong?Cloud computing continues to be one of the most advertising topic...

Read More >

Identify and stop idle Dynamics NAV sessions using User Session Control Addon

Since the advent of RTC clients and 3-tier architecture of Dynamics NAV, it’s been a question ...

Read More >

Locking VMs and Resources Groups with Azure Resource Manager using Azure PowerShell

Hello Folks!In this blog post we will be talking about locking down your Azure Resources with Azure ...

Read More >

WSUS, SCCM & MP Server 2008 WebDAV error

Recently when revisiting an internal SCCM deployment I found that the WSUS integration had stopped w...

Read More >

Create a simple Chatbot App using Botframework & Xamarin.Forms

We can integrate Chatbot API with Xamarin.Forms App using two methods, Embed Code with Web View or N...

Read More >

VSTS Team project Monitor

Let us discuss how to install VSTS team project monitor and how we can make use of it easily.Particu...

Read More >

Share

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

Sign Up

  • Recent
  • Popular
  • Tag
Monthly Archive
Subscribe
Name

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