SlideShare a Scribd company logo
1 of 26
Ansible Terms
Control Node: the machine where Ansible is installed, responsible
for running the provisioning on the servers you are managing.
Inventory: an INI file that contains information about the servers
you are managing.
Playbook: a YAML file containing a series of procedures that
should be automated.
Task: a block that defines a single procedure to be executed, e.g.:
install a package.
Module: a module typically abstracts a system task, like dealing
with packages or creating and changing files. Ansible has a
multitude of built-in modules, but you can also create custom ones.
 Role: a set of related playbooks, templates and other files,
organized in a pre-defined way to facilitate reuse and share.
 Play: a provisioning executed from start to finish is called a
play.
 Facts: global variables containing information about the
system, like network interfaces or operating system.
 Handlers: used to trigger service status changes, like restarting
or reloading a service.
Task Format
 A task defines a single automated step that should be executed
by Ansible. It typically involves the usage of a module or the
execution of a raw command. This is how a task looks:
 - name: This is a task
 apt: name=vim state=latest
 The name part is actually optional, but recommended, as it
shows up in the output of the provisioning when the task is
executed.
 The apt part is a built-in Ansible module that abstracts the
management of packages on Debian-based distributions.
 This example task tells Ansible that the package vim should
have its state changed to latest, which will cause the package
Playbooks
 Playbooks are YAML files containing a series of directives to
automate the provisioning of a server. The following example is
a simple playbook that perform two tasks: updates the apt
cache and installs vim afterwards:
 ---
 - hosts: all
 become: true
 tasks:
 - name: Update apt-cache
 apt: update_cache=yes
 - name: Install Vim
 apt: name=vim state=latest
 YAML relies on indentation to serialize data structures.
Variables in Playbooks
 ---
 - hosts: all
 become: true
 vars:
 package: vim
 tasks:
 - name: Install Package
 apt: name={{ package }} state=latest
 ---
 - hosts: all
 become: true
 vars:
 packages: [ 'vim', 'git', 'curl' ]
 tasks:
 - name: Install Package
 apt: name={{ item }} state=latest
 with_items: "{{ packages }}"
Using Conditions
 - name: Shutdown Debian Based Systems
 command: /sbin/shutdown -t now
 when: ansible_os_family == "Debian"
 A common use case for conditionals in IT automation is when
the execution of a task depends on the output of a command.
 With Ansible, the way we implement this is by registering a
variable to hold the results of a command execution, and then
testing this variable in a subsequent task.
 We can test for the command’s exit status (if failed or
successful).
 We can also check for specific contents inside the output,
although this might require the usage of regex expressions and
string parsing commands.
 ---
 - hosts: web_host1
 tasks:
 - name: Check if PHP is installed
 register: php_installed
 command: php -v
 ignore_errors: true
 - name: This task is only executed if PHP is
Using templates
 vhost.tpl
 <VirtualHost *:80>
 ServerAdmin webmaster@localhost
 DocumentRoot {{ doc_root }}
 <Directory {{ doc_root }}>
 AllowOverride All
 Require all granted
 </Directory>
 ---
 - hosts: web_host1
 vars:
 doc_root: /var/www/example
 tasks:
 - name: Change default apache virtual
host
 template:
 src: vhost.tpl
Roles
 Roles are a level of abstraction on top of tasks and playbooks
that let you structure your Ansible configuration in a modular
and reusable format.
 Playbooks can become complex when they are responsible for
configuring many different systems with multiple tasks for each
system, so Ansible also lets you organize tasks in a directory
structure called a Role.
 With this configuration, playbooks invoke roles instead of tasks,
so you can still group tasks together and then reuse roles in
other playbooks
 Roles also allow you to collect templates, static files, and
variables along with your tasks in one structured format.
 roles
 apache
 defaults files handlers meta tasks templates vars
 defaults: This directory lets you set default variables for included or
dependent roles. Any defaults set here can be overridden in
playbooks or inventory files.
 files: This directory contains static files and script files that might be
copied to or executed on a remote server.
 handlers: All handlers that were in your playbook previously can now
be added into this directory.
 meta: This directory is reserved for role
metadata, typically used for dependency
management.. For example, you can define a
list of roles that must be applied before the
current role is invoked.
 templates: This directory is reserved for
templates that will generate files on remote
hosts. Templates typically use variables defined
on files located in the vars directory, and on
host information that is collected at runtime.
 tasks: This directory contains one or more files
 roles/apache/tasks/main.yml
 ---
 - name: Update apt
 apt: update_cache=yes
 - name: Install Apache
 apt: name=apache2 state=latest
 - name: Create custom document root
 file: path={{ doc_root }} state=directory owner=www-data group=www-data
 - name: Set up HTML file
 roles/apache/handlers/main.yml
 ---
 - name: restart apache
 service: name=apache2 state=restarted
 roles/apache/files/index.html
 <html>
 <head><title>Configuration Management Hands On</title></head>
 <h1>This server was provisioned using
<strong>Ansible</strong></h1>
 </html>
 roles/apache/handlers/vhost.pl
 <VirtualHost *:80>
 ServerAdmin webmaster@localhost
 DocumentRoot {{ doc_root }}
 <Directory {{ doc_root }}>
 AllowOverride All
 Require all granted
 </Directory>
 </VirtualHost>
Variables
 In Ansible, variables provide the much-needed flexibility in
Playbooks, templates and inventories as we shall later see in
this tutorial.
 Built-in variables can be used to provide system information
such as hostname, system architecture, interfaces etc.
 Variables can also be used to replace strings and also in loops
to loop through a given set of values.
 A valid variable name is made of the following characters
 Letters
 Numbers
 Underscores
 A combination of any two or all of the above
 A variable SHOULD ALWAYS START WITH A LETTER and SHOULD
NOT CONTAIN ANY SPACES.
 Assigning a value to a variable in a playbook is quite easy and
straightforward.
 Begin by calling the vars keyword then call the variable name
followed by the value as shown.
 ---
 - hosts: all
 vars:
 salutations: Hello guys!
 tasks:
 - name: Ansible Variable Basic Usage
 debug:
 msg: "{{ salutations }}"
Variables with array
 You can use arrays and assign them to variables as shown in
the syntax below:
 vars:
 arrayname:
 – value1
 – value2
 - hosts: all
 vars:
 students:
 - Mark
 - Melisa
 - Arthur
 - Kevin
 - Lisa
Variables with dictionaries
 vars:
 arrayname:
 dictionary_name1:
 value1: itemvalue1
 value2: itemvalue2
 dictionary_name2:
 value1: itemvalue1
 value2: itemvalue2
 - hosts: all
 vars:
 students:
 - Mark:
 city: Melbourne
 address: 0045-0987-8642
 - Angela:
 city: Sydney
 address: 3456-7685-9087
 tasks:
 - debug:
 var: students
Variables in Inventory files
 [web_servers]
 webserver_1
 webserver_2
 [web_servers:vars]
 http_port=443
 ntp_server=us.pool.ntp.org
 web_servers:
 hosts:
 web_server_1:
 web_server_2:

 vars:
 http_port=80
 ntp_server=us.pool.ntp.org
 Although you can specify variables in the inventory file, standard practice
discourages storing variables in the inventory file. This is where the host and
group variable files come in.
 In a host variable file, the variable applier only to one host system in the
inventory file. The host variable file is usually stored in the host_vars
directory which is usually specified in /etc/ansible/ path.
 Consider the inventory file below where we have 2 servers each using
different ntp servers
 [web_servers]
 webserver_1 ntp_server=uk.pool.ntp.org
 webserver_2 ntp_server=de.pool.ntp.org

 # vim /etc/ansible/host_vars/webserver_1
 ---

More Related Content

Similar to playbooks.pptx

Introduction to Ansible - Peter Halligan
Introduction to Ansible - Peter HalliganIntroduction to Ansible - Peter Halligan
Introduction to Ansible - Peter HalliganCorkOpenTech
 
ACADGILD:: HADOOP LESSON
ACADGILD:: HADOOP LESSON ACADGILD:: HADOOP LESSON
ACADGILD:: HADOOP LESSON Padma shree. T
 
Ansible leveraging 2.0
Ansible leveraging 2.0Ansible leveraging 2.0
Ansible leveraging 2.0bcoca
 
Zero Downtime Deployment with Ansible
Zero Downtime Deployment with AnsibleZero Downtime Deployment with Ansible
Zero Downtime Deployment with AnsibleStein Inge Morisbak
 
From Dev to DevOps - Codemotion ES 2012
From Dev to DevOps - Codemotion ES 2012From Dev to DevOps - Codemotion ES 2012
From Dev to DevOps - Codemotion ES 2012Carlos Sanchez
 
Puppet for Java developers - JavaZone NO 2012
Puppet for Java developers - JavaZone NO 2012Puppet for Java developers - JavaZone NO 2012
Puppet for Java developers - JavaZone NO 2012Carlos Sanchez
 
Ansible with oci
Ansible with ociAnsible with oci
Ansible with ociDonghuKIM2
 
AnsibleFest 2014 - Role Tips and Tricks
AnsibleFest 2014 - Role Tips and TricksAnsibleFest 2014 - Role Tips and Tricks
AnsibleFest 2014 - Role Tips and Tricksjimi-c
 
MariaDB, MySQL and Ansible: automating database infrastructures
MariaDB, MySQL and Ansible: automating database infrastructuresMariaDB, MySQL and Ansible: automating database infrastructures
MariaDB, MySQL and Ansible: automating database infrastructuresFederico Razzoli
 
Configure h base hadoop and hbase client
Configure h base hadoop and hbase clientConfigure h base hadoop and hbase client
Configure h base hadoop and hbase clientShashwat Shriparv
 
Puppet: Eclipsecon ALM 2013
Puppet: Eclipsecon ALM 2013Puppet: Eclipsecon ALM 2013
Puppet: Eclipsecon ALM 2013grim_radical
 
101 2.4 use debian package management
101 2.4 use debian package management101 2.4 use debian package management
101 2.4 use debian package managementAcácio Oliveira
 
Drupal cambs ansible for drupal april 2015
Drupal cambs ansible for drupal april 2015Drupal cambs ansible for drupal april 2015
Drupal cambs ansible for drupal april 2015Ryan Brown
 
Ansible new paradigms for orchestration
Ansible new paradigms for orchestrationAnsible new paradigms for orchestration
Ansible new paradigms for orchestrationPaolo Tonin
 
Getting started with Ansible
Getting started with AnsibleGetting started with Ansible
Getting started with AnsibleIvan Serdyuk
 

Similar to playbooks.pptx (20)

Installing lemp with ssl and varnish on Debian 9
Installing lemp with ssl and varnish on Debian 9Installing lemp with ssl and varnish on Debian 9
Installing lemp with ssl and varnish on Debian 9
 
Introduction to Ansible - Peter Halligan
Introduction to Ansible - Peter HalliganIntroduction to Ansible - Peter Halligan
Introduction to Ansible - Peter Halligan
 
ACADGILD:: HADOOP LESSON
ACADGILD:: HADOOP LESSON ACADGILD:: HADOOP LESSON
ACADGILD:: HADOOP LESSON
 
Ansible leveraging 2.0
Ansible leveraging 2.0Ansible leveraging 2.0
Ansible leveraging 2.0
 
Puppet
PuppetPuppet
Puppet
 
Zero Downtime Deployment with Ansible
Zero Downtime Deployment with AnsibleZero Downtime Deployment with Ansible
Zero Downtime Deployment with Ansible
 
From Dev to DevOps - Codemotion ES 2012
From Dev to DevOps - Codemotion ES 2012From Dev to DevOps - Codemotion ES 2012
From Dev to DevOps - Codemotion ES 2012
 
Puppet for Java developers - JavaZone NO 2012
Puppet for Java developers - JavaZone NO 2012Puppet for Java developers - JavaZone NO 2012
Puppet for Java developers - JavaZone NO 2012
 
Installing AtoM with Ansible
Installing AtoM with AnsibleInstalling AtoM with Ansible
Installing AtoM with Ansible
 
Ansible with oci
Ansible with ociAnsible with oci
Ansible with oci
 
AnsibleFest 2014 - Role Tips and Tricks
AnsibleFest 2014 - Role Tips and TricksAnsibleFest 2014 - Role Tips and Tricks
AnsibleFest 2014 - Role Tips and Tricks
 
Puppet
PuppetPuppet
Puppet
 
MariaDB, MySQL and Ansible: automating database infrastructures
MariaDB, MySQL and Ansible: automating database infrastructuresMariaDB, MySQL and Ansible: automating database infrastructures
MariaDB, MySQL and Ansible: automating database infrastructures
 
Configure h base hadoop and hbase client
Configure h base hadoop and hbase clientConfigure h base hadoop and hbase client
Configure h base hadoop and hbase client
 
Cis 274 intro
Cis 274   introCis 274   intro
Cis 274 intro
 
Puppet: Eclipsecon ALM 2013
Puppet: Eclipsecon ALM 2013Puppet: Eclipsecon ALM 2013
Puppet: Eclipsecon ALM 2013
 
101 2.4 use debian package management
101 2.4 use debian package management101 2.4 use debian package management
101 2.4 use debian package management
 
Drupal cambs ansible for drupal april 2015
Drupal cambs ansible for drupal april 2015Drupal cambs ansible for drupal april 2015
Drupal cambs ansible for drupal april 2015
 
Ansible new paradigms for orchestration
Ansible new paradigms for orchestrationAnsible new paradigms for orchestration
Ansible new paradigms for orchestration
 
Getting started with Ansible
Getting started with AnsibleGetting started with Ansible
Getting started with Ansible
 

Recently uploaded

Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...caitlingebhard1
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....rightmanforbloodline
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformWSO2
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxMarkSteadman7
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingWSO2
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)Samir Dash
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governanceWSO2
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 

Recently uploaded (20)

Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation Computing
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governance
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

playbooks.pptx

  • 1. Ansible Terms Control Node: the machine where Ansible is installed, responsible for running the provisioning on the servers you are managing. Inventory: an INI file that contains information about the servers you are managing. Playbook: a YAML file containing a series of procedures that should be automated. Task: a block that defines a single procedure to be executed, e.g.: install a package. Module: a module typically abstracts a system task, like dealing with packages or creating and changing files. Ansible has a multitude of built-in modules, but you can also create custom ones.
  • 2.  Role: a set of related playbooks, templates and other files, organized in a pre-defined way to facilitate reuse and share.  Play: a provisioning executed from start to finish is called a play.  Facts: global variables containing information about the system, like network interfaces or operating system.  Handlers: used to trigger service status changes, like restarting or reloading a service.
  • 3. Task Format  A task defines a single automated step that should be executed by Ansible. It typically involves the usage of a module or the execution of a raw command. This is how a task looks:  - name: This is a task  apt: name=vim state=latest  The name part is actually optional, but recommended, as it shows up in the output of the provisioning when the task is executed.  The apt part is a built-in Ansible module that abstracts the management of packages on Debian-based distributions.  This example task tells Ansible that the package vim should have its state changed to latest, which will cause the package
  • 4. Playbooks  Playbooks are YAML files containing a series of directives to automate the provisioning of a server. The following example is a simple playbook that perform two tasks: updates the apt cache and installs vim afterwards:  ---  - hosts: all  become: true  tasks:  - name: Update apt-cache  apt: update_cache=yes  - name: Install Vim  apt: name=vim state=latest  YAML relies on indentation to serialize data structures.
  • 5. Variables in Playbooks  ---  - hosts: all  become: true  vars:  package: vim  tasks:  - name: Install Package  apt: name={{ package }} state=latest
  • 6.  ---  - hosts: all  become: true  vars:  packages: [ 'vim', 'git', 'curl' ]  tasks:  - name: Install Package  apt: name={{ item }} state=latest  with_items: "{{ packages }}"
  • 7. Using Conditions  - name: Shutdown Debian Based Systems  command: /sbin/shutdown -t now  when: ansible_os_family == "Debian"  A common use case for conditionals in IT automation is when the execution of a task depends on the output of a command.  With Ansible, the way we implement this is by registering a variable to hold the results of a command execution, and then testing this variable in a subsequent task.  We can test for the command’s exit status (if failed or successful).  We can also check for specific contents inside the output, although this might require the usage of regex expressions and string parsing commands.
  • 8.  ---  - hosts: web_host1  tasks:  - name: Check if PHP is installed  register: php_installed  command: php -v  ignore_errors: true  - name: This task is only executed if PHP is
  • 9. Using templates  vhost.tpl  <VirtualHost *:80>  ServerAdmin webmaster@localhost  DocumentRoot {{ doc_root }}  <Directory {{ doc_root }}>  AllowOverride All  Require all granted  </Directory>
  • 10.  ---  - hosts: web_host1  vars:  doc_root: /var/www/example  tasks:  - name: Change default apache virtual host  template:  src: vhost.tpl
  • 11. Roles  Roles are a level of abstraction on top of tasks and playbooks that let you structure your Ansible configuration in a modular and reusable format.  Playbooks can become complex when they are responsible for configuring many different systems with multiple tasks for each system, so Ansible also lets you organize tasks in a directory structure called a Role.  With this configuration, playbooks invoke roles instead of tasks, so you can still group tasks together and then reuse roles in other playbooks  Roles also allow you to collect templates, static files, and variables along with your tasks in one structured format.
  • 12.  roles  apache  defaults files handlers meta tasks templates vars  defaults: This directory lets you set default variables for included or dependent roles. Any defaults set here can be overridden in playbooks or inventory files.  files: This directory contains static files and script files that might be copied to or executed on a remote server.  handlers: All handlers that were in your playbook previously can now be added into this directory.
  • 13.  meta: This directory is reserved for role metadata, typically used for dependency management.. For example, you can define a list of roles that must be applied before the current role is invoked.  templates: This directory is reserved for templates that will generate files on remote hosts. Templates typically use variables defined on files located in the vars directory, and on host information that is collected at runtime.  tasks: This directory contains one or more files
  • 14.  roles/apache/tasks/main.yml  ---  - name: Update apt  apt: update_cache=yes  - name: Install Apache  apt: name=apache2 state=latest  - name: Create custom document root  file: path={{ doc_root }} state=directory owner=www-data group=www-data  - name: Set up HTML file
  • 15.  roles/apache/handlers/main.yml  ---  - name: restart apache  service: name=apache2 state=restarted  roles/apache/files/index.html  <html>  <head><title>Configuration Management Hands On</title></head>  <h1>This server was provisioned using <strong>Ansible</strong></h1>  </html>
  • 16.  roles/apache/handlers/vhost.pl  <VirtualHost *:80>  ServerAdmin webmaster@localhost  DocumentRoot {{ doc_root }}  <Directory {{ doc_root }}>  AllowOverride All  Require all granted  </Directory>  </VirtualHost>
  • 17. Variables  In Ansible, variables provide the much-needed flexibility in Playbooks, templates and inventories as we shall later see in this tutorial.  Built-in variables can be used to provide system information such as hostname, system architecture, interfaces etc.  Variables can also be used to replace strings and also in loops to loop through a given set of values.
  • 18.  A valid variable name is made of the following characters  Letters  Numbers  Underscores  A combination of any two or all of the above  A variable SHOULD ALWAYS START WITH A LETTER and SHOULD NOT CONTAIN ANY SPACES.
  • 19.  Assigning a value to a variable in a playbook is quite easy and straightforward.  Begin by calling the vars keyword then call the variable name followed by the value as shown.  ---  - hosts: all  vars:  salutations: Hello guys!  tasks:  - name: Ansible Variable Basic Usage  debug:  msg: "{{ salutations }}"
  • 20. Variables with array  You can use arrays and assign them to variables as shown in the syntax below:  vars:  arrayname:  – value1  – value2
  • 21.  - hosts: all  vars:  students:  - Mark  - Melisa  - Arthur  - Kevin  - Lisa
  • 22. Variables with dictionaries  vars:  arrayname:  dictionary_name1:  value1: itemvalue1  value2: itemvalue2  dictionary_name2:  value1: itemvalue1  value2: itemvalue2
  • 23.  - hosts: all  vars:  students:  - Mark:  city: Melbourne  address: 0045-0987-8642  - Angela:  city: Sydney  address: 3456-7685-9087  tasks:  - debug:  var: students
  • 24. Variables in Inventory files  [web_servers]  webserver_1  webserver_2  [web_servers:vars]  http_port=443  ntp_server=us.pool.ntp.org
  • 25.  web_servers:  hosts:  web_server_1:  web_server_2:   vars:  http_port=80  ntp_server=us.pool.ntp.org
  • 26.  Although you can specify variables in the inventory file, standard practice discourages storing variables in the inventory file. This is where the host and group variable files come in.  In a host variable file, the variable applier only to one host system in the inventory file. The host variable file is usually stored in the host_vars directory which is usually specified in /etc/ansible/ path.  Consider the inventory file below where we have 2 servers each using different ntp servers  [web_servers]  webserver_1 ntp_server=uk.pool.ntp.org  webserver_2 ntp_server=de.pool.ntp.org   # vim /etc/ansible/host_vars/webserver_1  ---