DevOps Batch June 2023 – Ansible
Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
What is Ansible?
=========================
Config mgmt tool for servers
PLAN -> Dev -> Ana -> Build -> UT -> PACK -> Archive -> Dep2QA -> AT -> CC
Ansible
Config
Server(S)
release
Ansible -> CMD -> FREE
Tower - UI -> PAID - Support
AWX - UI - FREE - nO support
Python
From Redhat
==============================
Why Ansible?
===================================
Bash - Windows?
PS - Linux?
Python
Easy to learn, extend, test, debug, share
DSL
Ideompotent
1 ------- 1 mins
10 ------- 10 mins
===============================
3 changes - 3 mins
==============================================
Other - chef - pupppet - salt - cf
=======================================================
How ansible works?
==============================================
Human -> ACS -------> ARS
Linux any
ansible nR
--------------------
Linux -- SSH
Windows - WInrm
Python Linux - Python
Wndows - PS3.0 , DOTNET4.5
====================================================
What is the component in ACS
======================================
Ansible
Executables
Module - its a Python Code, which run in ARS with param providers
4000+ Modules + 1000 roles
Plugins - its a Python Code, which run in ACS & add a featute of Ansible
Config - a file which has param to override behaviours of Executables
Inventory
a file which contains a IP add of ARS
group
Playbook
a yaml file which contains
hosts: localhost OR group name mentioned in Inventory OR all
tasks:
module1 and its param
module2 and its param
module2 and its param
========================================================
COPY a file in 1 machine
----------------------
cmd script?
ansible adhoc command
============================
Project
Setup a webserver..
Psucode
Step 1 - Install apache2 server in ubuntu apt name=apache2 state=name
Step 2 - Copy index.html to /var/www/html/index.html copy src=index.html dest=/var/www/html/index.html
Step 3 - Start a Process of Apache2 service name=apache2 state=started
======================================
ansible localhost -m apt -a"name=apache2 state=present"
ansible localhost -m copy -a"src=index.html dest=/var/www/html/index.html"
ansible localhost -m service -a"name=apache2 state=started"
ansible all -i inventory -m apt -a"name=apache2 state=present" -u ubuntu --key-file=node.pem -b
ansible all -i inventory -m copy -a"src=index.html dest=/var/www/html/index.html" -u ubuntu --key-file=node.pem -b
ansible all -i inventory -m service -a"name=apache2 state=started" -u ubuntu --key-file=node.pem -b
ansible web -i inventory -m apt -a"name=apache2 state=present" -u ubuntu --key-file=node.pem -b
ansible web -i inventory -m copy -a"src=index.html dest=/var/www/html/index.html" -u ubuntu --key-file=node.pem -b
ansible web -i inventory -m service -a"name=apache2 state=started" -u ubuntu --key-file=node.pem -b
===================================================
How to write a playbook
Playbook
a yaml file which contains
hosts: web
tasks:
module1 and its param
module2 and its param
module2 and its param
---
- name: Update web servers
hosts: web
tasks:
- name: Install Apache in ubuntu
ansible.builtin.apt:
name: "apache2"
state: latest
- name: Copy index.html
ansible.builtin.copy:
src: index.html
dest: /var/www/html/index.html
- name: Starting a Apache Server
ansible.builtin.service:
name: "apache2"
state: started
ansible-playbook -i inventory web.yaml -u ubuntu --key-file=node.pem -b
==================================================================
VARS - DONE
Templates
vars in playbook - inter*
vars in file used in playbook - no inter*
Handlers
---
- name: Update web servers
hosts: web
vars:
myname: "Rajesh Kumar"
tasks:
- name: Install Apache in centos7
ansible.builtin.apt:
name: apache2
state: latest
- name: Copy index.html
ansible.builtin.copy:
src: index.html
dest: /var/www/html/index.html
- name: Template index.html
template:
src: index.html.j2
dest: /var/www/html/index-template.html
- name: Starting a Apache Server
ansible.builtin.service:
name: apache2
state: started
Handlers
---
- name: Update web servers
hosts: web
vars:
myname: "Rajesh Kumar"
httpport: 8090
tasks:
- name: Install Apache in ubuntu
ansible.builtin.apt:
name: apache2
state: latest
- name: Copy index.html
ansible.builtin.copy:
src: index.html
dest: /var/www/html/index.html
- name: Starting a Apache Server
ansible.builtin.service:
name: apache2
state: started
- name: Template for httpd.conf
template:
src: ports.conf.j2
dest: /etc/apache2/ports.conf
notify:
- ReStarting a Apache Server
handlers:
- name: ReStarting a Apache Server
ansible.builtin.service:
name: apache2
state: restarted
===========================================================================================================
ROLE
==============================================================
a dir sructure
-------------------------
for managing following
- tasks
- vars
- template
- file
- handlers
site.yaml
---
- name: Update web servers
hosts: web
roles:
- web