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

Yes. It is Another Gold!

Keeping up the trend of staying current with the latest and the greatest technologies, Canarys has n...

Read More >

Apache Maven – A tool to build Artifacts

Apache Maven is a software project management and comprehension tool. Based on the concept of a proj...

Read More >

How to build chat application using SIgnalR

IntroductionHello friends, This blog is related to a chat application which conatins the exchanging ...

Read More >

VSTS on Mobile

Thank you for the overwhelming response for our first release of VSTS on mobile app. More than thous...

Read More >

Unit testing in .NET

Unit Testing – ExplainedA process which involves writing code to verify a system at a lower an...

Read More >

Analysis of Load Test Results

Analysis of Performance DataAfter you capture and consolidate your results, analyze the captured dat...

Read More >

Migrating Work Items from 1 Team Project to another Team Project in VSTS

Hi ya'll, in this post I am gonna show you how to migrate work items from 1 Team Project (TP) to ano...

Read More >

Microsoft Azure Webinars Registration- US

Date : 11th Feb 2016 | Day  : Thursday | Time : 11:30 AM EST | Duration : 1 HourREGISTER yourse...

Read More >

Bugzilla to TFS Migrator (Part 3 of 4) – Configuration

This is the third part of the Bugzilla to TFS Migrator blog series covering the configuration of the...

Read More >

Share

Comments

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

  • Recent
  • Popular
  • Tag
Monthly Archive
Subscribe
Name

Contact Us
  • *
  • *