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

Threading

Multi-taskingIt is a concept of performing multiple tasks over a certain period of time by executing...

Read More >

Microsoft Dynamics NAV Control & recognizes add-ins

NAV fill in assembly list table with these 3 fields and when NAV looks for dll it uses these fields ...

Read More >

How to use TFS 2012 Team Build for Visual Basic 6

Here is a process to build your VB6 projects in Team foundation Server 2012. 1. Have a copy of ...

Read More >

Bugzilla - TFS Migrator

Bugzilla to TFS 2013 MigratorSeamless, Stress-free and Simplified Migrations using CDMT “Canar...

Read More >

Converting UserControl into a dll

User controls are containers into which you can put markup and Web server controls. You can then tre...

Read More >

Webinar - Streamline Software Development Using Microsoft ALM

Streamline Software Development Using Microsoft ALMThe webinar covers the advantages of using the Mi...

Read More >

How to Create an Azure Virtual Network by using a Deployment Template

Hello Folks!In this Blog post, we will try to learn how to create an Azure V-Net using an ARM templa...

Read More >

Microsoft Azure : Running Linux Virtual Machine

Creating an Azure VM that runs Linux is easy to do. We will use ssh public key file to secure our&nb...

Read More >

Token Based Authentication for Web API's

Securing ASP.NET Web API using Custom Token Based AuthenticationProviding a security to the Web API&...

Read More >

Code Analysis & Code Metrics Integration in Azure Pipelines

This documentation shows the tutorial on how to integrate Visual Studio Code Analysis and Code Metri...

Read More >

Share

Comments

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

  • Recent
  • Popular
  • Tag
Tags
Monthly Archive
Subscribe
Name

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