Ansible Playbooks - Blogs
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

Overview of Microsoft Dynamics CRM 2013

What is CRM  Today’s world is fast changing and full of challenges, which fundamenta...

Read More >

Create a Windows Server 2012 R2 VM using ARM in Azure PowerShell

Hi Folks,In this Blog Post we will learn how to create an Azure ARM Virtual Machine using Azure Powe...

Read More >

Ansible Ad-Hoc Commands and Playbooks

An ansible ad-hoc command is a one-line command that lets you perform basic tasks efficiently withou...

Read More >

Microsoft Office 2013 Features and Looks

Now Office 2013 launched recently, now its not available for users may be its available for User aft...

Read More >

How to Print Microsoft Dynamics NAV 2013 R2 RDLC Report Page Footer on Last Page Only

As we are all know there were two properties in NAV 2009 to print footer at bottom and on last page ...

Read More >

How to Set Up Selenium WebDriver in Visual Studio Enterprise 2015?

Pre-requisite : Visual Studio Enterprise 2015 application should be installed into the system.Create...

Read More >

Excel Keyboard Shortcuts

Below are some of the Keyboard shortcuts I use on a daily basis. Maybe helpful to others Ctrl +...

Read More >

Telerik RAD (Rapid Application Development) Controls for ASP.NET AJAX

Introduction:Telerik provides a ultimate toolbox for developers which includes Web UI Controls, Desk...

Read More >

HOW TO DO BACKUP AND RESTORE ENTIRE K8S CLUSTER?

If you have deployed different applications on k8s cluster using various objects like deployments, p...

Read More >

Introduction to Load Testing

 Well, before learning what is Load testing. Let us discuss briefly the performance testing.&nb...

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
Contact Us
  • *
  • *