Skip to main content

Basics Of Ansible,A OpenSource Tech

What is Ansible?

Ansible is simple open source IT engine which automates application deployment, intra service orchestration, cloud provisioning and many other IT tools.

Ansible uses playbook to describe automation jobs, and playbook uses very simple language i.e. YAML (It’s a human-readable data serialization language & is commonly used for configuration files, but could be used in many applications where data is being stored)which is very easy for humans to understand, read and write. Hence the advantage is that even the IT infrastructure support guys can read and understand the playbook and debug if needed (YAML — It is in human readable form).

Ansible is designed for multi-tier deployment. Ansible does not manage one system at time, it models IT infrastructure by describing all of your systems are interrelated. Ansible is completely agentless which means Ansible works by connecting your nodes through ssh(by default). But if you want other method for connection like Kerberos, Ansible gives that option to you.

After connecting to your nodes, Ansible pushes small programs called as “Ansible Modules”. Ansible runs that modules on your nodes and removes them when finished. Ansible manages your inventory in simple text files (These are the hosts file). Ansible uses the hosts file where one can group the hosts and can control the actions on a specific group in the playbooks.

Installation and Configuration of Ansible

For installing ansible on RPM based OS, you can use yum or dnf on fedora to install it.

yum -y install ansible

For Debian based OS such as Ubuntu, you can use apt-get command to install ansible on your bare metal

apt-get install ansible

Check the version of ansible by using

ansible –version



Before we create a basic configuration, I want to take a moment to explain the Ansible file/folder structure. You’ll note that if you list the files/folders in /etc/ansible that you’re presented with the following. Alongside, I have included an explanation for each file or folder.

  • /etc/ansible — The main configuration folder which encompasses all Ansible config
  • /etc/ansible/hosts — This file holds information for the hosts/and host groups you will configure
  • /etc/ansible/ansible.cfg — The main configuration file for Ansible
  • /etc/ansible/roles — This folder allows you to create folders for each server role, web/app/db, etc.

In hosts file of ansible, you can mention the ip of all the systems that are connected to your localhost whether they are virtual machines or bare metal.

I have installed a centos7 virtual machine on my localhost virtual machine manager, which is a Fedora 28 environment as you can see in following picture



and I have mentioned the IP of this virtual machine in hosts file of ansible as

[vagrantians]

192.168.121.203

[vagrantians] is a group name for the all ip mentioned under it.

Ansible works by connecting to your nodes and pushing out small programs, called “Ansible modules” to them. Ansible then executes these modules (over SSH by default), and removes them when finished. Your library of modules can reside on any machine, and there are no servers, daemons, or databases required.

To configure ssh authorization for ansible,First generate the ssh keygen on your localhost by

ssh-keygen

Then you need to add id_rsa.pub (public key) of host machine to remote machine, For this you can use following command

ssh-copy-id root@192.168.121.203

If it doesn’t ask password then you need to edit a file located as /etc/ssh/sshd_config in your remote machine and make “PasswordAuthentication” value to “yes”.

After this, you can test the connection by running the command

ansible all -m ping

here all takes the IPs in /etc/ansible/hosts file, you can also create a inventory file for ansible to take the IPs from in your working directory with same syntax of hosts file.Then it can be mentioned in the command as follows with a sample command of ansible

ansible vagrantians -i inventory -m command -a ‘mkdir /root/Desktop/dir1’

This command takes the inventory file and IPs under the group vagrantians and makes a directory on their root Desktop.

Ansible Playbooks

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.

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)

Let’s take a example of following yaml file saved as test.yml for http hosting on remote machine

– name: hosting

hosts: all

become: yes

tasks:

– name: install

yum:

name: httpd

state: latest

– name: write

copy:

content:”testing for yaml’

dest: /var/www/html/index.html

– name: service enable

service:

name: httpd

enabled: true

state: started

– name: firewalld

firewalld:

service: http

permanent: true

state: enabled

immediate: yes

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.

You can run the file with following command

ansible-playbook -i inventory test.yml

You can learn more about ansible at

https://docs.ansible.com/https://docs.ansible.com/

Comments

Popular posts from this blog

Need of Open Source ?

You have already got what is  Open Source  from previous blogs. The simple reason is that why to pay if we get is free. Lets take an example you spend lots of money to buy an windows genuine some of the people don’t buy the licence copy of it but they buy an creak (DOS) version of windows but they have some problem in it. Some what same not a completely different OS are freely available in market then why should we pay for it ? You get high quality of software and hardware also in open source. They are very powerful and smooth running no lags get while working.They also give full support to solve your problem.Think an example of google you search on google and you get the result of what you search if google say that I want money for every search results then ?you will pay for it ? That the need of  open source .

The End Is near: January 19, 2038 3:14:07 GMT

  Year 2038 problem The Year 2038 problem is an issue for computing and data storage situations in which time values are stored or calculated as a signed 32-bit integer, and the number is interpreted as the number of seconds since 00:00:00 UTC on 1 January 1970 (the epoch). Systems working on 32-bit cannot encode times after 03:14:07 UTC on 19 January 2038, analogous to the Y2K problem , in which 2-digit values representing the number of years since 1900 could not encode the year 2000 or later. Most 32-bit Unix like systems store and manipulate time in this Unix time format, so the year 2038 problem is sometimes referred to as the  Unix Millennium  Bug by association. What is Unix time? The  Unix epoch   time  is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds . Literally speaking the epoch is Unix time 0 (midnight 1/1/1970), but ‘epoch’ is often used as a synonym for ‘Unix time’. Many Unix systems store epoch dates as a signe

What is Dynamic Programming ?

  What actually dynamic programming is ? In computer science or mathematics,  dynamic programming  is a method for solving a complex problem by breaking it down into a collection of simpler subproblems, solving each of those subproblems just once, and storing their solutions. Also known as  dynamic   optimization . Dynamic programming is similar to divide and conquer only difference is that dynamic programming is used when there is overlapping subproblem property and in divide and conquer there is no overlapping subproblem property. example, fibonacci series. When to use dynamic programming ? Following two attributes suggests that a problem can be solved using  dynamic programming  : optimal substructure overlapping sub-problems. Ways to of using dynamic programming : Top-Down :  Firstly, Start solving the given problem by breaking it down. If you see that the problem has been solved already, then just return the saved answer. If it has not been solved, solve it and save the answer. Th