SlideShare une entreprise Scribd logo
1  sur  31
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

Tendances (20)

Ansible - Introduction
Ansible - IntroductionAnsible - Introduction
Ansible - Introduction
 
Ansible Introduction
Ansible Introduction Ansible Introduction
Ansible Introduction
 
Docker introduction
Docker introductionDocker introduction
Docker introduction
 
Ansible presentation
Ansible presentationAnsible presentation
Ansible presentation
 
Introduction to ansible
Introduction to ansibleIntroduction to ansible
Introduction to ansible
 
Ansible 101
Ansible 101Ansible 101
Ansible 101
 
Jenkins Tutorial.pdf
Jenkins Tutorial.pdfJenkins Tutorial.pdf
Jenkins Tutorial.pdf
 
Ansible
AnsibleAnsible
Ansible
 
IT Automation with Ansible
IT Automation with AnsibleIT Automation with Ansible
IT Automation with Ansible
 
Ansible
AnsibleAnsible
Ansible
 
Introduction to Ansible
Introduction to AnsibleIntroduction to Ansible
Introduction to Ansible
 
Ansible
AnsibleAnsible
Ansible
 
Ansible Tutorial For Beginners | What Is Ansible And How It Works? | Ansible ...
Ansible Tutorial For Beginners | What Is Ansible And How It Works? | Ansible ...Ansible Tutorial For Beginners | What Is Ansible And How It Works? | Ansible ...
Ansible Tutorial For Beginners | What Is Ansible And How It Works? | Ansible ...
 
Automating with Ansible
Automating with AnsibleAutomating with Ansible
Automating with Ansible
 
DevOps with Ansible
DevOps with AnsibleDevOps with Ansible
DevOps with Ansible
 
Accelerating with Ansible
Accelerating with AnsibleAccelerating with Ansible
Accelerating with Ansible
 
Best practices for ansible
Best practices for ansibleBest practices for ansible
Best practices for ansible
 
Ansible
AnsibleAnsible
Ansible
 
Docker swarm
Docker swarmDocker swarm
Docker swarm
 
Jenkins tutorial
Jenkins tutorialJenkins tutorial
Jenkins tutorial
 

Similaire à Ansible

Ansible Tutorial.pdf
Ansible Tutorial.pdfAnsible Tutorial.pdf
Ansible Tutorial.pdfNigussMehari4
 
Intro to-ansible-sep7-meetup
Intro to-ansible-sep7-meetupIntro to-ansible-sep7-meetup
Intro to-ansible-sep7-meetupRamesh Godishela
 
Ansible & Salt - Vincent Boon
Ansible & Salt - Vincent BoonAnsible & Salt - Vincent Boon
Ansible & Salt - Vincent BoonMyNOG
 
UNIT-I Introduction to Ansible.pptx
UNIT-I Introduction to Ansible.pptxUNIT-I Introduction to Ansible.pptx
UNIT-I Introduction to Ansible.pptxPandiya Rajan
 
Ansible Devops North East - slides
Ansible Devops North East - slides Ansible Devops North East - slides
Ansible Devops North East - slides InfinityPP
 
Basics of Ansible - Sahil Davawala
Basics of Ansible - Sahil DavawalaBasics of Ansible - Sahil Davawala
Basics of Ansible - Sahil DavawalaSahil Davawala
 
Introduction to Ansible - Peter Halligan
Introduction to Ansible - Peter HalliganIntroduction to Ansible - Peter Halligan
Introduction to Ansible - Peter HalliganCorkOpenTech
 
Top 50 Ansible Interview Questions And Answers in 2023.pdf
Top 50 Ansible Interview Questions And Answers in 2023.pdfTop 50 Ansible Interview Questions And Answers in 2023.pdf
Top 50 Ansible Interview Questions And Answers in 2023.pdfDatacademy.ai
 
Ansible a tool for dev ops
Ansible a tool for dev opsAnsible a tool for dev ops
Ansible a tool for dev opsRené Ribaud
 
Ansible automation tool with modules
Ansible automation tool with modulesAnsible automation tool with modules
Ansible automation tool with modulesmohamedmoharam
 
Introduction to Ansible - (dev ops for people who hate devops)
Introduction to Ansible - (dev ops for people who hate devops)Introduction to Ansible - (dev ops for people who hate devops)
Introduction to Ansible - (dev ops for people who hate devops)Jude A. Goonawardena
 
Ansible is Our Wishbone(Automate DBA Tasks With Ansible)
Ansible is Our Wishbone(Automate DBA Tasks With Ansible)Ansible is Our Wishbone(Automate DBA Tasks With Ansible)
Ansible is Our Wishbone(Automate DBA Tasks With Ansible)M Malai
 
Ansible is Our Wishbone
Ansible is Our WishboneAnsible is Our Wishbone
Ansible is Our WishboneMydbops
 
MongoDB Management & Ansible
MongoDB Management & AnsibleMongoDB Management & Ansible
MongoDB Management & AnsibleMongoDB
 
Ansible Automation Best Practices From Startups to Enterprises - Minnebar 12
Ansible Automation Best Practices From Startups to Enterprises - Minnebar 12Ansible Automation Best Practices From Startups to Enterprises - Minnebar 12
Ansible Automation Best Practices From Startups to Enterprises - Minnebar 12Keith Resar
 

Similaire à Ansible (20)

Ansible Tutorial.pdf
Ansible Tutorial.pdfAnsible Tutorial.pdf
Ansible Tutorial.pdf
 
Intro to-ansible-sep7-meetup
Intro to-ansible-sep7-meetupIntro to-ansible-sep7-meetup
Intro to-ansible-sep7-meetup
 
Ansible & Salt - Vincent Boon
Ansible & Salt - Vincent BoonAnsible & Salt - Vincent Boon
Ansible & Salt - Vincent Boon
 
Ansible intro
Ansible introAnsible intro
Ansible intro
 
UNIT-I Introduction to Ansible.pptx
UNIT-I Introduction to Ansible.pptxUNIT-I Introduction to Ansible.pptx
UNIT-I Introduction to Ansible.pptx
 
ansible_rhel.pdf
ansible_rhel.pdfansible_rhel.pdf
ansible_rhel.pdf
 
Ansible Devops North East - slides
Ansible Devops North East - slides Ansible Devops North East - slides
Ansible Devops North East - slides
 
Ansible
AnsibleAnsible
Ansible
 
Ansible_Basics_ppt.pdf
Ansible_Basics_ppt.pdfAnsible_Basics_ppt.pdf
Ansible_Basics_ppt.pdf
 
Basics of Ansible - Sahil Davawala
Basics of Ansible - Sahil DavawalaBasics of Ansible - Sahil Davawala
Basics of Ansible - Sahil Davawala
 
DevOps for database
DevOps for databaseDevOps for database
DevOps for database
 
Introduction to Ansible - Peter Halligan
Introduction to Ansible - Peter HalliganIntroduction to Ansible - Peter Halligan
Introduction to Ansible - Peter Halligan
 
Top 50 Ansible Interview Questions And Answers in 2023.pdf
Top 50 Ansible Interview Questions And Answers in 2023.pdfTop 50 Ansible Interview Questions And Answers in 2023.pdf
Top 50 Ansible Interview Questions And Answers in 2023.pdf
 
Ansible a tool for dev ops
Ansible a tool for dev opsAnsible a tool for dev ops
Ansible a tool for dev ops
 
Ansible automation tool with modules
Ansible automation tool with modulesAnsible automation tool with modules
Ansible automation tool with modules
 
Introduction to Ansible - (dev ops for people who hate devops)
Introduction to Ansible - (dev ops for people who hate devops)Introduction to Ansible - (dev ops for people who hate devops)
Introduction to Ansible - (dev ops for people who hate devops)
 
Ansible is Our Wishbone(Automate DBA Tasks With Ansible)
Ansible is Our Wishbone(Automate DBA Tasks With Ansible)Ansible is Our Wishbone(Automate DBA Tasks With Ansible)
Ansible is Our Wishbone(Automate DBA Tasks With Ansible)
 
Ansible is Our Wishbone
Ansible is Our WishboneAnsible is Our Wishbone
Ansible is Our Wishbone
 
MongoDB Management & Ansible
MongoDB Management & AnsibleMongoDB Management & Ansible
MongoDB Management & Ansible
 
Ansible Automation Best Practices From Startups to Enterprises - Minnebar 12
Ansible Automation Best Practices From Startups to Enterprises - Minnebar 12Ansible Automation Best Practices From Startups to Enterprises - Minnebar 12
Ansible Automation Best Practices From Startups to Enterprises - Minnebar 12
 

Dernier

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 

Dernier (20)

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 

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