Ansible Training Notes 14 Oct 2023
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
--------
Server(S)
Release
cli - cmd - free - os
tower - web - paid -
awx - web - free but no support
From Redhat
Written in python
Config
file action - cud
dir
package i r u
service e s s r d
user a e d
group
yum i r
apt
cmd cmd
ps
bash cmd
=================================================================
Why We need Ansible?
==============================================
You can deploy 100 of changes in the 100s of server parrell.
IDEOMPOTENT -
1st ----> 10 Steps ------> 1 mins ---------> 10 mins
1st ----> 3 Steps ------> 1 mins ---------> 3 mins
SHELL
WINDOWS
PS
LINUX
PYTHON
10 days
Ansible -
Few Hours
DSL
Others tools - Chef - Puppet - Salt - Cfengine
=============================================================
Udeploy - Octoupus Deploy
How Ansible works!!!!
============================================================
YOU -------> ACS ------------------> ARS
Linux Anything
64 b anything
ansible not required
-----------------SSH -------> Linux
-----------------WINRM ------> WINDOWS
python linux - python
win - dotnet + ps3
==============================================================
Components of ACS
---------------------------------------
Ansible
- executables
- plugins - Its python code - Which run in ACS to add more feature
- modules - Its python/PS code - which run in ARS 5000
eg. copy - required param
- Ansible Config
/etc/ansible/ansible.cfg
Inventory
Playbook
===============================================================
ACS
65.2.170.81
ARS
13.126.189.32
3.108.193.197
How to install Ansible?
https://www.devopsschool.com/blog/ansible-installation-and-configuration-guide/
======================================================
Ansible Adhoc Commands
Ansible Inventory
----------------------------------
- List of IPADD of ARS
- CMD
- file
Ansible Playbook
Ansible Vars
Ansible Role
=======================================================
PROJECT
- Setup a web server
Psucode - UBUNTU
install apache
copy a web package
start a server
- do it manually
Ansible Module for Install package in UBUNTU apt
state
Ansible Module for copy a file package - copy
state
Ansible Module for starting a service
state
ansible localhost -m apt -a"name=apache2 state=latest"
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 13.126.189.32,3.108.193.197, -m apt -a"name=apache2 state=latest update_cache=yes" -u ubuntu --key-file=node.pem -b
ansible all -i 13.126.189.32,3.108.193.197, -m copy -a"src=index.html dest=/var/www/html/index.html" -u ubuntu --key-file=node.pem -b
ansible all -i 13.126.189.32,3.108.193.197, -m service -a"name=apache2 state=started" -u ubuntu --key-file=node.pem -b
[defaults]
# uncomment this to disable SSH key host checking
# host_key_checking = False SSH key host checking
host_key_checking = False
ansible all -i 13.126.189.32,3.108.193.197,3.108.193.197,3.108.193.197,3.108.193.197,3.108.193.197,3.108.193.197,3.108.193.197,3.108.193.197,3.108.193.197,3.108.193.197, -m apt -a"name=apache2 state=latest update_cache=yes" -u ubuntu --key-file=node.pem -b
ansible all -i 13.126.189.32,3.108.193.197, -m copy -a"src=index.html dest=/var/www/html/index.html" -u ubuntu --key-file=node.pem -b
ansible all -i 13.126.189.32,3.108.193.197, -m service -a"name=apache2 state=started" -u ubuntu --key-file=node.pem -b
inventory
13.126.189.32
3.108.193.197
ansible all -i inventory -m apt -a"name=apache2 state=latest update_cache=yes" -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
inventory
[web]
13.126.189.32
3.108.193.197
[db]
13.126.189.35
3.108.193.133
ansible web -i inventory -m apt -a"name=apache2 state=latest update_cache=yes" -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
ansible web -i inventory -m apt -a"name=apache2 state=latest update_cache=yes" -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
ansible web -i inventory -m apt -a"name=apache2 state=latest update_cache=yes" -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
ansible web -i inventory -m apt -a"name=apache2 state=latest update_cache=yes" -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
ansible web -i inventory -m apt -a"name=apache2 state=latest update_cache=yes" -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
ansible web -i inventory -m apt -a"name=apache2 state=latest update_cache=yes" -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
ansible web -i inventory -m apt -a"name=apache2 state=latest update_cache=yes" -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
ansible web -i inventory -m apt -a"name=apache2 state=latest update_cache=yes" -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
ansible web -i inventory -m apt -a"name=apache2 state=latest update_cache=yes" -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
ansible web -i inventory -m apt -a"name=apache2 state=latest update_cache=yes" -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
TOO MANY TASK
VARS
Cond
==========================================
What is Playbook?
YAML FILE
Collection of play
What is play?
Hosts: localhost OR groupname
Tasks:
Module 1 & Param
Module 2 & Param
Module 3 & 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
- name: install git
apt:
name: git
update_cache: yes
ansible-playbook -i inventory web.yaml -u ubuntu --key-file=node.pem -b
==================================================================
Variable
-----------------------------
---
- name: Update web servers
hosts: web
vars:
myname: "Rajeshkumar"
age: "18"
packagename: "apache2"
servicename: "apache2"
vars_files:
- "vars.yaml"
vars_prompt:
- name: "version"
prompt: "Which version Do you want to install?"
private: no
tasks:
- name: Install Apache in ubuntu
ansible.builtin.apt:
name: "{{ packagename }}"
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: "{{ servicename }}"
state: started
- name: Print return information from the previous task
ansible.builtin.debug:
var: myname
- name: Print return information from the previous task
ansible.builtin.debug:
msg: "My Name is {{ myname }} and My age is {{ age }}"
- name: Print return information from the previous task
ansible.builtin.debug:
var: version
- name: Ansible register variable basic example
shell: "find *.txt"
args:
chdir: "/root/ansible"
register: find_output
- debug:
var: find_output
- debug:
var: find_output.stdout_lines
- debug:
var: find_output.stdout_lines[0]
=====================================================================
ROLES
=======================================
TOO MANY playbook
TOO many vars
TOO many files would be used
TOO many Template would be used
TOO many Handler would be used
role
directory stru
---
- name: Update web servers
hosts: web
roles:
web
ansible-playbook -i inventory role.yaml -u ubuntu --key-file=node.pem -b