SlideShare a Scribd company logo
1 of 83
Download to read offline
Getting started with Ansible
Alexander Saprykin
Senior Software Engineer
13th October 2018
2
Introduction
What is Ansible?
Ansible history
Diving into Ansible roles
Basic concepts
Inventory
Playbook
Role
Module
Plugin
Agenda
Getting started
Create a role
Roles under the hood
How to use roles?
BASIC CONCEPTS
4
Inventory
5
What is inventory?
Inventory
● Defines the infrastructure
● Static inventory - can be sourced from a text file (INI or YAML format)
● Dynamic inventory - generated from a script
● Ansible provides dozens of inventory scripts (e.g. AWS EC2, OpenStack, Docker):
https://github.com/ansible/ansible/tree/devel/contrib/inventory
6
Inventory
INI YAML
all:
hosts:
mail.example.com:
children:
webservers:
hosts:
web01.example.com:
web02.example.com:
dbservers:
hosts:
db01.example.com:
db02.example.com:
db03.example.com:
mail.example.com
[webservers]
web01.example.com
web02.example.com
[dbservers]
db01.example.com
db02.example.com
db03.example.com
7
Groups
Inventory
INI YAML
all:
hosts:
mail.example.com:
children:
webservers:
hosts:
web01.example.com:
web02.example.com:
dbservers:
hosts:
db01.example.com:
db02.example.com:
db03.example.com:
mail.example.com
[webservers]
web01.example.com
web02.example.com
[dbservers]
db01.example.com
db02.example.com
db03.example.com
8
Hosts
Inventory
INI YAML
all:
hosts:
mail.example.com:
children:
webservers:
hosts:
web01.example.com:
web02.example.com:
dbservers:
hosts:
db01.example.com:
db02.example.com:
db03.example.com:
mail.example.com
[webservers]
web01.example.com
web02.example.com
[dbservers]
db01.example.com
db02.example.com
db03.example.com
9
Inventory
INI YAML
all:
hosts:
mail.example.com:
children:
webservers:
hosts:
web01.example.com:
web02.example.com:
dbservers:
hosts:
db01.example.com:
db02.example.com:
db03.example.com:
mail.example.com
[webservers]
web01.example.com
web02.example.com
[dbservers]
db01.example.com
db02.example.com
db03.example.com
jumper ansible_host=192.0.2.50 ansible_port=5555
10
Parameters
Inventory
INI
YAML
all:
hosts:
jumper:
ansible_host: 192.0.2.50
ansible_port: 5555
11
Playbook
12
What is playbook?
Playbook
● A yaml document
● Defines set of plays
● Plays bring together inventory and tasks
13
Playbook
- name: install and start nginx
hosts: web
become: yes
vars:
nginx_packages: [nginx, python-pip, python-devel, gcc]
nginx_test_message: This is a test message
nginx_keepalive_timeout: 115
tasks:
- name: install nginx packages
yum: name="{{ item }}" state=present
with_items: "{{ nginx_packages }}"
- name: install uwsgi
pip: name=uwsgi state=present
14
Playbook
- name: install and start nginx
hosts: web
become: yes
vars:
nginx_packages: [nginx, python-pip, python-devel, gcc]
nginx_test_message: This is a test message
nginx_keepalive_timeout: 115
tasks:
- name: install nginx packages
yum: name="{{ item }}" state=present
with_items: "{{ nginx_packages }}"
- name: install uwsgi
pip: name=uwsgi state=present
15
Playbook
- name: install and start nginx
hosts: web
become: yes
vars:
nginx_packages: [nginx, python-pip, python-devel, gcc]
nginx_test_message: This is a test message
nginx_keepalive_timeout: 115
tasks:
- name: install nginx packages
yum: name="{{ item }}" state=present
with_items: "{{ nginx_packages }}"
- name: install uwsgi
pip: name=uwsgi state=present
16
Playbook
- name: install and start nginx
hosts: web
become: yes
vars:
nginx_packages: [nginx, python-pip, python-devel, gcc]
nginx_test_message: This is a test message
nginx_keepalive_timeout: 115
tasks:
- name: install nginx packages
yum: name="{{ item }}" state=present
with_items: "{{ nginx_packages }}"
- name: install uwsgi
pip: name=uwsgi state=present
17
Playbook
- name: install and start nginx
hosts: web
become: yes
vars:
nginx_packages: [nginx, python-pip, python-devel, gcc]
nginx_test_message: This is a test message
nginx_keepalive_timeout: 115
tasks:
- name: install nginx packages
yum: name="{{ item }}" state=present
with_items: "{{ nginx_packages }}"
- name: install uwsgi
pip: name=uwsgi state=present
18
Playbook
- name: install and start nginx
hosts: web
become: yes
vars:
nginx_packages: [nginx, python-pip, python-devel, gcc]
nginx_test_message: This is a test message
nginx_keepalive_timeout: 115
tasks:
- name: install nginx packages
yum: name="{{ item }}" state=present
with_items: "{{ nginx_packages }}"
- name: install uwsgi
pip: name=uwsgi state=present
19
Playbook
- name: install and start nginx
hosts: web
become: yes
vars:
nginx_packages: [nginx, python-pip, python-devel, gcc]
nginx_test_message: This is a test message
nginx_keepalive_timeout: 115
tasks:
- name: install nginx packages
yum: name="{{ item }}" state=present
with_items: "{{ nginx_packages }}"
- name: install uwsgi
pip: name=uwsgi state=present
20
Playbook
- name: install and start nginx
hosts: web
become: yes
vars:
nginx_packages: [nginx, python-pip, python-devel, gcc]
nginx_test_message: This is a test message
nginx_keepalive_timeout: 115
tasks:
- name: install nginx packages
yum: name="{{ item }}" state=present
with_items: "{{ nginx_packages }}"
- name: install uwsgi
pip: name=uwsgi state=present
21
Playbook
- name: install and start nginx
hosts: web
become: yes
vars:
nginx_packages: [nginx, python-pip, python-devel, gcc]
nginx_test_message: This is a test message
nginx_keepalive_timeout: 115
tasks:
- name: install nginx packages
yum: name="{{ item }}" state=present
with_items: "{{ nginx_packages }}"
- name: install uwsgi
pip: name=uwsgi state=present
22
Playbook
- name: install and start nginx
hosts: web
become: yes
vars:
nginx_packages: [nginx, python-pip, python-devel, gcc]
nginx_test_message: This is a test message
nginx_keepalive_timeout: 115
tasks:
- name: install nginx packages
yum: name="{{ item }}" state=present
with_items: "{{ nginx_packages }}"
- name: install uwsgi
pip: name=uwsgi state=present
23
Playbook
- name: install and start nginx
hosts: web
become: yes
vars:
nginx_packages: [nginx, python-pip, python-devel, gcc]
nginx_test_message: This is a test message
nginx_keepalive_timeout: 115
tasks:
- name: install nginx packages
yum: name="{{ item }}" state=present
with_items: "{{ nginx_packages }}"
- name: install uwsgi
pip: name=uwsgi state=present
24
Playbook
- name: install and start nginx
hosts: web
become: yes
vars:
nginx_packages: [nginx, python-pip, python-devel, gcc]
nginx_test_message: This is a test message
nginx_keepalive_timeout: 115
tasks:
- name: install nginx packages
yum: name="{{ item }}" state=present
with_items: "{{ nginx_packages }}"
- name: install uwsgi
pip: name=uwsgi state=present
with_items:
Loops through the list, executing
the task for each item.
25
Include
● Import a file containing a list of tasks
● Pass parameters
● Dynamic
● Conditional
● Encourages code reuse
26
Include
- name: Add hostname
include: add_hostname.yml
- name: Add hostname
include: add_hostname.yml
param: ”{{ item }}”
with_items: [1, 2, 3]
- name: Get the client assets
include: “{{ ansible_os_family }}.yml”
27
Include
- name: Add hostname
include: add_hostname.yml
- name: Add hostname
include: add_hostname.yml
param: ”{{ item }}”
with_items: [1, 2, 3]
- name: Get the client assets
include: “{{ ansible_os_family }}.yml”
28
Include
- name: Add hostname
include: add_hostname.yml
- name: Add hostname
include: add_hostname.yml
param: ”{{ item }}”
with_items: [1, 2, 3]
- name: Get the client assets
include: “{{ ansible_os_family }}.yml”
29
Include
- name: Add hostname
include: add_hostname.yml
- name: Add hostname
include: add_hostname.yml
param: ”{{ item }}”
with_items: [1, 2, 3]
- name: Get the client assets
include: “{{ ansible_os_family }}.yml”
30
Role
● Self-contained, reusable, complete unit of work
● Decoupled from assumptions made by plays
● Decoupled from inventory
● Encourages collaboration
31
Module
● Called by a task (or used ad-hoc)
● Perform an action on a target host
● Can take direct action, wrap a command line tool, or talk to an API
● Ansible includes a 100’s of modules:
https://github.com/ansible/ansible/tree/devel/lib/ansible/modules
32
Facts
● Provided by setup module
● Returned by modules
● Created using set_fact module
● Use for variable substitution, and conditional checks
33
Plugin
● Augments Ansible core functionality
● Plugin types: action, cache, callback, connection, filter, lookup, shell, strategy,
terminal, test, vars
● Examples:
○ connection: local, ssh, docker, chroot
○ action: copy, fetch, synchronize
● See full list at https://github.com/ansible/ansible/tree/devel/lib/ansible/plugins
DIVING INTO ANSIBLE ROLES
35
Getting started
36
Playbooks
● Made up of plays
● Plays are opinionated: become, gather_facts, connection, vars, etc.
● Assume a specific inventory
● Target a specific use case
● Generally not reusable
37
Example playbook
- name: install uwsgi
pip: name=uwsgi state=present
- name: copy default.conf
template: src=templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf
> backup=yes
notify: start nginx
- name: copy index.html
template: src=templates/index.html.j2
> dest=/usr/share/nginx/html/index.html
notify: start nginx
# ...
38
Example playbook
# ...
- name: get response
uri: url=http://localhost/ return_content: yes
register: response
until: 'nginx_test_message in response.content'
retries: 10
delay: 1
handlers:
- name: start nginx
service: name=nginx state=started enabled=yes
39
Roles
● Decoupled from inventory and plays
● Not Just a set of tasks
● Self-contained, reusable, complete unit of work
40
● Install packages
● Update configuration
● Run tests
● Package software
● Build images
● Orchestrate containers
What you can do with roles?
41
Switch to a role
- name: install and start nginx
hosts: web
become: yes
roles:
- role: install-nginx
packages:
- nginx
- python-pip
- python-devel
- gcc
nginx_test_message: This is a test message
nginx_keepalive_timeout: 115
42
Switch to a role
- name: install and start nginx
hosts: web
become: yes
roles:
- role: install-nginx
packages:
- nginx
- python-pip
- python-devel
- gcc
nginx_test_message: This is a test message
nginx_keepalive_timeout: 115
43
Switch to a role
- name: install and start nginx
hosts: web
become: yes
roles:
- role: install-nginx
packages:
- nginx
- python-pip
- python-devel
- gcc
nginx_test_message: This is a test message
nginx_keepalive_timeout: 115
44
Switch to a role
- name: install and start nginx
hosts: web
become: yes
roles:
- role: install-nginx
packages:
- nginx
- python-pip
- python-devel
- gcc
nginx_test_message: This is a test message
nginx_keepalive_timeout: 115
45
Create a role
46
From scratch
Create a role
1. Create a roles directory
2. Create a minimal role directory structure:
mkdir -p ./roles/nginx-install/tasks/
3. Start writing your tasks in main.yml file in tasks directory
47
From template
Create a role
● Ansible Galaxy client tool
ansible-galaxy init nginx-install
● Creates a complete directory structure
● Creates default files
48
From Ansible Galaxy (...or more)
Or just download one
● From Ansible Galaxy - https://galaxy.ansible.com
ansible-galaxy install <namespace>.<role-name>
● From Git
ansible-galaxy install git+https://github.com/acme/nginx-install.git
● ...
49
Where are my roles?
● ANSIBLE_ROLES_PATH
● ansible.cfg
● Provide a colon : separated list of paths
● roles directory next to the playbook
[defaults]
roles_path=/path/to/roles
50
Roles under the hood
51
Role structure
roles/
└── install-nginx/
├── .travis.yml
├── README.md
├── defaults/
│ └── main.yml
├── files/
├── handles/
├── meta/
├── tasks/
│ └── main.yml
├── templates/
├── tests/
└── vars/
52
Tasks
● tasks directory
● Entrypoint: tasks/main.yml
● Contains the main list of tasks to be executed by the role
● Tie together handlers, templates, files, variables and defaults
53
Tasks
- name: install nginx packages
yum: name="{{ item }}" state=present
with_items: "{{ nginx_packages }}"
- name: install uwsgi
pip: name=uwsgi state=present
- name: copy default.conf
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf backup=yes
...
54
● handlers directory
● Entrypoint: handlers/main.yml
● Module indicates when a change has been made
● In response to a change, a notify action can be triggered
● Notify handlers by name…
● … or by topic - new in Ansible 2.2
Handlers
...
- name: copy index.html
template: src=index.html.j2 dest=/usr/share/nginx/html/index.html
notify: start nginx
...
55
Handlers
Task from tasks/main.yml
- name: start nginx
service: name=nginx state=started enabled=yes
56
Handlers by name
Handler from handlers/main.yml
- name: start and enable nginx
service: name=nginx state=started enabled=yes
listen: start nginx
- name: Restart and enable supervisord
service: name=supervisord state=restarted enabled=yes
listen: start nginx
57
Handlers by topic
Handler from handlers/main.yml
58
● files is the base directory for copy and synchronize modules
● Files are copied to the target node
● templates is the base directory for the template module
● Templates contain variables - during execution, the file is transformed, and the result
is copied to the target node
● Ansible uses Jinja2 as a template engine:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_templating.html
Files and templates
roles/
└── install-nginx/
├── ...
└── templates/
├── index.html.j2
└── nginx.conf.j2
59
Templates
- name: copy default.conf
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf backup=yes
- name: copy index.html
template: src=index.html.j2 dest=/usr/share/nginx/html/index.html
notify: start nginx
60
Templates
tasks/main.yml
...
http {
...
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout {{ nginx_keepalive_timeout }};
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
61
Templates
templates/nginx.conf.j2
...
<body>
<div class="container">
<img src="/static/images/happy-cow.png"/>
<p>{{ nginx_test_message }}</p>
</div>
<footer>Ansible by Red Hat</footer>
</body>
</html>
...
62
Templates
templates/index.html.j2
63
● defaults/main.yml
○ Defines variables the user can override to change role behavior (e.g.
conditionals, configuration settings)
● variables/main.yml
○ Used by the author to organize the role (e.g. constants, choices)
○ Add additional files to dynamically shape the role
Variables
packages:
- nginx
- python-pip
- python-devel
- gcc
nginx_test_message: Hello World!
nginx_keepalive_timeout: 65
64
Templates
defaults/main.yml
65
● meta/main.yml
● Resolved at install
● Executed before the role
● Recursive
● Each dependency executed once only
Dependencies
---
dependencies:
- { role: common, some_parameter: 3 }
- { role: apache, apache_port: 80 }
- { role: postgres, dbname: blarg, other_parameter: 12 }
66
Dependencies
meta/main.yml
...
dependencies:
- src: git+https://github.com/redhat/ansible-role-common.git
version: v1.0.0
name: common
some_parameter: 3
67
Dependencies
From SCM
...
dependencies:
- role: geerlingguy.php-fpm
some_parameter: 3
68
Dependencies
From Galaxy
69
● README.md
● meta/main.yml
● Example playbook
Documentation
● library directory - add custom modules
● See: https://docs.ansible.com/ansible/latest/dev_guide/developing_plugins.html
● <type>_plugin - add custom plugin
● See: https://docs.ansible.com/ansible/2.5/dev_guide/developing_modules.html
70
Modules and plugins
71
● Jeff Geerling (https://github.com/geerlingguy)
○ https://github.com/geerlingguy?tab=repositories&q=ansible-role
● DebOps
https://github.com/debops/debops
Some examples
72
How to use roles?
73
● Play can include roles and tasks
○ Roles are executed first, then tasks
○ For readability, list roles first
● Consider surfacing all defaults in the playbook
○ Make the playbook self-documenting
In a play
74
In a play
hosts: web
name: install and start nginx with wsgi
become: yes
roles:
- role: install-nginx
packages:
- nginx
- python-pip
- python-devel
- gcc
nginx_test_message: This is a test message
nginx_keepalive_timeout: 115
75
● New in Ansible 2.2
● include_role
● Treats the role more like a task
In a task
76
In a play
- name: Run my role
include_role:
name: myrole
- name: Run tasks/other.yml instead of 'main'
include_role:
name: myrole
tasks_from: other
- name: Pass variables to role
include_role:
name: myrole
vars:
rolevar1: 'value from task'
77
In a play
- name: Run my role
include_role:
name: myrole
- name: Run tasks/other.yml instead of 'main'
include_role:
name: myrole
tasks_from: other
- name: Pass variables to role
include_role:
name: myrole
vars:
rolevar1: 'value from task'
78
In a play
- name: Run my role
include_role:
name: myrole
- name: Run tasks/other.yml instead of 'main'
include_role:
name: myrole
tasks_from: other
- name: Pass variables to role
include_role:
name: myrole
vars:
rolevar1: 'value from task'
79
In a play
- name: Run my role
include_role:
name: myrole
- name: Run tasks/other.yml instead of 'main'
include_role:
name: myrole
tasks_from: other
- name: Pass variables to role
include_role:
name: myrole
vars:
rolevar1: 'value from task'
80
In a play
- name: Use role in loop
include_role:
name: myrole
with_items:
- '{{ roleinput1 }}'
- '{{ roleinput2 }}'
- name: Conditional role
include_role:
name: myrole
when: not some_condition
81
In a play
- name: Use role in loop
include_role:
name: myrole
with_items:
- '{{ roleinput1 }}'
- '{{ roleinput2 }}'
- name: Conditional role
include_role:
name: myrole
when: not some_condition
QUESTIONS?
THANK YOU
plus.google.com/+RedHat
linkedin.com/company/red-hat
youtube.com/user/RedHatVideos
facebook.com/redhatinc
twitter.com/RedHat

More Related Content

What's hot

Introduction to Ansible
Introduction to AnsibleIntroduction to Ansible
Introduction to AnsibleKnoldus Inc.
 
Introduction to ansible
Introduction to ansibleIntroduction to ansible
Introduction to ansibleOmid Vahdaty
 
Load Balancing and Scaling with NGINX
Load Balancing and Scaling with NGINXLoad Balancing and Scaling with NGINX
Load Balancing and Scaling with NGINXNGINX, Inc.
 
02.실전! 시스템 관리자를 위한 Ansible
02.실전! 시스템 관리자를 위한 Ansible02.실전! 시스템 관리자를 위한 Ansible
02.실전! 시스템 관리자를 위한 AnsibleOpennaru, inc.
 
[오픈소스컨설팅]Ansible overview
[오픈소스컨설팅]Ansible overview[오픈소스컨설팅]Ansible overview
[오픈소스컨설팅]Ansible overviewOpen Source Consulting
 
DevOps Meetup ansible
DevOps Meetup   ansibleDevOps Meetup   ansible
DevOps Meetup ansiblesriram_rajan
 
[1A7]Ansible의이해와활용
[1A7]Ansible의이해와활용[1A7]Ansible의이해와활용
[1A7]Ansible의이해와활용NAVER D2
 
Ansible presentation
Ansible presentationAnsible presentation
Ansible presentationJohn Lynch
 
DevOps with Ansible
DevOps with AnsibleDevOps with Ansible
DevOps with AnsibleSwapnil Jain
 
What Is Ansible? | How Ansible Works? | Ansible Tutorial For Beginners | DevO...
What Is Ansible? | How Ansible Works? | Ansible Tutorial For Beginners | DevO...What Is Ansible? | How Ansible Works? | Ansible Tutorial For Beginners | DevO...
What Is Ansible? | How Ansible Works? | Ansible Tutorial For Beginners | DevO...Simplilearn
 
Ansible, best practices
Ansible, best practicesAnsible, best practices
Ansible, best practicesBas Meijer
 
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 ...Simplilearn
 
Introduction to Docker storage, volume and image
Introduction to Docker storage, volume and imageIntroduction to Docker storage, volume and image
Introduction to Docker storage, volume and imageejlp12
 

What's hot (20)

Ansible - Hands on Training
Ansible - Hands on TrainingAnsible - Hands on Training
Ansible - Hands on Training
 
Ansible - Introduction
Ansible - IntroductionAnsible - Introduction
Ansible - Introduction
 
Introduction to Ansible
Introduction to AnsibleIntroduction to Ansible
Introduction to Ansible
 
Introduction to ansible
Introduction to ansibleIntroduction to ansible
Introduction to ansible
 
Load Balancing and Scaling with NGINX
Load Balancing and Scaling with NGINXLoad Balancing and Scaling with NGINX
Load Balancing and Scaling with NGINX
 
02.실전! 시스템 관리자를 위한 Ansible
02.실전! 시스템 관리자를 위한 Ansible02.실전! 시스템 관리자를 위한 Ansible
02.실전! 시스템 관리자를 위한 Ansible
 
Automating with Ansible
Automating with AnsibleAutomating with Ansible
Automating with Ansible
 
[오픈소스컨설팅]Ansible overview
[오픈소스컨설팅]Ansible overview[오픈소스컨설팅]Ansible overview
[오픈소스컨설팅]Ansible overview
 
DevOps Meetup ansible
DevOps Meetup   ansibleDevOps Meetup   ansible
DevOps Meetup ansible
 
[1A7]Ansible의이해와활용
[1A7]Ansible의이해와활용[1A7]Ansible의이해와활용
[1A7]Ansible의이해와활용
 
Ansible presentation
Ansible presentationAnsible presentation
Ansible presentation
 
DevOps with Ansible
DevOps with AnsibleDevOps with Ansible
DevOps with Ansible
 
What Is Ansible? | How Ansible Works? | Ansible Tutorial For Beginners | DevO...
What Is Ansible? | How Ansible Works? | Ansible Tutorial For Beginners | DevO...What Is Ansible? | How Ansible Works? | Ansible Tutorial For Beginners | DevO...
What Is Ansible? | How Ansible Works? | Ansible Tutorial For Beginners | DevO...
 
Ansible get started
Ansible get startedAnsible get started
Ansible get started
 
Ansible
AnsibleAnsible
Ansible
 
Ansible, best practices
Ansible, best practicesAnsible, best practices
Ansible, best practices
 
Ansible
AnsibleAnsible
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 ...
 
Introduction to Docker storage, volume and image
Introduction to Docker storage, volume and imageIntroduction to Docker storage, volume and image
Introduction to Docker storage, volume and image
 

Similar to Getting started with Ansible

#OktoCampus - Workshop : An introduction to Ansible
#OktoCampus - Workshop : An introduction to Ansible#OktoCampus - Workshop : An introduction to Ansible
#OktoCampus - Workshop : An introduction to AnsibleCédric Delgehier
 
Zero Downtime Deployment with Ansible
Zero Downtime Deployment with AnsibleZero Downtime Deployment with Ansible
Zero Downtime Deployment with AnsibleStein Inge Morisbak
 
Ansible with oci
Ansible with ociAnsible with oci
Ansible with ociDonghuKIM2
 
Ansible Tutorial.pdf
Ansible Tutorial.pdfAnsible Tutorial.pdf
Ansible Tutorial.pdfNigussMehari4
 
Ansible is the simplest way to automate. MoldCamp, 2015
Ansible is the simplest way to automate. MoldCamp, 2015Ansible is the simplest way to automate. MoldCamp, 2015
Ansible is the simplest way to automate. MoldCamp, 2015Alex S
 
Zero Downtime Deployment with Ansible
Zero Downtime Deployment with AnsibleZero Downtime Deployment with Ansible
Zero Downtime Deployment with AnsibleStein Inge Morisbak
 
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
 
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
 
Introduction to Ansible - Peter Halligan
Introduction to Ansible - Peter HalliganIntroduction to Ansible - Peter Halligan
Introduction to Ansible - Peter HalliganCorkOpenTech
 
Ansible new paradigms for orchestration
Ansible new paradigms for orchestrationAnsible new paradigms for orchestration
Ansible new paradigms for orchestrationPaolo Tonin
 
A tour of Ansible
A tour of AnsibleA tour of Ansible
A tour of AnsibleDevOps Ltd.
 
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
 
How to deploy spark instance using ansible 2.0 in fiware lab v2
How to deploy spark instance using ansible 2.0 in fiware lab v2How to deploy spark instance using ansible 2.0 in fiware lab v2
How to deploy spark instance using ansible 2.0 in fiware lab v2Fernando Lopez Aguilar
 
How to Deploy Spark Instance Using Ansible 2.0 in FIWARE Lab
How to Deploy Spark Instance Using Ansible 2.0 in FIWARE LabHow to Deploy Spark Instance Using Ansible 2.0 in FIWARE Lab
How to Deploy Spark Instance Using Ansible 2.0 in FIWARE LabFIWARE
 
Ansible roles done right
Ansible roles done rightAnsible roles done right
Ansible roles done rightDan Vaida
 
Getting Started with Ansible
Getting Started with AnsibleGetting Started with Ansible
Getting Started with Ansibleahamilton55
 
Ansible Workshop for Pythonistas
Ansible Workshop for PythonistasAnsible Workshop for Pythonistas
Ansible Workshop for PythonistasMihai Criveti
 
2017-03-11 02 Денис Нелюбин. Docker & Ansible - лучшие друзья DevOps
2017-03-11 02 Денис Нелюбин. Docker & Ansible - лучшие друзья DevOps2017-03-11 02 Денис Нелюбин. Docker & Ansible - лучшие друзья DevOps
2017-03-11 02 Денис Нелюбин. Docker & Ansible - лучшие друзья DevOpsОмские ИТ-субботники
 

Similar to Getting started with Ansible (20)

#OktoCampus - Workshop : An introduction to Ansible
#OktoCampus - Workshop : An introduction to Ansible#OktoCampus - Workshop : An introduction to Ansible
#OktoCampus - Workshop : An introduction to Ansible
 
Zero Downtime Deployment with Ansible
Zero Downtime Deployment with AnsibleZero Downtime Deployment with Ansible
Zero Downtime Deployment with Ansible
 
Ansible with oci
Ansible with ociAnsible with oci
Ansible with oci
 
Ansible Tutorial.pdf
Ansible Tutorial.pdfAnsible Tutorial.pdf
Ansible Tutorial.pdf
 
Ansible is the simplest way to automate. MoldCamp, 2015
Ansible is the simplest way to automate. MoldCamp, 2015Ansible is the simplest way to automate. MoldCamp, 2015
Ansible is the simplest way to automate. MoldCamp, 2015
 
Zero Downtime Deployment with Ansible
Zero Downtime Deployment with AnsibleZero Downtime Deployment with Ansible
Zero Downtime Deployment with Ansible
 
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)
 
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
 
Introduction to Ansible - Peter Halligan
Introduction to Ansible - Peter HalliganIntroduction to Ansible - Peter Halligan
Introduction to Ansible - Peter Halligan
 
Ansible new paradigms for orchestration
Ansible new paradigms for orchestrationAnsible new paradigms for orchestration
Ansible new paradigms for orchestration
 
A tour of Ansible
A tour of AnsibleA tour of Ansible
A tour of Ansible
 
Ansible
AnsibleAnsible
Ansible
 
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
 
How to deploy spark instance using ansible 2.0 in fiware lab v2
How to deploy spark instance using ansible 2.0 in fiware lab v2How to deploy spark instance using ansible 2.0 in fiware lab v2
How to deploy spark instance using ansible 2.0 in fiware lab v2
 
How to Deploy Spark Instance Using Ansible 2.0 in FIWARE Lab
How to Deploy Spark Instance Using Ansible 2.0 in FIWARE LabHow to Deploy Spark Instance Using Ansible 2.0 in FIWARE Lab
How to Deploy Spark Instance Using Ansible 2.0 in FIWARE Lab
 
Ansible roles done right
Ansible roles done rightAnsible roles done right
Ansible roles done right
 
Angular2 ecosystem
Angular2 ecosystemAngular2 ecosystem
Angular2 ecosystem
 
Getting Started with Ansible
Getting Started with AnsibleGetting Started with Ansible
Getting Started with Ansible
 
Ansible Workshop for Pythonistas
Ansible Workshop for PythonistasAnsible Workshop for Pythonistas
Ansible Workshop for Pythonistas
 
2017-03-11 02 Денис Нелюбин. Docker & Ansible - лучшие друзья DevOps
2017-03-11 02 Денис Нелюбин. Docker & Ansible - лучшие друзья DevOps2017-03-11 02 Денис Нелюбин. Docker & Ansible - лучшие друзья DevOps
2017-03-11 02 Денис Нелюбин. Docker & Ansible - лучшие друзья DevOps
 

Recently uploaded

%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 

Recently uploaded (20)

%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 

Getting started with Ansible

  • 1. Getting started with Ansible Alexander Saprykin Senior Software Engineer 13th October 2018
  • 2. 2 Introduction What is Ansible? Ansible history Diving into Ansible roles Basic concepts Inventory Playbook Role Module Plugin Agenda Getting started Create a role Roles under the hood How to use roles?
  • 5. 5 What is inventory? Inventory ● Defines the infrastructure ● Static inventory - can be sourced from a text file (INI or YAML format) ● Dynamic inventory - generated from a script ● Ansible provides dozens of inventory scripts (e.g. AWS EC2, OpenStack, Docker): https://github.com/ansible/ansible/tree/devel/contrib/inventory
  • 12. 12 What is playbook? Playbook ● A yaml document ● Defines set of plays ● Plays bring together inventory and tasks
  • 13. 13 Playbook - name: install and start nginx hosts: web become: yes vars: nginx_packages: [nginx, python-pip, python-devel, gcc] nginx_test_message: This is a test message nginx_keepalive_timeout: 115 tasks: - name: install nginx packages yum: name="{{ item }}" state=present with_items: "{{ nginx_packages }}" - name: install uwsgi pip: name=uwsgi state=present
  • 14. 14 Playbook - name: install and start nginx hosts: web become: yes vars: nginx_packages: [nginx, python-pip, python-devel, gcc] nginx_test_message: This is a test message nginx_keepalive_timeout: 115 tasks: - name: install nginx packages yum: name="{{ item }}" state=present with_items: "{{ nginx_packages }}" - name: install uwsgi pip: name=uwsgi state=present
  • 15. 15 Playbook - name: install and start nginx hosts: web become: yes vars: nginx_packages: [nginx, python-pip, python-devel, gcc] nginx_test_message: This is a test message nginx_keepalive_timeout: 115 tasks: - name: install nginx packages yum: name="{{ item }}" state=present with_items: "{{ nginx_packages }}" - name: install uwsgi pip: name=uwsgi state=present
  • 16. 16 Playbook - name: install and start nginx hosts: web become: yes vars: nginx_packages: [nginx, python-pip, python-devel, gcc] nginx_test_message: This is a test message nginx_keepalive_timeout: 115 tasks: - name: install nginx packages yum: name="{{ item }}" state=present with_items: "{{ nginx_packages }}" - name: install uwsgi pip: name=uwsgi state=present
  • 17. 17 Playbook - name: install and start nginx hosts: web become: yes vars: nginx_packages: [nginx, python-pip, python-devel, gcc] nginx_test_message: This is a test message nginx_keepalive_timeout: 115 tasks: - name: install nginx packages yum: name="{{ item }}" state=present with_items: "{{ nginx_packages }}" - name: install uwsgi pip: name=uwsgi state=present
  • 18. 18 Playbook - name: install and start nginx hosts: web become: yes vars: nginx_packages: [nginx, python-pip, python-devel, gcc] nginx_test_message: This is a test message nginx_keepalive_timeout: 115 tasks: - name: install nginx packages yum: name="{{ item }}" state=present with_items: "{{ nginx_packages }}" - name: install uwsgi pip: name=uwsgi state=present
  • 19. 19 Playbook - name: install and start nginx hosts: web become: yes vars: nginx_packages: [nginx, python-pip, python-devel, gcc] nginx_test_message: This is a test message nginx_keepalive_timeout: 115 tasks: - name: install nginx packages yum: name="{{ item }}" state=present with_items: "{{ nginx_packages }}" - name: install uwsgi pip: name=uwsgi state=present
  • 20. 20 Playbook - name: install and start nginx hosts: web become: yes vars: nginx_packages: [nginx, python-pip, python-devel, gcc] nginx_test_message: This is a test message nginx_keepalive_timeout: 115 tasks: - name: install nginx packages yum: name="{{ item }}" state=present with_items: "{{ nginx_packages }}" - name: install uwsgi pip: name=uwsgi state=present
  • 21. 21 Playbook - name: install and start nginx hosts: web become: yes vars: nginx_packages: [nginx, python-pip, python-devel, gcc] nginx_test_message: This is a test message nginx_keepalive_timeout: 115 tasks: - name: install nginx packages yum: name="{{ item }}" state=present with_items: "{{ nginx_packages }}" - name: install uwsgi pip: name=uwsgi state=present
  • 22. 22 Playbook - name: install and start nginx hosts: web become: yes vars: nginx_packages: [nginx, python-pip, python-devel, gcc] nginx_test_message: This is a test message nginx_keepalive_timeout: 115 tasks: - name: install nginx packages yum: name="{{ item }}" state=present with_items: "{{ nginx_packages }}" - name: install uwsgi pip: name=uwsgi state=present
  • 23. 23 Playbook - name: install and start nginx hosts: web become: yes vars: nginx_packages: [nginx, python-pip, python-devel, gcc] nginx_test_message: This is a test message nginx_keepalive_timeout: 115 tasks: - name: install nginx packages yum: name="{{ item }}" state=present with_items: "{{ nginx_packages }}" - name: install uwsgi pip: name=uwsgi state=present
  • 24. 24 Playbook - name: install and start nginx hosts: web become: yes vars: nginx_packages: [nginx, python-pip, python-devel, gcc] nginx_test_message: This is a test message nginx_keepalive_timeout: 115 tasks: - name: install nginx packages yum: name="{{ item }}" state=present with_items: "{{ nginx_packages }}" - name: install uwsgi pip: name=uwsgi state=present with_items: Loops through the list, executing the task for each item.
  • 25. 25 Include ● Import a file containing a list of tasks ● Pass parameters ● Dynamic ● Conditional ● Encourages code reuse
  • 26. 26 Include - name: Add hostname include: add_hostname.yml - name: Add hostname include: add_hostname.yml param: ”{{ item }}” with_items: [1, 2, 3] - name: Get the client assets include: “{{ ansible_os_family }}.yml”
  • 27. 27 Include - name: Add hostname include: add_hostname.yml - name: Add hostname include: add_hostname.yml param: ”{{ item }}” with_items: [1, 2, 3] - name: Get the client assets include: “{{ ansible_os_family }}.yml”
  • 28. 28 Include - name: Add hostname include: add_hostname.yml - name: Add hostname include: add_hostname.yml param: ”{{ item }}” with_items: [1, 2, 3] - name: Get the client assets include: “{{ ansible_os_family }}.yml”
  • 29. 29 Include - name: Add hostname include: add_hostname.yml - name: Add hostname include: add_hostname.yml param: ”{{ item }}” with_items: [1, 2, 3] - name: Get the client assets include: “{{ ansible_os_family }}.yml”
  • 30. 30 Role ● Self-contained, reusable, complete unit of work ● Decoupled from assumptions made by plays ● Decoupled from inventory ● Encourages collaboration
  • 31. 31 Module ● Called by a task (or used ad-hoc) ● Perform an action on a target host ● Can take direct action, wrap a command line tool, or talk to an API ● Ansible includes a 100’s of modules: https://github.com/ansible/ansible/tree/devel/lib/ansible/modules
  • 32. 32 Facts ● Provided by setup module ● Returned by modules ● Created using set_fact module ● Use for variable substitution, and conditional checks
  • 33. 33 Plugin ● Augments Ansible core functionality ● Plugin types: action, cache, callback, connection, filter, lookup, shell, strategy, terminal, test, vars ● Examples: ○ connection: local, ssh, docker, chroot ○ action: copy, fetch, synchronize ● See full list at https://github.com/ansible/ansible/tree/devel/lib/ansible/plugins
  • 36. 36 Playbooks ● Made up of plays ● Plays are opinionated: become, gather_facts, connection, vars, etc. ● Assume a specific inventory ● Target a specific use case ● Generally not reusable
  • 37. 37 Example playbook - name: install uwsgi pip: name=uwsgi state=present - name: copy default.conf template: src=templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf > backup=yes notify: start nginx - name: copy index.html template: src=templates/index.html.j2 > dest=/usr/share/nginx/html/index.html notify: start nginx # ...
  • 38. 38 Example playbook # ... - name: get response uri: url=http://localhost/ return_content: yes register: response until: 'nginx_test_message in response.content' retries: 10 delay: 1 handlers: - name: start nginx service: name=nginx state=started enabled=yes
  • 39. 39 Roles ● Decoupled from inventory and plays ● Not Just a set of tasks ● Self-contained, reusable, complete unit of work
  • 40. 40 ● Install packages ● Update configuration ● Run tests ● Package software ● Build images ● Orchestrate containers What you can do with roles?
  • 41. 41 Switch to a role - name: install and start nginx hosts: web become: yes roles: - role: install-nginx packages: - nginx - python-pip - python-devel - gcc nginx_test_message: This is a test message nginx_keepalive_timeout: 115
  • 42. 42 Switch to a role - name: install and start nginx hosts: web become: yes roles: - role: install-nginx packages: - nginx - python-pip - python-devel - gcc nginx_test_message: This is a test message nginx_keepalive_timeout: 115
  • 43. 43 Switch to a role - name: install and start nginx hosts: web become: yes roles: - role: install-nginx packages: - nginx - python-pip - python-devel - gcc nginx_test_message: This is a test message nginx_keepalive_timeout: 115
  • 44. 44 Switch to a role - name: install and start nginx hosts: web become: yes roles: - role: install-nginx packages: - nginx - python-pip - python-devel - gcc nginx_test_message: This is a test message nginx_keepalive_timeout: 115
  • 46. 46 From scratch Create a role 1. Create a roles directory 2. Create a minimal role directory structure: mkdir -p ./roles/nginx-install/tasks/ 3. Start writing your tasks in main.yml file in tasks directory
  • 47. 47 From template Create a role ● Ansible Galaxy client tool ansible-galaxy init nginx-install ● Creates a complete directory structure ● Creates default files
  • 48. 48 From Ansible Galaxy (...or more) Or just download one ● From Ansible Galaxy - https://galaxy.ansible.com ansible-galaxy install <namespace>.<role-name> ● From Git ansible-galaxy install git+https://github.com/acme/nginx-install.git ● ...
  • 49. 49 Where are my roles? ● ANSIBLE_ROLES_PATH ● ansible.cfg ● Provide a colon : separated list of paths ● roles directory next to the playbook [defaults] roles_path=/path/to/roles
  • 51. 51 Role structure roles/ └── install-nginx/ ├── .travis.yml ├── README.md ├── defaults/ │ └── main.yml ├── files/ ├── handles/ ├── meta/ ├── tasks/ │ └── main.yml ├── templates/ ├── tests/ └── vars/
  • 52. 52 Tasks ● tasks directory ● Entrypoint: tasks/main.yml ● Contains the main list of tasks to be executed by the role ● Tie together handlers, templates, files, variables and defaults
  • 53. 53 Tasks - name: install nginx packages yum: name="{{ item }}" state=present with_items: "{{ nginx_packages }}" - name: install uwsgi pip: name=uwsgi state=present - name: copy default.conf template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf backup=yes ...
  • 54. 54 ● handlers directory ● Entrypoint: handlers/main.yml ● Module indicates when a change has been made ● In response to a change, a notify action can be triggered ● Notify handlers by name… ● … or by topic - new in Ansible 2.2 Handlers
  • 55. ... - name: copy index.html template: src=index.html.j2 dest=/usr/share/nginx/html/index.html notify: start nginx ... 55 Handlers Task from tasks/main.yml
  • 56. - name: start nginx service: name=nginx state=started enabled=yes 56 Handlers by name Handler from handlers/main.yml
  • 57. - name: start and enable nginx service: name=nginx state=started enabled=yes listen: start nginx - name: Restart and enable supervisord service: name=supervisord state=restarted enabled=yes listen: start nginx 57 Handlers by topic Handler from handlers/main.yml
  • 58. 58 ● files is the base directory for copy and synchronize modules ● Files are copied to the target node ● templates is the base directory for the template module ● Templates contain variables - during execution, the file is transformed, and the result is copied to the target node ● Ansible uses Jinja2 as a template engine: https://docs.ansible.com/ansible/latest/user_guide/playbooks_templating.html Files and templates
  • 59. roles/ └── install-nginx/ ├── ... └── templates/ ├── index.html.j2 └── nginx.conf.j2 59 Templates
  • 60. - name: copy default.conf template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf backup=yes - name: copy index.html template: src=index.html.j2 dest=/usr/share/nginx/html/index.html notify: start nginx 60 Templates tasks/main.yml
  • 61. ... http { ... sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout {{ nginx_keepalive_timeout }}; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf; 61 Templates templates/nginx.conf.j2
  • 62. ... <body> <div class="container"> <img src="/static/images/happy-cow.png"/> <p>{{ nginx_test_message }}</p> </div> <footer>Ansible by Red Hat</footer> </body> </html> ... 62 Templates templates/index.html.j2
  • 63. 63 ● defaults/main.yml ○ Defines variables the user can override to change role behavior (e.g. conditionals, configuration settings) ● variables/main.yml ○ Used by the author to organize the role (e.g. constants, choices) ○ Add additional files to dynamically shape the role Variables
  • 64. packages: - nginx - python-pip - python-devel - gcc nginx_test_message: Hello World! nginx_keepalive_timeout: 65 64 Templates defaults/main.yml
  • 65. 65 ● meta/main.yml ● Resolved at install ● Executed before the role ● Recursive ● Each dependency executed once only Dependencies
  • 66. --- dependencies: - { role: common, some_parameter: 3 } - { role: apache, apache_port: 80 } - { role: postgres, dbname: blarg, other_parameter: 12 } 66 Dependencies meta/main.yml
  • 67. ... dependencies: - src: git+https://github.com/redhat/ansible-role-common.git version: v1.0.0 name: common some_parameter: 3 67 Dependencies From SCM
  • 69. 69 ● README.md ● meta/main.yml ● Example playbook Documentation
  • 70. ● library directory - add custom modules ● See: https://docs.ansible.com/ansible/latest/dev_guide/developing_plugins.html ● <type>_plugin - add custom plugin ● See: https://docs.ansible.com/ansible/2.5/dev_guide/developing_modules.html 70 Modules and plugins
  • 71. 71 ● Jeff Geerling (https://github.com/geerlingguy) ○ https://github.com/geerlingguy?tab=repositories&q=ansible-role ● DebOps https://github.com/debops/debops Some examples
  • 72. 72 How to use roles?
  • 73. 73 ● Play can include roles and tasks ○ Roles are executed first, then tasks ○ For readability, list roles first ● Consider surfacing all defaults in the playbook ○ Make the playbook self-documenting In a play
  • 74. 74 In a play hosts: web name: install and start nginx with wsgi become: yes roles: - role: install-nginx packages: - nginx - python-pip - python-devel - gcc nginx_test_message: This is a test message nginx_keepalive_timeout: 115
  • 75. 75 ● New in Ansible 2.2 ● include_role ● Treats the role more like a task In a task
  • 76. 76 In a play - name: Run my role include_role: name: myrole - name: Run tasks/other.yml instead of 'main' include_role: name: myrole tasks_from: other - name: Pass variables to role include_role: name: myrole vars: rolevar1: 'value from task'
  • 77. 77 In a play - name: Run my role include_role: name: myrole - name: Run tasks/other.yml instead of 'main' include_role: name: myrole tasks_from: other - name: Pass variables to role include_role: name: myrole vars: rolevar1: 'value from task'
  • 78. 78 In a play - name: Run my role include_role: name: myrole - name: Run tasks/other.yml instead of 'main' include_role: name: myrole tasks_from: other - name: Pass variables to role include_role: name: myrole vars: rolevar1: 'value from task'
  • 79. 79 In a play - name: Run my role include_role: name: myrole - name: Run tasks/other.yml instead of 'main' include_role: name: myrole tasks_from: other - name: Pass variables to role include_role: name: myrole vars: rolevar1: 'value from task'
  • 80. 80 In a play - name: Use role in loop include_role: name: myrole with_items: - '{{ roleinput1 }}' - '{{ roleinput2 }}' - name: Conditional role include_role: name: myrole when: not some_condition
  • 81. 81 In a play - name: Use role in loop include_role: name: myrole with_items: - '{{ roleinput1 }}' - '{{ roleinput2 }}' - name: Conditional role include_role: name: myrole when: not some_condition