Ansible - Full
Ansible - Full
By Mostafa Yehia
What is Ansible
Why Ansible
SSH overview
Ansible & SSH
DAY 1
Installing Ansible & preparing SSH
AGENDA Ad-hoc commands
Inventory file
Ansible.cfg file
Ad-hoc commands escalation
WHAT IS ANSIBLE ?
v Ansible is a software tool that provides simple but powerful
automation for cross-platform computer support.
v It is used for updates on workstations and servers, cloud
provisioning, configuration management, and nearly anything a
systems administrator does on a daily basis.
server server server
C2 General
WHY ANSIBLE ?
v Idempotent: An operation is idempotent if the result of performing
it once is exactly the same as the result of performing it repeatedly
without any intervening actions.
v Agentless: Other tools like (Puppet & Chef) require an agent to be
installed on the target device. Ansible only requires an SSH
connection to the target device.
v Open-source: Ansible is an open-source community project
sponsored by Red Hat.
SSH OVERVIEW
v OpenSSH is the premier connectivity tool for remote login with the
SSH protocol.
v It encrypts all traffic to eliminate connection hijacking and other
attacks.
PC 2
SSH
PC 1
C2 General
ANSIBLE & SSH
How Ansible connects to servers ?
server server server
SSH
C2 General
SSH My pc
SSH
Structure:
ansible [pattern] -i [inventory] --private-key [/path/to/private/key] -u [remote_user] -m [module_name]
Example:
ansible all -i 3.87.24.251, --private-key ~/.ssh/devops -u ubuntu -m ping
SSH equivalent:
ssh [email protected] -i ~/.ssh/devops -o 'RemoteCommand echo pong;' -t
INVENTORY FILE
inventory file: A file that describes Hosts and Groups in Ansible.
Examples:
[web_servers]
3.87.24.251
[database_servers]
3.87.24.252
3.87.24.253
Create the inventory file
Put the IP of host 1 in the inventory file
Use the inventory file path in your ad-hoc command instead of using
the IP hard-coded
Example:
ansible all -i inventory --private-key ~/.ssh/devops -u ubuntu -m ping
INVENTORY FILE
C2 General
CONFIGURATION FILE
ansible.cfg file: This is the brain and the heart of Ansible.
The file that governs the behavior of all interactions performed by the control machine.
Locations: Example:
ANSIBLE_CONFIG (environment variable if set)
ansible.cfg (in the current directory) [defaults]
~/.ansible.cfg (in the home directory) inventory = ./inventory
/etc/ansible/ansible.cfg private_key_file = ~/.ssh/devops
remote_user = ubuntu
Create the configuration file
Insert some values in the configuration file
Run the minimized ad-hoc command
Example: ansible all -m ping
CONFIGURATION FILE
C2 General
AD-HOC COMMANDS ESCALATION
Ad-hoc: Running Ansible to perform some quick command with SUDO permissions.
Structure:
ansible [pattern] -m [module_name] --become Example ansible.cfg:
Example: [privilege_escalation]
ansible all -m command -a "whoami" --become become = true
Insert the correct values in the configuration file
Example: ansible all -m command -a "whoami"
What is the output of the command ?
C2 General
Ansible playbook
DAY 2 Ansible modules
AGENDA Tags
Variables
PLAYBOOK
• A playbook is a list of plays.
• playbook: Playbook is the language Ansible uses to orchestrate, configure, administer, or deploy systems.
• Play: is a mapping between a set of hosts (groups, hostnames, or IPs) and the tasks which run on those hosts
to define the role that those systems will perform.
• There can be one or many plays in a playbook.
Examples:
PLAYBOOK
C2 General
MODULES
Modules: are units of code that can control system resources or execute system commands.
Ansible provides a module library that you can execute directly on remote hosts or through playbooks.
MODULES
C2 General
TAGS
Tags: Running only specific parts of a playbook instead of running the entire playbook.
Example:
TAGS
C2 General
VARIABLES
Variables: Ansible uses variables to manage differences between systems.
example:
Locations:
VARIABLES
C2 General
Loops
DAY 3 When
Register
AGENDA Handlers
Templates “.j2”
LOOPS
Loops: Ansible uses loops to execute a task multiple times.
example: example:
LOOPS
C2 General
WHEN
When: You want to execute different tasks depending on the value of a fact, a variable, or the result of a previous task.
Example:
WHEN
C2 General
REGISTER
Register: Ansible register is a way to capture the output from task execution and store it in a variable.
Example:
C2 General
HANDLERS
Handlers: Handlers are tasks that only run when notified.
Example:
handlers:
- name: my_handler
service:
name: nginx
state: restarted
1. install a package
(get the package name from vars)
2. copy a list of files (index.html & file.css & file.js) from controller to host using loop
(get the list of file names from vars)
(the actual files will be stored in ./your_current_directory)
(will be defined as Handler)
(will be triggered by step 1)
3. Restart the service of the installed package
(will be defined as Handler)
(will be triggered by step 2)
HANDLERS
C2 General
TEMPLATES
Templates: Ansible uses Jinja2 templates to create dynamic content at the controller end, and render it
as static content at the host end.
index.html.j2 index.html
index.html.j2
<html> <html>
<body> <body>
<h1> {{ my_message }} </h1> <h1> {{
Hello
my_message
from ansible
}} <h1>
<h1>
</body> </body>
</html> </html>
1. installing a package
(get the package name from vars)
2. Copying index.html from controller to host using template
(get the template name & template message from vars)
(the actual template file will be stored in ./your_current_directory)
(will trigger step 4)
3. copying a list of files (file.css & file.js) from controller to host using loop
(get the list of file names from vars)
(the actual files will be stored in ./your_current_directory)
(will be defined as Handler)
(will be triggered by step 1)
4. Restart the service of the installed package
(will be defined as Handler)
(will be triggered by step 3)
TEMPLATES
C2 General
QUESTIONS ?
THANK YOU
DAY 4 Roles
Ansible-Vault
AGENDA Ansible-Galaxy
ROLES
Roles: pack related vars, files, templates, tasks, and handlers, based on a known file structure so you can
easily reuse them and share them with other users.
roles
web db
main.yml main.yml
Create your first role with name (web)
ROLES
C2 General
HANDLERS
Handlers: Handlers are tasks that only run when notified.
Example:
handlers:
- name: my_handler
service:
name: nginx
state: restarted
roles
web db
rs
tasks vars files an dle
h
HANDLERS
C2 General
TEMPLATES
Templates: Ansible uses Jinja2 templates to create dynamic content at the controller end, and render it
as static content at the host end.
index.html.j2 index.html
index.html.j2
<html> <html>
<body> <body>
<h1> {{ my_message }} </h1> <h1> {{
Hello
my_message
from ansible
}} <h1>
<h1>
</body> </body>
</html> </html>
roles
web db
rs s
dle late
tasks vars files h an
tem
p
TEMPLATES
C2 General
ANSIBLE-VAULT
Ansible-Vault: provides a way to encrypt and manage sensitive data such as passwords.
Example:
passwords.yml
- name: my play with vault user_pass: 123456
hosts: all
vars:
user_name: ahmed
Commands:
var_files:
- ./passwords.yml $ ansible-vault encrypt ./passwords.yml
tasks: $ ansible-playbook my-playbook.yml --ask-vault-pass
- name: my task1 with vault
user:
name: "{{ user_name }}"
password: "{{ user_pass }}"
ANSIBLE-GALAXY
Ansible-Galaxy: refers to the Galaxy website, a free site for finding, downloading, and sharing
community developed roles and collections.
Commands: