Ansible Is Simple Open Source IT Engine Which Automates Application Deployment
Ansible Is Simple Open Source IT Engine Which Automates Application Deployment
[abc-node]
#server1 ansible_host = <target machine for DU deployment>
ansible_user = <Ansible
user> ansible_connection = ssh
server1 ansible_host = <your host name> ansible_user = <your unix
user>
ansible_connection = ssh
[def-node]
#server2 ansible_host = <target machine for artifact upload>
ansible_user = <Ansible user> ansible_connection = ssh
server2 ansible_host = <host> ansible_user = <user>
ansible_connection = ssh
What is Configuration Management
Configuration management in terms of Ansible means that it maintains configuration of
the product performance by keeping a record and updating detailed information which
describes an enterprise’s hardware and software.
Such information typically includes the exact versions and updates that have been
applied to installed software packages and the locations and network addresses of
hardware devices. For e.g. If you want to install the new version
of WebLogic/WebSphere server on all of the machines present in your enterprise, it is
not feasible for you to manually go and update each and every machine.
You can install WebLogic/WebSphere in one go on all of your machines with Ansible
playbooks and inventory written in the most simple way. All you have to do is list out
the IP addresses of your nodes in the inventory and write a playbook to install
WebLogic/WebSphere. Run the playbook from your control machine & it will be
installed on all your nodes.
Ansible can be run from any machine with Python 2 (versions 2.6 or 2.7) or Python 3
(versions 3.5 and higher) installed.
Note − Windows does not support control machine.
By default, Ansible uses ssh to manage remote machine.
Ansible does not add any database. It does not require any daemons to start or keep it
running. While managing remote machines, Ansible does not leave any software
installed or running on them. Hence, there is no question of how to upgrade it when
moving to a new version.
Ansible can be installed on control machine which have above mentioned requirements
in different ways. You can install the latest release through Apt, yum, pkg, pip,
OpenCSW, pacman, etc.
For installing Ansible you have to configure PPA on your machine. For this, you have
to run the following line of code −
$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo apt-add-repository ppa:ansible/ansible $ sudo apt-get update
$ sudo apt-get install ansible
After running the above line of code, you are ready to manage remote machines
through Ansible. Just run Ansible–version to check the version and just to check
whether Ansible was installed properly or not.
Ansible uses YAML syntax for expressing Ansible playbooks. This chapter provides an
overview of YAML. Ansible uses YAML because it is very easy for humans to
understand, read and write when compared to other data formats like XML and JSON.
Every YAML file optionally starts with “---” and ends with “...”.
Understanding YAML
In this section, we will learn the different ways in which the YAML data is represented.
key-value pair
YAML uses simple key-value pair to represent the data. The dictionary is represented
in key: value pair.
Note − There should be space between : and value.
Abbreviation
Example
James: {name: james john, rollNo: 34, div: B, sex: male}
Representing List
We can also represent List in YAML. Every element(member) of list should be written
in a new line with same indentation starting with “- “ (- and space).
Example
---
countries:
- America
- China
- Canada
- Iceland
…
Abbreviation
Example
Countries: [‘America’, ‘China’, ‘Canada’, ‘Iceland’]
Example
---
james:
name: james john
rollNo: 34
div: B
sex: male
likes:
- maths
- physics
- english
…
List of Dictionaries
Example
---
- james:
name: james john
rollNo: 34
div: B
sex: male
likes:
- maths
- physics
- english
- robert:
name: robert richardson
rollNo: 53
div: B
sex: male
likes:
- biology
- chemistry
…
YAML uses “|” to include newlines while showing multiple lines and “>” to suppress
newlines while showing multiple lines. Due to this we can read and edit large lines. In
both the cases intendentation will be ignored.
We can also represent Boolean (True/false) values in YAML. where boolean values
can be case insensitive.
Example
---
- james:
name: james john
rollNo: 34
div: B
sex: male
likes:
- maths
- physics
- english
result:
maths: 87
chemistry: 45
biology: 56
physics: 70
english: 80
passed: TRUE
messageIncludeNewLines: |
Congratulation!!
You passed with 79%
messageExcludeNewLines: >
Congratulation!!
You passed with 79%
File Transfer
You can use the Ad-hoc commands for doing SCP (Secure Copy Protocol) lots of files
in parallel on multiple machines.
Managing Packages
The Ad-hoc commands are available for yum and apt. Following are some Ad-hoc
commands using yum.
The following command checks if yum package is installed or not, but does not update
it.
$ Ansible abc -m yum -a "name = demo-tomcat-1 state = present"
The following command check the package is not installed.
$ Ansible abc -m yum -a "name = demo-tomcat-1 state = absent"
The following command checks the latest version of package is installed.
$ Ansible abc -m yum -a "name = demo-tomcat-1 state = latest"
Gathering Facts
Facts can be used for implementing conditional statements in playbook. You can find
adhoc information of all your facts through the following Ad-hoc command −
$ Ansible all -m setup
Playbooks are the files where Ansible code is written. Playbooks are written in YAML
format. YAML stands for Yet Another Markup Language. Playbooks are one of the
core features of Ansible and tell Ansible what to execute. They are like a to-do list for
Ansible that contains a list of tasks.
Playbooks contain the steps which the user wants to execute on a particular machine.
Playbooks are run sequentially. Playbooks are the building blocks for all the use cases
of Ansible.
Playbook Structure
Each playbook is an aggregation of one or more plays in it. Playbooks are structured
using Plays. There can be more than one play inside a playbook.
The function of a play is to map a set of instructions defined against a particular host.
YAML is a strict typed language; so, extra care needs to be taken while writing the
YAML files. There are different YAML editors but we will prefer to use a simple editor
like notepad++. Just open notepad++ and copy and paste the below yaml and change
the language to YAML (Language → YAML).
A YAML starts with --- (3 hyphens)
Create a Playbook
Let us start by writing a sample YAML file. We will walk through each section written in
a yaml file.
---
name: install and configure DB
hosts: testServer
become: yes
vars:
oracle_db_port_value : 1521
tasks:
-name: Install the Oracle DB
yum: <code to install the DB>
The above is a sample Playbook where we are trying to cover the basic syntax of a
playbook. Save the above content in a file as test.yml. A YAML syntax needs to follow
the correct indentation and one needs to be a little careful while writing the syntax.
The Different YAML Tags
Let us now go through the different YAML tags. The different tags are described below
−
name
This tag specifies the name of the Ansible playbook. As in what this playbook will be
doing. Any logical name can be given to the playbook.
hosts
This tag specifies the lists of hosts or host group against which we want to run the task.
The hosts field/tag is mandatory. It tells Ansible on which hosts to run the listed tasks.
The tasks can be run on the same machine or on a remote machine. One can run the
tasks on multiple machines and hence hosts tag can have a group of hosts’ entry as
well.
vars
Vars tag lets you define the variables which you can use in your playbook. Usage is
similar to variables in any programming language.
tasks
All playbooks should contain tasks or a list of tasks to be executed. Tasks are a list of
actions one needs to perform. A tasks field contains the name of the task. This works
as the help text for the user. It is not mandatory but proves useful in debugging the
playbook. Each task internally links to a piece of code called a module. A module that
should be executed, and arguments that are required for the module you want to
execute.