Ansible Notes of June 2024 Session
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?
Why do we need ansible?
How Ansible Works?
Componenet of Ansible?
Top 10 Ansible Adhoc commands
https://www.devopsschool.com/blog/ansible-adhoc-commands-lab-excercise-part-1/
https://docs.ansible.com/ansible/2.9/modules/list_of_all_modules.html
https://docs.ansible.com/ansible/2.9_ja/modules/modules_by_category.html
What is Ansible?
-----------------------------------------
Config mgmt tool
Release
ansible - cmd - free
awx - GUI - FREE - NON TESTED VERSION OF TOWER RELEASE - NO SUPPORT
tower - GUI - PAID - TESTED - SUPPORT
Writtenin python
From Redhat
Version
3.X
mgmt
Server(S)
Config
Server Resources
file C M D
dir C M D
services S S E D
user A E D
group
apt I R
yum I R Up
cmd script - cmd
HARDWARE
hypervisor
SOFTWARE
PEOPLE
PROCESS
Why we need ansible?
--------------------------------------------------------
Easy to learn - test - debug - share - extend compare
python - java
Bash
- Would it work on windows
Platform inde
Config large number of server(S) in parrel way
iDEOPOTEMNT
------------------------
10 things -> 1 mins ------> 10 mins - 1st
2 things -- 1 mins -------- 2 mins - 2nd
--- Code Chef - puppet - cfengine - salt
--- UI udeploy - octo deploy - xl deploy
How it works?
==========================================================
Human -> ACS -------> ARS
ACS - Ansible control server
ARS - Ansible remote server
ACS ARS
linux any
ansible NA - Agent Less
64 bit any
ssh linux
winrm windows [ http protol]
python
linux - python
windows - dotnet + ps3.0
====================================================================
Component of Ansible
===================================
Executables
ansible
ansible-playbook
Modules - 5000 modules
python code ----> RUN IN ARS - change which u want to do in ARS
copy --- src dest
services --- name --- ss
user - user
file
require param....
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
https://docs.ansible.com/ansible/latest/collections/all_plugins.html
Configfile
1 confi file
/etc/ansible/ansible.cfg
Ansible Tutorials: Example of ansible.cfg
===========================================================================
Step 1 - Install Ansible
Step 2 - What is your change requirement? --- Aka project
Deploy a web server in 100 of servers
1. install apache2 apt state=latest name=apache2
2. copy index.html copy dest=/var/www/html/index.html src=index.html
3. start a service service name=apache2 state=started
Step 3 -
====================
ansible localhost -m apt -a"state=latest name=apache2"
ansible all -i 172.31.40.191, -m apt -a"state=latest name=apache2" -u ubuntu --key-file=node.pem -b
-i
-------------------------------
inventory
LIST OF IP Add of ARS
CMD
file
dir
script
group
all
nogroup
ansible all -i inventory -m apt -a"state=latest name=apache2" -u ubuntu --key-file=node.pem -b
inventory
172.31.40.191
4.54.2.1
inventory
5.4.23.3
[web]
172.31.40.191
4.54.2.1
4.54.2.2
[app]
4.54.2.3
4.54.2.4
4.54.2.4
[db]
4.54.2.43
4.54.2.553
[gog:children]
web
app
ansible web -i inventory -m apt -a"state=latest name=apache2" -u ubuntu --key-file=node.pem -b
ansible web,app -i inventory -m apt -a"state=latest name=apache2" -u ubuntu --key-file=node.pem -b
inventory
ip
group
SSH
=================================================================================================
Authentication
username -u
passsowrd -k
key --key-file
Authorization
sudo -b
sudo with password -b -K
sudo with diff username n pass -b --become-user -K
-u -k -b -K
-u -k -b --become-user -K
Ansible inventory behavioural param
https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#connecting-to-hosts-behavioral-inventory-parameters
inventory
5.4.23.3
[web]
172.31.40.191 ansible_user=raju ansible_password=sddad
4.54.2.1 ansible_user=ramu ansible_password=dsdsdsd
4.54.2.2
[app]
4.54.2.3
4.54.2.4
4.54.2.4
[db]
4.54.2.43
4.54.2.553
---
- name: Update web servers
hosts: localhost
vars:
myname: "Rajesh Kumar"
port: 81
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: Template for httpd.conf
template:
src: ports.conf.j2
dest: /etc/apache2/ports.conf
notify:
- ReStarting a Apache Server
- name: Starting a Apache Server
ansible.builtin.service:
name: apache2
state: started
handlers:
- name: ReStarting a Apache Server
ansible.builtin.service:
name: apache2
state: restarted
Role
==========================
1 PERSON
DEV =========> 100 engineer
===================================================
Mgmt Skill - Org Skills
role
FIXED directory structure
tasks
main.yaml
second.yaml
vars
main.yaml users
default
main.yaml admin Developer
files
index.html
sdazs.sh
templates
.j2
handlers
main.yaml
---
- name: Update web servers
hosts: web
roles:
- web
- geerlingguy.java
==============================================================================================
What is Ansible?
Why Ansible?
How Ansible works? Ansible Architecture?
How to install Ansbile
Component of ACS?
Ansible Adhoc
Ansible Modules
Ansible Plusgins
Ansible Configfile
Ansible Playbook
Playbook Vars
PlaybookTemplate
PlaybookHandlers
Ansible galaxy
Roles
https://galaxy.ansible.com/ui/standalone/roles/geerlingguy/java/install/
https://github.com/geerlingguy/ansible-role-java
Ansible Template and Handlers explained with Example
Ansible Tutorials: Ansible Variables in Playbook
https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable
https://www.devopsschool.com/blog/ansible-playbook-example/https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html#examples
Ansible Notes of June 2024 Session
Ansible Tutorial: Anatomy of Ansible playbook defined!
Ansible Training Flow
Ansible Template and Handlers explained with Example
Thanks
1. Ansible is a configuration management tools which manage configuration of multiple resource on many servers.
2. Is easy to earn, test, debug, share, extend compare to python, java, etc.
3. It need Ansible Control Server to manage Ansible Remote server by connected agent less using SSH for Linux and WINRM for windows.
4.it consist of Executables, Modules, Plugins and Config file.
5.
a) – name: Create a new group
ansible.windows.win_group:
name: deploy
b) #ansible all -i 127.0.13.1, -m user -a “name=deploy group=deploy” -u tester -K -b -k
c) ansible all -i 127.0.13.1, -m yum -a “name=httpd state=present” -u tester -K -b -k
d) ansible all -i 127.0.13.1, -m yum -a “name=httpd state=start enabled=true” -u tester -K -b -k -k
e) ansible all -i 127.0.13.1, -m file -a “path=/var/www/html state=directory” -u tester -K -b -k
ansible all -i127.0.13.1, -m file -a “dest=/var/www/html/index.html mode=600 state=touch” -u tester -K -b -k
ansible all -i 127.0.13.1, -m lineinfile -a “dest=/var/www/html/index.html line=’
f) ansible all -i 127.0.13.1, -m yum -a “name=git,wget state=present” -u tester -K -b -k
g) nsible all -i 127.0.13.1, -m reboot -u tester -K -b -k
What is Ansible?
Ansible, it’s a component open-source automation for Configuration Management Tool that use Phyton Code Base
Why do we need ansible?
Its human readable automation no special coding skills to do powerful things can be utilized and shared, so they can get productive quickly and contribute their expertise
How Ansible Works?
Is an open source, command-line for automation software application written in Python, works by connecting to nodes and with small programs in modules with Plugins. The nodes are the target servers, network devices, or any computer that you manage with Ansible
Components of Ansible?
Executables Playbooks, Modules, Plugins & Config files
Top 10 Ansible Adhoc commands
Ansible ad hoc commands are one-liner Linux shell commands are quick and easy, and playbooks are like a shell script, a collective of many commands with structure logic
What is Ansible?
#Ansible is an Open Source Software,an Automation Engine. This allows us to manage and control multiple servers from one central location and working via SSH Protocol for UNIX systems and winrm for Windows.
Why do we need ansible?
# Easy to learn, test, debug, share
How Ansible Works?
# Human-readable scripts called command,playbooks to declare the desired state of a system. Ansible connects to local or remote servers via SSH and ensures that the system remains in that state.
Componenet of Ansible?
# Inventory,Playbook,Control node,Managed node,Module
Top 10 Ansible Adhoc commands
#
reboot server
managing files
managing packages
managing users and groups
managing services
gathering facts
check mode
sample :
ansible multi -m shell -a uptime
ansible all -m command -a “df -h”
ansible webservers -m copy -a “src=/local/file.conf dest=/remote/path/file.conf”
ansible all -m file -a “path=/path/to/directory state=directory mode=0755”
ansible all -m service -a “name=nginx state=restarted”
ansible all -m apt -a “name=nginx state=present”
ansible all -m user -a “name=john state=present”
ansible all -m setup
ansible ‘webserver*’-m ping
6. Ansible modules are small programs or scripts that Ansible uses to perform specific tasks on remote systems. They are the building blocks that Ansible uses to execute commands, deploy configurations, or manage infrastructure. Modules can be simple (like copying a file or installing a package) or complex (like provisioning cloud resources or configuring network devices).
7. Ansible plugins extend the functionality of Ansible by providing additional capabilities beyond what is offered by default modules and features. It can be categorise as Inventory Plugins, module plugins, Action Plugins, Lookup Plugins, Callback Plugines and Filter Plugins.
8.The Ansible configuration file, often referred to as ansible.cfg, is a central configuration file used to customize and control various aspects of how Ansible behaves when executing tasks and playbooks.
9. An Ansible playbook is a file or script written in YAML format that defines a set of tasks to be executed against one or more managed nodes (servers or devices) in our infrastructure. Playbooks allow us to automate tasks and configurations, making it easier to manage and orchestrate complex IT environments.
10. Variables (often referred to simply as “vars”) are used to store and manage data that can be reused throughout the playbook. They allow us to parameterize the tasks and configurations, making playbooks more flexible and easier to maintain.
11. A playbook template typically refers to a pre-defined structure or blueprint for writing Ansible playbooks. This template serves as a starting point or guide, ensuring consistency in playbook structure, organization, and best practices across projects or teams.
12. A playbook template provides a structured starting point that includes predefined sections, best practices, and placeholders to facilitate the writing of Ansible automation scripts which can be reusable.
13. Handlers are a special type of task that are triggered by other tasks within a playbook. They are commonly used to manage services that need to be restarted or have their status refreshed only when necessary, usually after changes have been made.
14. Ansible Galaxy is a hub or repository for finding, sharing, and managing Ansible roles.
15. Roles in Ansible are units of organization that encapsulate tasks, handlers, templates, and other Ansible artifacts in a reusable format.
What is Ansible?
Ansible is a Configuration management tool.
Why Ansible?
Manage multiple resources at the same time.
How Ansible works? Ansible Architecture?
Ansible connect to the endpoint without any agent and execute code on the remote machine with help of ansible modules.
How to install Ansbile
On Linux machine by running command yum install ansible.
Component of ACS?
Modules, plugins, inventory.
Ansible Adhoc
It’s a command line tool to execute single task on one or more then one end points.
Ansible Modules
Ansible module is a small program (or api) which gets executed on local or remote server.
Ansible Plusgins
Ansible uses plugins to add features of its core functionality.
Ansible Configfile
It’s a file through which Ansible core settings are managed.
Ansible Playbook
Its a set of tasks which gets executed on single or multiple end points (servers)
Playbook Vars
Playbook vars are used to pass the values to the ansible code (static or dynamic).
PlaybookTemplate
Template is used to facilitate and automate the management of configuration files for different endpoints.
PlaybookHandlers
It gets executed when called by other event.
Ansible galaxy
It’s a platform where users share their codes which is run by roles.
Roles
It’s a method of structure the ansible tasks, files, and variables.
What is Ansible?Ansible is an open-source automation tool used for configuration management, application deployment, task automation, and IT orchestration. It simplifies complex tasks, reduces manual efforts, and ensures consistency across IT environments.
Why Do We Need Ansible?Ansible is needed for several reasons:
How Ansible Works?Ansible works by connecting to nodes (clients/servers) through SSH (or WinRM for Windows) and then pushing out small programs called “Ansible modules” to these nodes. These modules execute the instructions written in the playbooks (Ansible’s configuration, deployment, and orchestration language). After execution, Ansible removes the modules. It follows an agentless architecture, meaning no software needs to be installed on the nodes being managed.
Components of Ansible
What is Ansible?
Ansible is an automation tool used for application deployment, configuration management, cloud provisioning, and other IT automation tasks.
Why do we need Ansible?
We need Ansible because it enables automation, consistency, agility, scalability, and ease of management across different environments and systems. It helps reduce manual effort, minimize errors, and streamline IT processes.
How Ansible Works and its Components
Ansible works by connecting to remote nodes (servers, VMs, containers) over SSH (Linux/Unix) or WinRM (Windows) and executing tasks defined in Ansible playbooks, which are written in YAML format. The key components of Ansible include.
Inventory,Playbooks,Modules,Plugins,Roles,Collections.
Top 10 Ansible Ad-hoc Commands:
ansible all -m ping
ansible web -m setup
ansible db -m service -a “name=mysql state=started”
ansible app -m copy -a “src=app.conf dest=/etc/app.conf”
ansible all -m apt -a “name=apache2 state=present” –become
ansible web -m command -a “df -h”
ansible app -m lineinfile -a “path=/etc/hosts line=’127.0.0.1 localhost'”
ansible db -m user -a “name=myuser password=secret”
ansible all -m reboot
ansible web -m debug -a “msg=’Hello, World!'”
What is Ansible?
Ansible is a software tool that provides simple but powerful automation for cross-platform computer support.
Why do we need ansible
It can configure systems, deploy software, and orchestrate advanced workflows to support application deployment, system updates, and more.
How Ansible Works?
Ansible works by connecting to nodes (or hosts) and pushing out small programs—called modules—to these nodes. Nodes are the target endpoints—servers, network devices, or any computer—that you aim to manage with Ansible. Modules are used to accomplish automation tasks in Ansible.
Component of Ansible?
Ansible Adhoc
Ansible Modules
Ansible Plusgins
Ansible Configfile
Ansible Playbook
Playbook Vars
PlaybookTemplate
PlaybookHandlers
Ansible galaxy
Roles
Top 10 Ansible Adhoc commands
ansible all -m ping
ansible all -m setup
ansible all -a ” /bin/echo hello”
ansible all -m copy -a “src=/path/to/local/file dest=/path/to/remote/file” -b
ansible all -m service -a “name=nginx sate=restarted” -b
ansible all -m file -a “path=/path/to/file mode=0646” -b
ansible all -m user -a name=johndoe state=present” -b
ansible all -i inventory -m apt -a”state=latest name=apache2″ -u ubuntu –key-file=node.pem -b
ansible all -i 172.31.40.191, -m apt -a”state=latest name=apache2″ -u ubuntu –key-file=node.pem -b
ansible-playbook -i hosts <your yaml> –tags “install” -vvv
What is Ansible?
It is a configuration and automation tool.
Why do we need ansible?
It can configure systems, deploy software, and orchestrate advanced workflows to support application deployment, system updates, etc.
How Ansible Works?
It uses ssh and winrm protocol to executes playbooks at endpoint from the ansible controller.
Componenet of Ansible?
There are two categories of computers: the control node and managed nodes. The control node is a computer that runs Ansible. There must be at least one control node, although a backup control node may also exist. A managed node is any device being managed by the control node.
Top 10 Ansible Adhoc commands
ansible <group> -m ping
ansible <group> -a “<command>”
ansible <group> -m shell -a “<command>”
ansible all -m setup
ansible all -m copy -a “src=file.txt dest=dest.txt”
ansible all -m service -a “name=<service_name> state=restarted”
ansible all -m yum -a “name=app state=latest”
What is Ansible?
#Ansible is an Open Source Software, an Automation Engine. This allows us to manage and control multiple servers from one central location and working via SSH Protocol for UNIX systems and winrm for Windows.
Why Ansible?
#Very simple to set up, learn and use, Flexible
How Ansible works? Ansible Architecture?
#Human-readable scripts called command, playbooks to declare the desired state of a system. Ansible connects to local or remote servers via SSH and ensures that the system remains in that state.
Architecture : modules, plugins, inventories, playbooks, APIs
How to install Ansbile
# install ansible , package, apache ,python, start service, verify version
Component of ACS?
# Inventory,Playbook,Control node,Managed node,Module
Ansible Adhoc
# uses the /usr/bin/ansible command-line tool to automate a single task on one or more managed nodes
And execute a quick one-liner in ansible without writing a playbook
Ansible Modules
#Ansible modules are units of code that can control system resources or execute system commands.
Ansible Plugins
#Are pieces of code that extend core Ansible functionality.
Ansible Configfile
# configuration file : ansible.cfg for configuring core functionality
Ansible Playbook
# List of task, use for deploy and in YAML format.
Playbook Vars
# Uses variables to manage differences between systems. Defining them in a file, passing them at the command line, or registering the return value or values of a task as a new variable.
PlaybookTemplate
#You can create a template for a configuration file, then deploy that configuration file to multiple environments and supply the correct data (IP address, hostname, version) for each environment. You can also use templating in playbooks directly, by templating task names and more.
PlaybookHandlers
#Handlers in Ansible playbooks are used to run specific tasks only when a change occurs on a machine.
handlers:
– name: ReStarting a Apache Server
ansible.builtin.service:
name: apache2
state: restarted
Ansible galaxy
#Command to manage Ansible roles and collections.
Roles
#
Ansible Roles are expressed in YAML used to write configuration files. When a role is included in a task ,Ansible looks for a main.yml file :
Role/
common/
Tasks/
main.yaml
second.yaml
Vars/
main.yaml
Default/
main.yaml
Files/
index.html
modify.sh
Templates/
ntp.conf.j2
Handlers/
main.yaml