0% found this document useful (0 votes)
26 views

Ansible

Uploaded by

srassanto2022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Ansible

Uploaded by

srassanto2022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

ANSIBLE:

 Ansible is a config management tool


 We can create the resources or if we want to do a task on multiple systems, we can use this
to accomplish the task.
 RED HAT team has taken this and they are updating the modules.
 Its written in python

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)

Some Disadvantages are:


 Debugging is very tough to understand
 If the task or if our target machine count has high number there is some performance
issue will occur in some cases
 For Linux its very goof but for windows targets having small issue sometimes

HOW TO INSTALL ANSIBLE: [ In Linux]


 Update the system using CMD: sudo apt-get update
 To install the ansible CMD: sudo apt-get install ansible
 Current Ansible version is: 3.10.12

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

For client/target side: type the cmd : ssh-keygen


NOTE: passwords has been stored under /root/.ssh
 Paste the host public key under /root/.ssh/autheriosed_keys and save
 Try ssh <client/target-machine IP > on ansible host
ANSIBLE ADOC CMDS:
 We can use this a: doc cmds to do small small task like to create the file check the disk space
on target machine like that task we can use
 Note: Its not for big tasks
 For Big tasks always use playbooks

Eg : I want to create the file using adhoc cmd in ansible

 On host server :
Ansible -i <to/the/path/inventory> all -m “required-module” -a “required cmd to pass
on target machines”

Cmd : ansible -i inventory all -m “shell” -a “/etc/touch new_file.txt”


-m : module
-a : arguments

 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:

 To accomplish the long tasks we can use the modules

NOTE: How to run the playbooks?

 Always run the playbook using below cmd format:

CMD: ansible-playbook -vvv -i inventory <Playnbook> .yml

V meaning vabrose(debug logs) -i means interactive mode

SOME IMP MODULES WHICH WE ARE USING DAILY USE:

 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:

 Default is pong. This represents the return value on success

 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

win_command can be used in place of command module.


 Playbook syntax:
---
- name: check the hostname and create the dir and check the dir path
hosts: all

tasks:
- name: run hostname cmd
command: hostname

- name: create a dir using mkdir


command: mkdir /home/spingali/HANUMA

- name: check the present working dir using pwd cmd


command: pwd
 raw module
 Raw module is used to executes a low-down and dirty SSH command, It is s useful and
should only be done in a few cases. A common case is installing python on a system
which doesnot have python.
 using shell or command module is much more appropriate, Arguments given to raw are
run directly through the configured remote shell.
 Playbook syntax
---
- name: raw module usage
hosts: all
become: yes
- name: Run a command that uses non-posix shell-isms (in this example /bin/sh doesn't
handle redirection and wildcards together but bash does)
raw: cat < /tmp/*txt
args:
executable: /bin/bash
 copy module:
 copy module can be used to copy a file from local/remote machine to remote machine.
 For windows machine you can use win_copy module.
 Playbook Syntax:
---
- name: copy module
hosts: localhost

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

Eg2: using arguments


---
- name: fetch module
hosts: all
tasks:
- name: copy a file from remote machine to local machine without parent folder
structure.
fetch:
src: /var/log/access.log
dest: /var/{{ inventory_hostname }}/
flat: true
Flat = means Allows you to override the default behaviour of appending
hostname/path/to/file to the destination.

 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

- name: create the dir


file:
path: /home/spingali/2024
state: directory

- name: task is to del


file:
path : /home/spingali/siva.tgz
state: absent

 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

Some other playbook examples for find module


Eg1
---
- name: find module
hosts: all
tasks:
- name: Recursively find /tmp files older than 2 days
find:
paths: /tmp
age: 2d
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

- name: deleting a line


lineinfile:
path: /etc/sudoers
state: absent
regexp: '^%wheel'

- name: Replacing a line


lineinfile:
path: /etc/hosts
regexp: '^127\.0\.0\.1'
line: 127.0.0.1 localhost
- name: replace a line only after a specified string
lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^Listen '
insertafter: '^#Listen '
line: Listen 8080
 service module:

 service module is used to control service on target nodes,


 we can start/stop/restart/reload a service through this module.
 For windows based target machine you can use win_service module.

Syntax:
---
- name: service module
hosts: all
tasks:
- name: Start service httpd, if not started
service:
name: httpd
state: started

- name: Stop service httpd, if started


service:
name: httpd
state: stopped

- name: Restart service httpd, in all cases


service:
name: httpd
state: restarted

- name: Reload service httpd, in all cases


service:
name: httpd
state: reloaded

You might also like