Ansible Learning Notes
Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
DAY1
What is Ansible
---------------------------------
Deployment tool
Config mgmt tool
tool
save cost
save time
imp qual
mgmt
SERVER(S)
Config
Config of Server
file
dir
user
apt
yum
services
bash
Python
Release
ansible - cmd
tower - web
awx - web
from Redhat
Why Ansible?
platform inde*
Ability to run Script parr* in 1000 of server(S)
IDEOMPOTENT
Easy
to read
write
debug
share
learn
Others
Chef puppet ansible salt cfegine
====================================================================
How to Works? Arch*
==========================================================
Human --> ACS --------> ARS(S)
========================================================
linux Any
Ansible NA
--------- Linux -- SSH
--------- Windows -- Winrm / HTTP
python3 Linux - python
win - dotnet + ps3
54.89.178.94
Components of Ansible
--------------------------------
Ansible
Executable(S)
Modules - Python code --> RUN IN ARS - Copy delete apt script
https://docs.ansible.com/ansible/2.9/modules/modules_by_category.html
https://docs.ansible.com/ansible/2.9/modules/list_of_all_modules.html
Plugins - Python code --> RUN IN ACS - Adding extra fea of Ansible
Configfile - /etc/ansible/ansible.cfg
https://gist.github.com/alivx/2a4ca3e577ead4bd38d247c258e6513b
Inventory | hostfile
file
contains IPAdd(s) of ARS
10.1.1.1
10.22.2.2
222.33.3.3
4.5.5.5
group - all(built-in)
[web]
10.1.1.1
10.22.2.2
[db]
222.33.3.3
4.5.5.5
playbook
yaml file
contains play
play
hosts: GROUP of Inventory | all | web | db
tasks:
- module copy n Its param
- module apt n Its param
- module service n Its param
ansible-playbook -i inventory web.yaml
============================================================
ADHOC
---------------
Project
write a ansible automation to deploy webserver.
Step 1 - Find out ARS is linux or windows-- linux | ubuntu | apache
Step 2 - find a module which would install apache in ubuntu - apt
- https://docs.ansible.com/ansible/latest/collections/ansible/builtin/apt_module.html
Step 3 - find a module which copy software into /var/lib/www/ - copy
- https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html
Step 4 - find a module which which would start apache services - services
- https://docs.ansible.com/ansible/latest/collections/ansible/builtin/service_module.html
ansible localhost -m apt -a"name=apache2 state=latest"
ansible localhost -m copy -a"src=index.html dest=/var/www/html"
ansible localhost -m service -a"name=apache2 state=started"
[web]
54.89.178.94
34.239.106.240
10.1.1.1
10.22.2.2
[db]
222.33.3.3
4.5.5.5
ansible web -i inventory -m apt -a"name=apache2 state=latest"
ansible web -i inventory -m copy -a"src=index.html dest=/var/www/html"
ansible web -i inventory -m service -a"name=apache2 state=started"
root@ip-172-31-87-83:~# ansible web -i inventory -m apt -a"name=apache2 state=latest"
The authenticity of host '54.89.178.94 (54.89.178.94)' can't be established.
ED25519 key fingerprint is SHA256:/bb5HKInMiqK9Gg4+HAIpXvL1so/gMPk5YXHBOw7960.
This key is not known by any other names
The authenticity of host '34.239.106.240 (34.239.106.240)' can't be established.
ED25519 key fingerprint is SHA256:n1JRfvbTbzn5irTGzoH9UOVp0gAPLz+IyxsR55PsiSk.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
54.89.178.94 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Warning: Permanently added '54.89.178.94' (ED25519) to the list of known hosts.\r\nroot@54.89.178.94: Permission denied (publickey).",
"unreachable": true
}
10.1.1.1 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 10.1.1.1 port 22: Connection timed out",
"unreachable": true
}
10.22.2.2 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 10.22.2.2 port 22: Connection timed out",
"unreachable": true
}
34.239.106.240 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Host key verification failed.",
"unreachable": true
ansible web -i inventory -m apt -a"name=apache2 state=latest" -u ubuntu --key-file=node.pem -b
ansible web -i inventory -m copy -a"src=index.html dest=/var/www/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
Day 2
=======================================================
PLAYBOOK
playbook
yaml file
contains play
play
hosts: GROUP of Inventory | all | web | db
tasks:
- module copy n Its param
- module apt n Its param
- module service n 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
- name: Print return information from the previous task
ansible.builtin.debug:
msg: "Hello Everyone"
ansible-playbook -i inventory web.yaml -u ubuntu --key-file=node.pem -b