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
Comments
Post a Comment