ANSIBLE
In this section, we will walkthrough the W(s) & H of Ansible,
understanding the basics and demonstration of practical
implementation with various modules.
WHAT is Ansible? Ansible is a task execution engine, which defines a simple automation language for dramatically improving the scalabilty, consistency, reliability of a network or IT infrastructure. Funded and developed under the umbrella of Redhat Enterprise, it is an open-source tool/ software/language/system (..and may be magic) which has main utility of configuration management, sever/hosts configuration, continuous integration, and orchestration.
WHY Ansible?
- Scalable network operations: Ansible is designed to specially designed in order to scale its functionality in accordance to work with small as we large fleet of devices in an infrastructure.
- Intact Simplicity: To encourage the ease of understanding and human readability, YAML format files are used to support declarative configuration feature.
- LAN as well as Remote management: One of the most important feature of ansible is its capability to manage the remote networks.
- Supports Infrastructure, Network, Cloud, Containers: Ansible can automate most of the contemporary infrastructure models from bare metal to virtualization
ANSIBLE INSTALLATION
Ansible uses SSH protocol for management of the machines.
On installation, Ansible does not adds any database, or starts any
background process. The control machine is installed with
ansible, and then it is used to further control other machines
(referred as, Nodes) on the same LAN or on remote newtork.
NOTE: Windows is not used as a Control Machine. Ansible official
documentation page quotes, "Running Ansible from a Windows control
machine directly is not a goal of the project. Refrain from asking
for this feature, as it limits what technologies, features, and code
we can use in the main project in the future."
REQUIREMENTS AND DEPENDENCIES
Control machine
Basic requirement of a control machine is Python 2 (2.6 or 2.7) or
Python 3 (3.5 or higher). However, only the latest version of Ansible
support Python 3, it is still advisable to run Python 2 for the
beginners.
Nodes
Python 2 (2.6 or 2.7) should be up and running on node machines to be
managed.
SSH, SFTP, SCP are few protocols which can be used as mode of
communictaion to manage the nodes. By default SFTP is used, however,
it is advisable to switch to SSH.
UBUNTU installation
$ 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
MAC installation 'pip' is used for easy installation, but it isn't available on OSX by default. So, we can use python's package manager easy_install to install pip.
$sudo easy_install pip
Post pip installation, ansible can be very easy installed on an OSX
$ sudo pip install ansible $ sudo pip install ansible --upgrade
On OSX Maverick, compiler might create some issue, which can be easily dealt with following command.
$ sudo CFLAGS=-Qunused-arguments CPPFLAGS=-Qunused-arguments pip install ansible
Essentials 1: PLAYS and PLAYBOOK
A 'Play' can be simply defined as set of rules which map host or
servers in concern to a task or activity. A play basically is used to
call for for an ansible module. A playbook is simply defined as a set
of multiple plays in a list.
As a formal definition, "Playbooks are Ansible’s configuration,
deployment, and orchestration language. They can describe a policy
you want your remote systems to enforce, or a set of steps in a
general IT process. Ansible playbooks are a way to send commands to
remote computers in a scripted way. Instead of using Ansible commands
individually to remotely configure computers from the command line,
you can configure entire complex environments by passing a script to
one or more systems."[1]
As mentioned in previous section, playbooks are written in YAML, with
help of minimum syntaxes and rules. The main intent is to make sure
that Ansible is least related to any scripting or programming
language.
EXAMPLE:
--- - name: install ansible with pip - hosts: hostA tasks: -name: ansible pip pip: name: ansible virtualenv: ~/ansible_folder
In this example, we have used pip module to install latest ansible
package on the hostA in a particular python virtual environment named
as 'ansible_folder'.
Now let us break each step in the above playbook to understand the
basics of the play.
INVENTORY and HOSTS
An inventory can be defined as a basic unit of the Ansible, which
define the group of hosts on which task(s) are executed. In simplest
form, it can be a put together as IPs of hostnames. When the number
of these IP addresses exapand, it is advised to contain them into
numbers of groups.
In the above example, we have not defined any inventory but have
simply mentioned a single host 'hostA' as the target for the task.
- hosts: hostA
VARIABLES
Variables in any script or programming language provide flexibility
to store and iterate through various values or information.
Similary, in ansible, the variables play an important for evaluating
system values or strings used in various formats.
Variable could use letters, numbers, and underscores. However, a
variables must have a letter as its first character.
'foo_port' or 'foo123' are acceptable variable names. However,
'foo-port, foo port, foo.port and 12' are invalid variable names.
In a playbook, variables can be defined as
- hosts: webservers vars: http_port: 80
Using Variables in Conditions
Variables can be used as conditions, thus ensuring that certain tasks
are only run when for example on a given host the requested variable
is set to a certain value:
tasks: - name: install Apache on Solaris pkg5: name=web/server/apache-24 when: ansible_os_family == "Solaris" - name: install Apache on RHEL yum: name=httpd when: ansible_os_family == "RedHat"
In the above example, the task 1 acts when variable value is equal to
"Solaris" and task 2 executes when variable is equal to "Redhat"
TASKS
Ansible uses plays to define the certain task which are executed
serially againt the hosts or machine names defined as individual or in
groups. This basically depends on the mapping of a certain task with
the defined group of hosts as mentioned in previous section.
Errors generated or logged due to failiure of tasks are simply missed
for the whole playbook and later the reason for failed execution can
be simply edited or removed from the playbook, followed running the
play again.
The main aim of a task is to execute an ansible module along with
arguments as needed, here used-defined variables are used as arguments.
In the main example, we have described a simple task of installing
latest version of ansible on a host with ansible's pip module. Here,
we have not only given a command to pip ansible to the host but also
to install it in a seperate virtual environment folder.
tasks: -name: ansible pip pip: name: ansible virtualenv: ~/ansible_folder
MODULES
Ansible is majorly all about running a module in a correct way on a
list of hosts or inventory. Another major part of successful
implementation is also dependent on "Modules (also referred to as “task plugins” or “library
plugins”) are the ones that do the actual work in ansible, they are
what gets executed in each playbook task. But you can also run a single
one using the ‘ansible’ command."[3]
One of the most common way of using a module and passing arguments in
YAML is defined below as an example
- name: restart webserver service: name: httpd state: restarted
The data returned by Ansible module results can be stored into a variable, as well as, it can be produced as text output results on the screen
This is the end of part 1 of Ansible basics tutorial, in the next
section of the tutorial we will only discuss various ansible modules,
working, and examples. The best way to learn is to dig into practice,
thus, it is highly advisable to implement the scripts and examples in
the next section to understand the working and improve the ability to
debug the common issues faced during Ansible implementation.
ANSIBLE PART 2
REFERENCES:
1. https://docs.microsoft.com/sl-si/azure/ansible/ansible-overview
2. docs.ansible.com/ansible
3. http://docs.ansible.com/ansible/2.4/modules_intro.html
4. http://docs.w3cub.com/ansible/common_return_values/
5. docs.ansible.com/ansible
6. nsrc.org/workshops/2015/rwnog/raw-attachment/wiki/Track2Agenda/
first-playbook.htm
7. www.digitalocean.com/community/tutorials/how-to-create-ansible-
playbooks-to-automate-system-configuration-on-ubuntu
8. https://www.lynda.com/Ansible-tutorials/
9. http://docs.w3cub.com/ansible/playbooks_variables/