Ansible
Ansible
Ansible
What is Ansible ?
Ansible is an open source automation and orchestration tool for software provisioning, configuration
management, and software deployment. Ansible can easily run and configure Unix-like systems as well
as Windows systems to provide infrastructure as code .
Why is Ansible ?
One of the most significant advantages of Ansible is that it is free to use by everyone.
It does not need any special system administrator skills to install and use Ansible
Its modularity regarding plugins, modules, inventories, and playbooks make Ansible the perfect
companion to orchestrate large environments .
Ansible is very lightweight and consistent, and no constraints regarding the operating system or
underlying hardware are present
It is also very secure due to its agentless capabilities and due to the use of OpenSSH security features .
Notifier -Section attributed to a task which calls a handler if the output is changed
[tomcat-nodes]
172.31.34.69
# Ex 2: A collection of hosts belonging to the 'webservers' group
## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110
Ansible Ad-hoc Commands (Note – If you run this command with local
user then add that user in sudoers file and start command syntax with
sudo command )
Syntax – sudo ansible <group of nodes/single node> -a “command
If you don’t want to use sudo command everytime then use –b option with a
Syntax – ansible <group of nodes/single node> -ba “command – it will execute without using sudo
➢ It is nothing but the simple linux commands to execute simple task on nodes at a time
➢ If we don’t know that which module is used to done some other work on nodes then we can use
ad-hoc commands
➢ Ad-hoc commands are that commands which can run individually to perform quick functions
➢ These are of one time usage only
➢ Ad-hoc commands Path - /usr/bin/ansible
➢ Ansible commands –
➢ Syntax – ansible <node group name/single node> <argument> “command”
➢ Actual command – ansible 192.168.0.54/all/group_of_node –a “touch file1.txt”
➢ (Note – -a is used to execute ahead command)
➢ It will create file on 192.168.0.54 this node
➢ If we want to perform a task on all nodes then here below is that command
➢ Command – ansible all –a “command”
➢ If we want to perform a task on particular node within group
➢ Syntax – ansible <name of nodes group> [no of node] –a “command”
➢ (Note – first node of group is consider as 0 and last node is consider as -1)
➢ Command – ansible demo[0] –a “command”
➢ If we want to perform within two group of node on particular nodes individually then go
through below command
➢ Syntax – ansible <group1>[node no]:<group2>[node no]
➢ Command – ansible demo[0]:tomcat_nodes[2] –ba “yum install httpd –y”
➢ (Above command will execute on first node of group demo and as well as 3rd node of group
tomcat_nodes)
➢ If we want to perform a task within group of nodes in range of nodes then below is the
command
➢ Syntax – ansible <group of nodes>[node-node] –ba “command”
➢ Command – ansible tomcat_nodes[0-3] –ba “yum install tree –y”
Ansible Modules – Ansible ships with the number of modules that can be executed directly
nodes or through playbook
There are no servers , deamons or database required
3) File – It is used to create directory or file / as well as changing ownership forcefully Parameter
used in file module (path , state , owner , group , mode , remote_src , recurse)
Syntax –
- name: Creating Directory Tomcat in /
file:
path: "{{path}}"
state: directory
owner: "{{user}}"
group: "{{user}}"
mode: "{{perm}}"
remote_src: yes
Changing ownership –
- name: Changing ownership recursive
file:
path: "{{path}}"
state: directory
owner: "{{user}}"
group: "{{user}}"
recurse: yes
4) get_url – It is used to download any package or file using url Parameter used with get_url
module as ( url , dest )
Syntax -
- name: Downloading Tomcat Tar
get_url:
url: "{{tomcat_url}}"
dest: /{{path}}
5) unarchive – it is used to untar file Parameter used with unarchive module as (src , dest ,
remote_src)
Syntax –
- name: Unarchiving apache tomcat
unarchive:
src: /tomcat/apache-tomcat-{{req_tomcat_ver}}.tar.gz
dest: /tomcat
remote_src: yes
6) copy – It is used to copy files from ansible engine or from target server to target servers if your
war file is present in your ansible server and you want to deploy on your nodes then simply give
path as src: ./{{dep_war}} because my current war file location is /ansible/playbook/sample.war
and I am already under this path that is cd /home/ansible/playbook that’s why I have mentioned
as src: ./{{dep_war}} ( It is the variable name which I add in vars/main.yml file - {{dep_war}}
Parameter used with copy module as ( src , dest , remote_src)
Syntax –
- name: deploying war file
copy:
src: ./{{dep_war}}
dest: /tomcat/apache-tomcat-{{req_tomcat_ver}}/webapps
remote_src: yes
(Note – here nohup parameter is used for to start service beacuase in case if you not use nohup
parameter with this module it wont be start your service because when you start your service only with
This /tomcat/apache-tomcat-{{req_tomcat_ver}}/bin/startup.sh service will start but after it will exit
that’s why we must have to mention nohup parameter with this to execute and to run in background
If you are working with role to complete any task then follow below
steps –
Install ansible Server which will do manage nodes within your environment do below steps on ansible
server and as well as your nodes
1) first of all we need to add our target server/target nodes in inventory file that is
/etc/ansible/hosts , you can assign group of nodes or single ip its upto you below are the demo
group with two nodes
vim /etc/ansible/hosts
[demo]
172.31.34.69
172.31.44.213
:wq
2) Second step we want to states ansible server that inventory file present there then we have to go
in configuration file and uncomment the inventory parameter as well as sudo user parameter
Because with the local user you cant do anything done on nodes
Vim /etc/ansible/ansible.cfg
# some basic default values...
inventory = /etc/ansible/hosts
#library = /usr/share/my_modules/
#module_utils = /usr/share/my_module_utils/
#remote_tmp = ~/.ansible/tmp
#local_tmp = ~/.ansible/tmp
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks =5
#poll_interval = 15
sudo_user = root
:wq
Once if it is done then Create user on both server and nodes and give password and root user privilege
to them
[root@ip] # useradd ansible
[root@ip] # passwd ansible
3) Third step we have to established ssh connection on ansible server as well nodes because
ansible is agentless and need not any agent or software to do work with nodes
First we need to go in ssh config file and and make changes with root user
[root@ip] # vim /etc/ssh/sshd_config
Do passwordAuthentication=yes
:wq
4) Once if it will done then ping your nodes with ansible server with simple ping command it will
pinged but while working with task we cant put password each and every time during task
execution that’s why we need to configure key-based authentication on server side as well as on
nodes
5) Now go with ansible user and do below steps to established ssh connection without giving
password each and every time (it is called trust relationship as well that is root-root , local-local)
[root@ip] # Su – ansible
[ansible@ip] $ ssh-keygen --- it will generate key-pair in current location like below
Now we have two keys that is private key and public key
Now we want to dispatch public key on all nodes which you will manage respectively go with below
command
[ansible@ip] $ ssh-copy-id ansible@172.31.34.69
o/p – public key will be gone on your node
6) Now take ssh of your nodes it wont be ask for password to connect with it
7) Now go to your ansible server and login with ansible user who will managed nodes
[ansible@ip] $ pwd
/home/ansible
Now go inside the playbook directory and create some other parent directories
[ansible@ip] $ cd playbook
[ansible@ip playbook] $ mkdir –p roles/tomcat/tasks
[ansible@ip playbook] $ mkdir roles/tomcat/vars
8) Now create a master.yml file which will call your created role to execute your task on nodes and
it will create parallely with roles directory
[ansible@ip playbook] $ touch master.yml
10) Now create variable file name main.yml under roles/tomcat/vars dir
[ansible@ip playbook] $ vim roles/tomcat/vars/main.yml
java_ver: java-1.8.0-openjdk.x86_64
user: tomcat
passwd: tomcat
shell: /bin/bash
path: /tomcat
perm: 0770
req_tomcat_ver: 8.5.65
tomcat_url: https://downloads.apache.org/tomcat/tomcat-{{ req_tomcat_ver.split('.')[0] }}/v{{
req_tomcat_ver }}/bin/apache-tomcat-8.5.65.tar.gz
dep_war: sample.war
--- # tomcat installation with deploying war file and start --> it is just heading of your playbook
- name: Updating Repo ---------------------> it is just message you can right here according to your task
yum: ----------------> module name
name: "*" ----------------> supported parameters
state: latest
- name: Installing Required Java
yum:
name: "{{java_ver}}"
state: latest
- name: Creating User Tomcat
user:
name: "{{user}}"
password: "{{passwd}}"
state: present
shell: "{{shell}}"
createhome: yes
home: /home/{{user}}
- name: Creating Directory Tomcat in /
file:
path: "{{path}}"
state: directory
owner: "{{user}}"
group: "{{user}}"
mode: "{{perm}}"
remote_src: yes
- name: Downloading Tomcat Tar
get_url:
url: "{{tomcat_url}}"
dest: /{{path}}
- name: Changing ownership recursive
file:
path: "{{path}}"
state: directory
owner: "{{user}}"
group: "{{user}}"
recurse: yes
- name: Unarchiving apache tomcat
unarchive:
src: /tomcat/apache-tomcat-{{req_tomcat_ver}}.tar.gz
dest: /tomcat
remote_src: yes
- name: deploying war file
copy:
src: ./{{dep_war}}
dest: /tomcat/apache-tomcat-{{req_tomcat_ver}}/webapps
remote_src: yes
- name: Starting Tomcat Service
shell: nohup /tomcat/apache-tomcat-{{req_tomcat_ver}}/bin/startup.sh
13) Now run the playbook with below command you will get output as given below
[ansible@server playbook]$ ansible-playbook master.yml
PLAY [demo]
*************************************************************************************
*********************************************
PLAY RECAP
*************************************************************************************
**********************************************
172.31.34.69 : ok=9 changed=9 unreachable=0 failed=0 skipped=0 rescued=0
ignored=0
172.31.44.213 : ok=9 changed=9 unreachable=0 failed=0 skipped=0 rescued=0
ignored=0
Now you have successfully install tomcat on your target nodes within some minutes without taking to
much time.
Thanks !