SlideShare a Scribd company logo
Ansible	Hands-on	Introduction
Jon	Jozwiak,	Sr.	Cloud	Solutions	Architect
Minneapolis	RHUG	-	April	13,	2017
What	is	Ansible?
It's	a	simple	automation	language	that	can	perfectly	describe
an	IT	application	infrastructure	in	Ansible	Playbooks.
It's	an	automation	engine	that	runs	Ansible	Playbooks.
Ansible	Tower	is	an	enterprise	framework	for	controlling,
securing	and	managing	your	Ansible	automation	with	a	UI	and
RESTful	API.
Ansible	Is...
Community
THE	MOST	POPULAR	OPEN-SOURCE	AUTOMATION	COMMUNITY	ON	GITHUB
13,000+	stars	&	4,000+	forks	on	GitHub
2000+	GitHub	Contributors
Over	900	modules	shipped	with	Ansible
New	contributors	added	every	day
1200+	users	on	IRC	channel
Top	10	open	source	projects	in	2014
World-wide	meetups	taking	place	every	week
Ansible	Galaxy:	over	18,000	subscribers
250,000+	downloads	a	month
AnsibleFests	in	NYC,	SF,	London
http://ansible.com/community
Installing	Ansible
#	Install	with	yum	(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
How	Ansible	Works
Modules
Modules	are	bits	of	code	transferred	to	the	target	system	and
executed	to	satisfy	the	task	declaration.	Ansible	ships	with
several	hundred	today!
apt/yum
copy
file
get_url
git
ping
debug
service
synchronize
Modules	Documentation
http://docs.ansible.com/
Modules	Documentation
#	List	out	all	modules	installed
$	ansible-doc	-l
...
copy
cron
...
#	Read	documentation	for	installed	module
$	ansible-doc	copy
>	COPY
		The	[copy]	module	copies	a	file	on	the	local	box	to	remote	locations.	Use	the	[fetch]	module	to	copy	files	from	remote	locations	to	the	local
		box.	If	you	need	variable	interpolation	in	copied	files,	use	the	[template]	module.
		*	note:	This	module	has	a	corresponding	action	plugin.
Options	(=	is	mandatory):
...
Modules:	Run	Commands
If	Ansible	doesn’t	have	a	module	that	suits	your	needs	there	are	the	“run
command”	modules:
command:	Takes	the	command	and	executes	it	on	the	host.	The	most
secure	and	predictable.
shell:	Executes	through	a	shell	like	/bin/sh	so	you	can	use	pipes	etc.	Be
careful.
script:	Runs	a	local	script	on	a	remote	node	after	transferring	it.
raw:	Executes	a	command	without	going	through	the	Ansible	module
subsystem.
NOTE:	Unlike	standard	modules,	run	commands	have	no	concept	of	desired
state	and	should	only	be	used	as	a	last	resort.
Inventory
Inventory	is	a	collection	of	hosts	(nodes)	with	associated	data	and	groupings
that	Ansible	can	connect	and	manage.
Hosts	(nodes)
Groups
Inventory-specific	data	(variables)
Static	or	dynamic	sources
Static	Inventory	Example
10.42.0.2
10.42.0.6
10.42.0.7
10.42.0.8
10.42.0.100
host.example.com
Static	Inventory	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-hoc	command	is	a	single	Ansible	task	to	perform	quickly,	but	don’t	want
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
#	web	group
$	ansible	web	-m	command	-a	"uptime"
Sidebar:	Discovered	Facts
Facts	are	bits	of	information	derived	from	examining	a	host	systems	that	are
stored	as	variables	for	later	use	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-Hoc	Commands
Variables
Ansible	can	work	with	metadata	from	various	sources	and	manage	their	context
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	package	is	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
Handler	Tasks
Handlers	are	special	tasks	that	run	at	the	end	of	a	play	if	notified	by	another
task	when	a	change	occurs.
If	a	configuration	file	gets	changed	notify	a	service
restart	task	that	it	needs	to	run.
Example	Handler	Task	in	a	Play
tasks:
-	name:	httpd	package	is	present
		yum:
				name:	httpd
				state:	latest
		notify:	restart	httpd
-	name:	latest	index.html	file	is	present
		copy:
				src:	files/index.html
				dest:	/var/www/html/
handlers:
-	name:	restart	httpd
		service:
				name:	httpd
				state:	restarted
Plays	&	Playbooks
Plays	are	ordered	sets	of	tasks	to	execute	against	host	selections	from	your
inventory.	A	playbook	is	a	file	containing	one	or	more	plays.
Playbook	Example
---
-	name:	install	and	start	apache
		hosts:	web
		become:	yes
		vars:
				http_port:	80
		tasks:
		-	name:	httpd	package	is	present
				yum:
						name:	httpd
						state:	latest
		-	name:	latest	index.html	file	is	present
				copy:
						src:	files/index.html
						dest:	/var/www/html/
		-	name:	httpd	is	started
				service:
						name:	httpd
						state:	started
Human-Meaningful	Naming
	---
	-	name:	install	and	start	apache
			hosts:	web
			become:	yes
			vars:
					http_port:	80
			tasks:
			-	name:	httpd	package	is	present
					yum:
							name:	httpd
							state:	latest
			-	name:	latest	index.html	file	is	present
					copy:
							src:	files/index.html
							dest:	/var/www/html/
			-	name:	httpd	is	started
					service:
								name:	httpd
								state:	started
Host	Selector
---
-	name:	install	and	start	apache
		hosts:	web
		become:	yes
		vars:
				http_port:	80
		tasks:
		-	name:	httpd	package	is	present
				yum:
						name:	httpd
						state:	latest
		-	name:	latest	index.html	file	is	present
				copy:
						src:	files/index.html
						dest:	/var/www/html/
		-	name:	httpd	is	started
				service:
						name:	httpd
						state:	started
Privilege	Escalation
---
-	name:	install	and	start	apache
		hosts:	web
		become:	yes
		vars:
				http_port:	80
		tasks:
		-	name:	httpd	package	is	present
				yum:
						name:	httpd
						state:	latest
		-	name:	latest	index.html	file	is	present
				copy:
						src:	files/index.html
						dest:	/var/www/html/
		-	name:	httpd	is	started
				service:
						name:	httpd
						state:	started
Play	Variables
---
-	name:	install	and	start	apache
		hosts:	web
		become:	yes
		vars:
				http_port:	80
		tasks:
		-	name:	httpd	package	is	present
				yum:
						name:	httpd
						state:	latest
		-	name:	latest	index.html	file	is	present
				copy:
						src:	files/index.html
						dest:	/var/www/html/
		-	name:	httpd	is	started
				service:
						name:	httpd
						state:	started
Tasks
---
-	name:	install	and	start	apache
		hosts:	web
		become:	yes
		vars:
				http_port:	80
		tasks:
		-	name:	latest	httpd	package	is	present
				yum:
						name:	httpd
						state:	latest
		-	name:	latest	index.html	file	is	present
				copy:
						src:	files/index.html
						dest:	/var/www/html/
		-	name:	httpd	is	started
				service:
						name:	httpd
						state:	started
Lab	#	2:	
A	Simple	Playbook
Doing	More	with	Playbooks
Here	are	some	more	essential	playbook	features	that	you	can	apply:
Templates
Loops
Conditionals
Tags
Blocks
Templates
Ansible	embeds	the	 	that	can	be	used	to	dynamically:
Set	and	modify	play	variables
Conditional	logic
Generate	files	such	as	configurations	from	variables
Jinja2	templating	engine
Loops
Loops	can	do	one	task	on	multiple	things,	such	as	create	a	lot	of	users,	install	a
lot	of	packages,	or	repeat	a	polling	step	until	a	certain	result	is	reached.
-	yum:
				name:	"{{	item	}}"
				state:	latest
		with_items:
		-	httpd
		-	mod_wsgi
Conditionals
Ansible	supports	the	conditional	execution	of	a	task	based	on	the	run-time
evaluation	of	variable,	fact,	or	previous	task	result.
-	yum:
				name:	httpd
				state:	latest
		when:	ansible_os_family	==	"RedHat"
Tags
Tags	are	useful	to	be	able	to	run	a	subset	of	a	playbook	on-demand.
-	yum:
				name:	"{{	item	}}"
				state:	latest
		with_items:
		-	httpd
		-	mod_wsgi
		tags:
					-	packages
	-	template:
					src:	templates/httpd.conf.j2
					dest:	/etc/httpd/conf/httpd.conf
		tags:
					-	configuration
Blocks
Blocks	cut	down	on	repetitive	task	directives,	allow	for	logical	grouping	of	tasks
and	even	in	play	error	handling.
-	block:
		-	yum:
						name:	"{{	item	}}"
						state:	latest
				with_items:
				-	httpd
				-	mod_wsgi
		-	template:
						src:	templates/httpd.conf.j2
						dest:	/etc/httpd/conf/httpd.conf
		when:	ansible_os_family	==	"RedHat"
Roles
Roles	are	a	packages	of	closely	related	Ansible	content	that	can	be	shared	more
easily	than	plays	alone.
Improves	readability	and	maintainability	of	complex	plays
Eases	sharing,	reuse	and	standardization	of	automation	processes
Enables	Ansible	content	to	exist	independently	of	playbooks,	projects	--
even	organizations
Provides	functional	conveniences	such	as	file	path	resolution	and	default
values
Project	with	Embedded	Roles	Example
site.yml
roles/
			common/
					files/
					templates/
					tasks/
					handlers/
					vars/
					defaults/
					meta/
			apache/
					files/
					templates/
					tasks/
					handlers/
					vars/
					defaults/
					meta/
Project	with	Embedded	Roles	Example
#	site.yml
---
-	hosts:	web
		roles:
					-	common
					-	apache
Ansible	Galaxy
http://galaxy.ansible.com
Ansible	Galaxy	is	a	hub	for	finding,	reusing	and	sharing	Ansible	content.
Jump-start	your	automation	project	with	content	contributed	and	reviewed	by
the	Ansible	community.
Lab	#3:	
A	Playbook	Using	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 nice-pdf-copy-for-pres
PPTX
Provisionator 3000
PPTX
Using Ansible and PowerShell Together
PPTX
Programmability and Automation in Data Center Networks: A talk on Hot Air Bal...
PPT
Devoxx - Flying with Griffon
PDF
Concourse updates
PDF
Ansible Introduction
PDF
Control-with-Ansible-Tower
Ansible nice-pdf-copy-for-pres
Provisionator 3000
Using Ansible and PowerShell Together
Programmability and Automation in Data Center Networks: A talk on Hot Air Bal...
Devoxx - Flying with Griffon
Concourse updates
Ansible Introduction
Control-with-Ansible-Tower

What's hot (20)

PDF
DevOps with Ansible
PPTX
Getting started with automation using ansible
PDF
CloudKit as a backend
PDF
ConcourseCi Dockerimage
PDF
Manage any AWS resources with Terraform 0.12 - April 2020
PDF
Ansible on AWS
PDF
EclipseCon-Europe 2013: Making the Eclipse IDE fun again
PDF
Up in the air serverless computing with azure functions
PDF
Docker Cambridge: Serverless Functions Made Simple with OpenFaaS
PPTX
Server Simulator
PDF
IL2CPP: Debugging and Profiling
PDF
Splunk user group - automating Splunk with Ansible
PPTX
Programming for the Internet of Things
PDF
OpenFaaS 2019 Project Update
PDF
Cloudfoundry Introduction
PDF
server side Swift
PDF
OpenFaaS KubeCon Zero to Serverless in 60 seconds anywhere
PDF
GitOps meets Serverless
PDF
Monitor Cloud Foundry and Bosh with Prometheus
PPTX
Ansible training | redhat Ansible 2.5 Corporate course - GOT
DevOps with Ansible
Getting started with automation using ansible
CloudKit as a backend
ConcourseCi Dockerimage
Manage any AWS resources with Terraform 0.12 - April 2020
Ansible on AWS
EclipseCon-Europe 2013: Making the Eclipse IDE fun again
Up in the air serverless computing with azure functions
Docker Cambridge: Serverless Functions Made Simple with OpenFaaS
Server Simulator
IL2CPP: Debugging and Profiling
Splunk user group - automating Splunk with Ansible
Programming for the Internet of Things
OpenFaaS 2019 Project Update
Cloudfoundry Introduction
server side Swift
OpenFaaS KubeCon Zero to Serverless in 60 seconds anywhere
GitOps meets Serverless
Monitor Cloud Foundry and Bosh with Prometheus
Ansible training | redhat Ansible 2.5 Corporate course - GOT
Ad

Similar to Ansible hands-on-introduction (20)

ODP
ansible why ?
PDF
ansible_rhel.pdf
PDF
Introduction to Ansible in RHEL- RHCE.pdf
PDF
Ansible Automation to Rule Them All
PPTX
Accelerating with Ansible
PPTX
Ansible: Automation Tool
PDF
Top 50 Ansible Interview Questions And Answers in 2023.pdf
PPTX
Introduction to ansible
PPTX
Deploying Symfony2 app with Ansible
PDF
Red hat ansible automation technical deck
PDF
Ansible
PPTX
SESSION Ansible how to deploy and push resources
PDF
Ansible
PPTX
Intro to-ansible-sep7-meetup
PDF
Rui Violante - Syone - OSL19
PDF
Ansible: infrastructure automation for everyone
PPTX
Ansible presentation of cil for education prepare
ODP
Ansible get started
PPTX
Ansible: What, Why & How
PDF
ansible why ?
ansible_rhel.pdf
Introduction to Ansible in RHEL- RHCE.pdf
Ansible Automation to Rule Them All
Accelerating with Ansible
Ansible: Automation Tool
Top 50 Ansible Interview Questions And Answers in 2023.pdf
Introduction to ansible
Deploying Symfony2 app with Ansible
Red hat ansible automation technical deck
Ansible
SESSION Ansible how to deploy and push resources
Ansible
Intro to-ansible-sep7-meetup
Rui Violante - Syone - OSL19
Ansible: infrastructure automation for everyone
Ansible presentation of cil for education prepare
Ansible get started
Ansible: What, Why & How
Ad

Recently uploaded (20)

PDF
RMMM.pdf make it easy to upload and study
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Complications of Minimal Access Surgery at WLH
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
GDM (1) (1).pptx small presentation for students
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
master seminar digital applications in india
PPTX
Institutional Correction lecture only . . .
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
RMMM.pdf make it easy to upload and study
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Complications of Minimal Access Surgery at WLH
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Final Presentation General Medicine 03-08-2024.pptx
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
GDM (1) (1).pptx small presentation for students
A systematic review of self-coping strategies used by university students to ...
O7-L3 Supply Chain Operations - ICLT Program
master seminar digital applications in india
Institutional Correction lecture only . . .
202450812 BayCHI UCSC-SV 20250812 v17.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
O5-L3 Freight Transport Ops (International) V1.pdf
human mycosis Human fungal infections are called human mycosis..pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Microbial disease of the cardiovascular and lymphatic systems
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
VCE English Exam - Section C Student Revision Booklet

Ansible hands-on-introduction