SlideShare a Scribd company logo
Ansible Hands-on Introduction
By: Rahul Bajaj
@rahulrb0509 Associate Software Engineer, Red Hat
What is Ansible?
It's a simple automation language that can perfectly describe
an IT application infrastructurein Ansible Playbooks.
It's an automation engine that runsAnsible Playbooks.
Ansible Tower is an enterprise framework for controlling,
securing and managing yourAnsible automationwith a UI and
RESTful API.
AnsibleIs...
Installing Ansible
# Install withyum (Example RHEL 7)
$ wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm
$ yum localinstall epel-release-7-9.noarch.rpm
$ yum --enablerepo=epel install ansible
# Install using pip
$ pip install ansible
HowAnsibleWorks
Modules
Modules are bits of code transferred to the target system and
executedto satisfy the task declaration. Ansible ships with
several hundred today!
apt/yum
copy
file
get_url
git
ping
debug
service
synchronize
template
Modules Documentation
http://docs.ansible.com/
Modules Documentation
# List out all modulesinstalled
$ ansible-doc -l
...
copy
cron
...
# Read documentation for installedmodule
$ ansible-doc copy
> COPY
The [copy] module copies a file on the local box to remote locations. Use the [fetch] module to copy filesfrom
box.If you need variable interpolationin copied files, use the [template] module.
* note: Thismodule has a corresponding action plugin.
Options (= is mandatory):
...
Modules: Run Commands
If Ansible doesn’t have a module that suits yourneeds there are the
command” modules:
command: Takes the command and executes it on the host. The m
secure and predictable.
shell: Executesthrough a shell like /bin/shsoyou can use pipes etc. B
careful.
script: Runs a local script on a remote node after transferring
raw: Executesa command without going through the Ansible modu
subsystem.
NOTE:Unlike standard modules,run commands have no concept of desired
state and should only be used asa last resort.
Inventory
Inventory is a collection of hosts (nodes) with associated dataand groupin
that Ansible can connect and manage.
Hosts (nodes)
Groups
Inventory-specific data(variables)
Static ordynamic sources
StaticInventory Example
10.42.0.2
10.42.0.6
10.42.0.7
10.42.0.8
10.42.0.100
host.example.com
StaticInventory Example
[control]
control ansible_host=10.42.0.2
[web]
node-[1:3] ansible_host=10.42.0.[6:8]
[haproxy]
haproxy ansible_host=10.42.0.100
[all:vars]
ansible_user=vagrant
ansible_ssh_private_key_file=~/.vagrant.d/insecure_private_key
Ad-Hoc Commands
An ad-hoccommand is a single Ansible task to perform quickly, but don’
to save for later.
# check all my inventory hosts are ready to be
# managed by Ansible
$ ansible all -m ping
# collect and display the discovered facts
# for the localhost
$ ansible localhost -m setup
# run the uptime command on all hosts in the
# webgroup
$ ansible web-m command -a "uptime"
Sidebar: Discovered Facts
Facts are bits of informationderived from examining a host systems that
stored asvariablesfor lateruse in a play.
$ ansible localhost -m setup
localhost | success >> {
"ansible_facts": {
"ansible_default_ipv4": {
"address": "192.168.1.37",
"alias": "wlan0",
"gateway": "192.168.1.1",
"interface": "wlan0",
"macaddress": "c4:85:08:3b:a9:16",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.1.0",
"type": "ether"
},
Lab # 1:
Ad-HocCommands
Variables
Ansible can work with metadata from various sources and manage the
in the form of variables.
Command line parameters
Plays and tasks
Files
Inventory
Discovered facts
Roles
Tasks
Tasks are the application of a module to perform a specific unit of work.
file: A directory should exist
yum: A package should be installed
service: A service should be running
template: Render a configuration file from a template
get_url: Fetch an archive file from a URL
git: Clone a source code repository
Example Tasks in a Play
tasks:
- name: httpd packageis present
yum:
name: httpd
state: latest
- name: latest index.html file is present
copy:
src: files/index.html
dest: /var/www/html/
- name: restart httpd
service:
name: httpd
state: restarted
Plays & Playbooks
Plays are ordered sets of tasks to execute against host selections from
inventory. A playbookis a file containing one ormore plays.
Lab # 2:
A Simple Playbook
DoingMore withPlaybooks
Here are some more essential playbookfeatures that you can apply:
Templates
Loops
Conditionals
Tags
Blocks
Templates
Ansible embeds that can be used to dynamically:
Set and modifyplay variables
Conditionallogic
Generate files such asconfigurations from variables
Jinja2 templating engine
Loops
Loops can do one task on multiple things,such ascreate a lot of users,
lot of packages, orrepeat a pollingstep untila certain result is reached.
Conditionals
Ansible supports the conditional execution of a task based on the run-tim
evaluation of variable, fact,orprevious task result.
Tags
Tagsare useful to be able to run a subset of a playbookon-demand.
Roles
Roles are a packages of closelyrelated Ansible content that can be sh
easily thanplays alone.
Improvesreadability and maintainability of complex plays
Eases sharing, reuse and standardization of automationprocesses
Enables Ansible content to exist independently of playbooks, projects
even organizations
Provides functional conveniencessuch asfile pathresolution and defau
values
Project withEmbeddedRoles Example
site.yml
roles/
common/
files/
templates/
tasks/
handlers/
vars/
defaults/
meta/
apache/
files/
templates/
tasks/
handlers/
vars/
defaults/
meta/
Project withEmbeddedRoles Example
# site.yml
---
- hosts: web
roles:
- common
- apache
AnsibleGalaxy
http://galaxy.ansible.com
Ansible Galaxyis a hub for finding, reusing and sharing Ansible conten
Jump-start yourautomationprojectwith content contributedand reviewedby
the Ansible community.
Lab #3:
A PlaybookUsing Roles
Lab #4:
Using an Ansible Galaxy Role
Next Steps
It's easy to get started
ansible.com/get-started
Join the Ansible community
ansible.com/community
Would you like to learn a lot more?
redhat.com/en/services/training/do407-automation-ansible

More Related Content

PDF
Ansible - Introduction
PPTX
Introduction to ansible
PDF
Ansible, best practices
PDF
Ansible 101
PDF
Ansible Introduction
PPTX
What Is Ansible? | How Ansible Works? | Ansible Tutorial For Beginners | DevO...
PPTX
Introduction to Ansible
PDF
Ansible - Hands on Training
Ansible - Introduction
Introduction to ansible
Ansible, best practices
Ansible 101
Ansible Introduction
What Is Ansible? | How Ansible Works? | Ansible Tutorial For Beginners | DevO...
Introduction to Ansible
Ansible - Hands on Training

What's hot (20)

ODP
Introduction to Ansible
PDF
Ansible
PDF
Automation with ansible
PDF
Ansible
PPTX
Ansible presentation
PDF
IT Automation with Ansible
PPT
Ansible presentation
PDF
DevOps with Ansible
PPTX
Best practices for ansible
ODP
ansible why ?
PDF
Network Automation with Ansible
ODP
An Introduction To Jenkins
PDF
Ansible
PPTX
Introduction to Docker - 2017
PDF
Kubernetes
PDF
Getting Started with Kubernetes
PPTX
Jenkins CI presentation
PPTX
Jenkins CI
PDF
Evolution of containers to kubernetes
PDF
What Is Kubernetes | Kubernetes Introduction | Kubernetes Tutorial For Beginn...
Introduction to Ansible
Ansible
Automation with ansible
Ansible
Ansible presentation
IT Automation with Ansible
Ansible presentation
DevOps with Ansible
Best practices for ansible
ansible why ?
Network Automation with Ansible
An Introduction To Jenkins
Ansible
Introduction to Docker - 2017
Kubernetes
Getting Started with Kubernetes
Jenkins CI presentation
Jenkins CI
Evolution of containers to kubernetes
What Is Kubernetes | Kubernetes Introduction | Kubernetes Tutorial For Beginn...
Ad

Similar to Ansible (20)

PPTX
Intro to-ansible-sep7-meetup
PDF
ansible_rhel.pdf
PPTX
Ansible as configuration management tool for devops
PDF
Ansible nice-pdf-copy-for-pres
PPTX
Ansible Automation Best Practices From Startups to Enterprises - Minnebar 12
PPTX
Automating with ansible (Part A)
PDF
A quick intro to Ansible
PDF
Ansible_Basics_ppt.pdf
PDF
#OktoCampus - Workshop : An introduction to Ansible
PPTX
Introduction to Ansible - (dev ops for people who hate devops)
PDF
DevOpsDaysCPT Ansible Infrastrucutre as Code 2017
PPTX
SESSION Ansible how to deploy and push resources
PDF
Automating with ansible (part a)
PPTX
Ansible
PDF
Getting Started with Ansible - Jake.pdf
PDF
Ansible 202 - sysarmy
PPTX
Ansible Network Automation session1
PDF
Ansible not only for Dummies
PDF
Dal caos all’automazione di sistemi e infrastrutture IT con Ansible
Intro to-ansible-sep7-meetup
ansible_rhel.pdf
Ansible as configuration management tool for devops
Ansible nice-pdf-copy-for-pres
Ansible Automation Best Practices From Startups to Enterprises - Minnebar 12
Automating with ansible (Part A)
A quick intro to Ansible
Ansible_Basics_ppt.pdf
#OktoCampus - Workshop : An introduction to Ansible
Introduction to Ansible - (dev ops for people who hate devops)
DevOpsDaysCPT Ansible Infrastrucutre as Code 2017
SESSION Ansible how to deploy and push resources
Automating with ansible (part a)
Ansible
Getting Started with Ansible - Jake.pdf
Ansible 202 - sysarmy
Ansible Network Automation session1
Ansible not only for Dummies
Dal caos all’automazione di sistemi e infrastrutture IT con Ansible
Ad

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Approach and Philosophy of On baking technology
PDF
Encapsulation theory and applications.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
KodekX | Application Modernization Development
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Big Data Technologies - Introduction.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Spectral efficient network and resource selection model in 5G networks
NewMind AI Weekly Chronicles - August'25 Week I
Advanced methodologies resolving dimensionality complications for autism neur...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
20250228 LYD VKU AI Blended-Learning.pptx
Understanding_Digital_Forensics_Presentation.pptx
Review of recent advances in non-invasive hemoglobin estimation
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
The AUB Centre for AI in Media Proposal.docx
Approach and Philosophy of On baking technology
Encapsulation theory and applications.pdf
A Presentation on Artificial Intelligence
KodekX | Application Modernization Development
The Rise and Fall of 3GPP – Time for a Sabbatical?
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Big Data Technologies - Introduction.pptx
MYSQL Presentation for SQL database connectivity
Encapsulation_ Review paper, used for researhc scholars
Spectral efficient network and resource selection model in 5G networks

Ansible

  • 1. Ansible Hands-on Introduction By: Rahul Bajaj @rahulrb0509 Associate Software Engineer, Red Hat
  • 2. What is Ansible? It's a simple automation language that can perfectly describe an IT application infrastructurein Ansible Playbooks. It's an automation engine that runsAnsible Playbooks. Ansible Tower is an enterprise framework for controlling, securing and managing yourAnsible automationwith a UI and RESTful API.
  • 4. Installing Ansible # Install withyum (Example RHEL 7) $ wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm $ yum localinstall epel-release-7-9.noarch.rpm $ yum --enablerepo=epel install ansible # Install using pip $ pip install ansible
  • 6. Modules Modules are bits of code transferred to the target system and executedto satisfy the task declaration. Ansible ships with several hundred today! apt/yum copy file get_url git ping debug service synchronize template
  • 8. Modules Documentation # List out all modulesinstalled $ ansible-doc -l ... copy cron ... # Read documentation for installedmodule $ ansible-doc copy > COPY The [copy] module copies a file on the local box to remote locations. Use the [fetch] module to copy filesfrom box.If you need variable interpolationin copied files, use the [template] module. * note: Thismodule has a corresponding action plugin. Options (= is mandatory): ...
  • 9. Modules: Run Commands If Ansible doesn’t have a module that suits yourneeds there are the command” modules: command: Takes the command and executes it on the host. The m secure and predictable. shell: Executesthrough a shell like /bin/shsoyou can use pipes etc. B careful. script: Runs a local script on a remote node after transferring raw: Executesa command without going through the Ansible modu subsystem. NOTE:Unlike standard modules,run commands have no concept of desired state and should only be used asa last resort.
  • 10. Inventory Inventory is a collection of hosts (nodes) with associated dataand groupin that Ansible can connect and manage. Hosts (nodes) Groups Inventory-specific data(variables) Static ordynamic sources
  • 12. StaticInventory Example [control] control ansible_host=10.42.0.2 [web] node-[1:3] ansible_host=10.42.0.[6:8] [haproxy] haproxy ansible_host=10.42.0.100 [all:vars] ansible_user=vagrant ansible_ssh_private_key_file=~/.vagrant.d/insecure_private_key
  • 13. Ad-Hoc Commands An ad-hoccommand is a single Ansible task to perform quickly, but don’ to save for later. # check all my inventory hosts are ready to be # managed by Ansible $ ansible all -m ping # collect and display the discovered facts # for the localhost $ ansible localhost -m setup # run the uptime command on all hosts in the # webgroup $ ansible web-m command -a "uptime"
  • 14. Sidebar: Discovered Facts Facts are bits of informationderived from examining a host systems that stored asvariablesfor lateruse in a play. $ ansible localhost -m setup localhost | success >> { "ansible_facts": { "ansible_default_ipv4": { "address": "192.168.1.37", "alias": "wlan0", "gateway": "192.168.1.1", "interface": "wlan0", "macaddress": "c4:85:08:3b:a9:16", "mtu": 1500, "netmask": "255.255.255.0", "network": "192.168.1.0", "type": "ether" },
  • 16. Variables Ansible can work with metadata from various sources and manage the in the form of variables. Command line parameters Plays and tasks Files Inventory Discovered facts Roles
  • 17. Tasks Tasks are the application of a module to perform a specific unit of work. file: A directory should exist yum: A package should be installed service: A service should be running template: Render a configuration file from a template get_url: Fetch an archive file from a URL git: Clone a source code repository
  • 18. Example Tasks in a Play tasks: - name: httpd packageis present yum: name: httpd state: latest - name: latest index.html file is present copy: src: files/index.html dest: /var/www/html/ - name: restart httpd service: name: httpd state: restarted
  • 19. Plays & Playbooks Plays are ordered sets of tasks to execute against host selections from inventory. A playbookis a file containing one ormore plays.
  • 20. Lab # 2: A Simple Playbook
  • 21. DoingMore withPlaybooks Here are some more essential playbookfeatures that you can apply: Templates Loops Conditionals Tags Blocks
  • 22. Templates Ansible embeds that can be used to dynamically: Set and modifyplay variables Conditionallogic Generate files such asconfigurations from variables Jinja2 templating engine
  • 23. Loops Loops can do one task on multiple things,such ascreate a lot of users, lot of packages, orrepeat a pollingstep untila certain result is reached.
  • 24. Conditionals Ansible supports the conditional execution of a task based on the run-tim evaluation of variable, fact,orprevious task result.
  • 25. Tags Tagsare useful to be able to run a subset of a playbookon-demand.
  • 26. Roles Roles are a packages of closelyrelated Ansible content that can be sh easily thanplays alone. Improvesreadability and maintainability of complex plays Eases sharing, reuse and standardization of automationprocesses Enables Ansible content to exist independently of playbooks, projects even organizations Provides functional conveniencessuch asfile pathresolution and defau values
  • 28. Project withEmbeddedRoles Example # site.yml --- - hosts: web roles: - common - apache
  • 29. AnsibleGalaxy http://galaxy.ansible.com Ansible Galaxyis a hub for finding, reusing and sharing Ansible conten Jump-start yourautomationprojectwith content contributedand reviewedby the Ansible community.
  • 30. Lab #3: A PlaybookUsing Roles Lab #4: Using an Ansible Galaxy Role
  • 31. Next Steps It's easy to get started ansible.com/get-started Join the Ansible community ansible.com/community Would you like to learn a lot more? redhat.com/en/services/training/do407-automation-ansible