Ansible
Ansible
WHY ANSIBLE:
Its do PUSH mechanism meaning create one config file and we can push to N number of
required systems to perform the task
Its AGENT LESS meaning it doesn’t have any agents to install in all servers to complete
the task, we just add the target machine IP’s on Inventory file that was present in the
ansible host server.
It’s connected to targeted machine using password less oauth .
Dynamic Inventory meaning if new target machine comes to the network ansible host
system have the capability to autodetect.
Its very easy to use and we can accomplish the tasks using playbooks
Playbooks are written in YAML language.(yet another markup language)
INITIAL SETUP:
For password less auth : To communicate the host system to tareget system
For server side: type the cmd : ssh-keygen
NOTE: passwords has been stored under /root/.ssh
Copy the idrsa-pub key and keep it ready with you so we need to paste in target
machines
On host server :
Ansible -i <to/the/path/inventory> all -m “required-module” -a “required cmd to pass
on target machines”
Once cmds executed check the target machine if fine is created or not.
Note: always create a ansible dir and keep the inventory file in the ansible dir
(recommended)
Default path for inventory in some cases are /etc/ansible/hosts/inventory (default)
Ansible-Modules:
Ping module:
Thid module is to check the connectivity between the your server and target
Playbook script:
---
- name: to check the ping connecvity
hosts: all
become: false
tasks:
- name: check connecvity using ping
ping:
o/p:
apt module:
This module will help to install update delete the packages/softwares
Our goal is to delete the nginix on target server
Install packages httpd and httpd-tools
Playbook script:
---
- name: uninstall nginx and install git and update the packages of httpd
hosts: all
tasks:
- name: uninstall nginx
apt:
name: nginx
state: absent
- name: install git
apt:
name: git
state: present
- name: check the version of git
shell: git --version
- name: update the packages of httpd and httpd-tools
apt:
name: "{{ packages }}"
vars:
packages:
- httpd
PACKAGE MODULE:
Package module using ansible.builtin.package:
manages packages on a target without specifying a package manager module (lyum, apt,
…).
This module acts as a proxy
Playbook Script:
---
- name: Install Packages using ansible.builtin.package on target system
hosts: all
tasks:
- name: Install the latest version of python3.8
ansible.builtin.package:
name:
- python3.8
state: present
command module : Command module is used to execute commands on a remote node. It is
used mostly to run simple Linux commands . The command(s) will not be processed through
the shell, so variables like $HOSTNAME and operations like “*”, “<”, “>”, “|”, “;” and “&” will
not work.
For Windows targets, use the win_command module instead. For windows based target
tasks:
- name: run hostname cmd
command: hostname
tasks:
- name: copy module is used to copy the file from local to remote host
copy:
src: /home/spingali/SitaRam
dest: /home/Sita
eg2:
- name: copy a file from local machine to remote machine with owner and permissions
copy:
src: files/src.txt
dest: /etc/dest.txt
owner: <Usr_name>
group: <grp_name>
mode: '0644’
fetch module:
fetch module can be used whenever we want to fetch a file from remote machine to a
local machine. Files are stored in local machine in a directory with the name of the
hostname.
Playbook syntax:
---
- name: fetch module
hosts: all
tasks:
- name: copy a file from remote machine to local machine
fetch:
src: /home/spingali/siva/matha
dest: /home/spingali
get_url module:
get_url module can be used to download files form HTTPS/HTTP/FTP servers. By default
this module uses the proxy configured for the node.
Custom proxy can be used by setting environment variable or by using use_proxy option.
Playbook syntax:
---
- name: install tomact using get-url module
hosts: all
tasks:
- name: Download the apache
get_url:
url: https://downloads.apache.org/tomcat/tomcat-8/v8.5.98/bin/apache-tomcat-
8.5.98-deployer.tar.gz
dest: /tmp
archive module:
archive module is used to create a compressed file package of the format of zip, tar, gz,
bz2 and xz.
By default it assumes that the source file you are trying to compress does exists and It
does not copy source file to target node before compressing.
Playbook Script:
---
- name: archive
hosts: all
tasks:
- name: archive the file
archive:
path: /home/spingali/siva
dest: /home/spingali/siva.tgz
unarchive module:
unarchive module is used to unpacks an archive file such as tar, gz, zip.
It can also copy the file to the remote server before uncompressing them.
The module use unzip and tar -xzf command to unpack the compressed file so these
commands should be installed on target nodes.
For windows node win_unzip can be used.
Playbook syntax:
---
- name: unarchive module
hosts: all
tasks:
- name: Extract foo.tgz into /var/lib/foo
unarchive:
src: foo.tgz
dest: /var/lib/foo
file module:
file module is responsible for performing tasks such as creating files and directories,
deleting files and directories, creating soft and hard symbolic links, adding and modifying
file and directory permissions, and more. For windows machine you could use win_file
module.
Playbook Syntax:
---
- name: filemodule
hosts: all
tasks:
- name: create the file
file:
path: /home/spingali/feb13.txt
state: touch
template module:
A template in Ansible is a file which contains all your configuration parameters, but the
dynamic values are given as variables. During the playbook execution, depending on the
conditions like which cluster you are using, the variables will be replaced with the
relevant values with the help of Jinj2 templating engine. The template files will usually
have the .j2 extension
Playbook Syntax:
---
- name: template module
hosts: all
tasks:
- name: Template a file to /etc/file.conf
template:
src: /mytemplates/foo.j2
dest: /etc/file.conf
owner: <main-user to run the config>
group: <grp-name>
mode: '0755'
find module:
find module functions as same as the Linux Find command and helps to find files and
directories based on various search criteria.
For windows, you should use a win_find module instead.
Playbook Syntax:
---
- name: find module
hosts: all
tasks:
- name: find the file
find:
paths: /home/spingali/siva
patterns: "*"
recurse: yes
- name: Recursively find /tmp files older than 4 weeks and equal or greater than 1
megabyte
find:
paths: /tmp
age: 4w
size: 1m
recurse: yes
- name: Recursively find /var/tmp files with last access time greater than 3600 seconds
find:
paths: /var/tmp
age: 3600
age_stamp: atime
recurse: yes
replace module:
replace module is used to replace all the instances of a matching string in a file. It also
supports regular expression and can also create backup of a file before replacing.
Playbook Syntax
---
- name: replace mdoule
hosts: all
tasks:
- name: replace module in target with backup
replace:
path: /home/spingali/feb13.txt
regexp: 'santosh'
replace: 'siva'
backup: yes
lineinfile module:
lineinfile module is helpful when you want to add, remove, modify a single line in a file.
You can also use conditions to match the line before modifying or removing using the
regular expressions.
You can reuse and modify the matched line using the back reference parameter.
you can also use insertafter and insertbefore attribute to make changes at specified
portion of the file
Syntax:
---
- name: lineinfile module
hosts: all
tasks:
- name: adding a line
lineinfile:
path: /etc/selinux/config
regexp: '^SELINUX='
line: SELINUX=enforcing
Syntax:
---
- name: service module
hosts: all
tasks:
- name: Start service httpd, if not started
service:
name: httpd
state: started