SlideShare a Scribd company logo
1 of 28
Download to read offline
ANSIBLE BEST PRACTICES AT SCALE
LEARNING THE 10 BEST PRACTICES USED
BY LEADING OSS THAT DEPEND ON
ANSIBLE
Keith Resar
@KeithResar
@KeithResar
Keith Resar: Bio
Wear many hats
@KeithResar Keith.Resar@RedHat.com
Coder
Open Source Contributor and Advocate
Infrastructure Architect
ANSIBLE WAS MADE TO HELP MORE
PEOPLE EXPERIENCE THE POWER OF
AUTOMATION SO THEY COULD WORK
BETTER AND FASTER TOGETHER
The Open Source Container Application
Platform.
Built around a core of Docker container
packaging and Kubernetes container cluster
management, Origin is also augmented by
application lifecycle management functionality
and DevOps tooling. Origin provides a
complete open source container application
platform.
YOUR LOOK INSIDE
HOW OPENSHIFT DOES ANSIBLE
ANSIBLE FILES SHOULD NOT USE JSON
(USE PURE YAML INSTEAD)
RULE
1
@KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Ansible-files-SHOULD-NOT-use-JSON-use-pure-YAML-instead
- foo
- bar:
- baz
- kwa
- 1.0
- 2
[
"foo",
{
"bar": [
"baz",
"kwa",
1,
2
]
}
]
JSON YAML
ANSIBLE FILES SHOULD NOT USE JSON
(USE PURE YAML INSTEAD)
RULE
1
@KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Ansible-files-SHOULD-NOT-use-JSON-use-pure-YAML-instead
YAML is a superset of JSON, which means that Ansible allows JSON
syntax to be interspersed. Even though YAML (and by extension Ansible)
allows for this, JSON SHOULD NOT be used.
Reasons:
โ— Ansible is able to give clearer error messages when the files are pure
YAML
โ— YAML makes for nicer diffs as YAML tends to be multi-line, whereas
JSON tends to be more concise
โ— YAML reads more nicely (opinion?)
3 OR MORE PARAMETERS TO ANSIBLE
MODULES SHOULD USE THE YAML
DICTIONARY FORMAT
RULE
2
@KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Parameters-to-Ansible-modules-SHOULD-use-the-Yaml-dictionary-fo
rmat-when-3-or-more-parameters-are-being-passed
# โœ˜ BAD
- file: src=/file/to/link/to 
dest=/path/to/symlink owner=foo 
group=foo state=link
# โœ” GOOD
- file:
src: /file/to/link/to
dest: /path/to/symlink
owner: foo
group: foo
state: link
3 OR MORE PARAMETERS TO ANSIBLE
MODULES SHOULD USE THE YAML
DICTIONARY FORMAT
RULE
2
@KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Parameters-to-Ansible-modules-SHOULD-use-the-Yaml-dictionary-fo
rmat-when-3-or-more-parameters-are-being-passed
When a module has several parameters that are being passed in, itโ€™s
hard to see exactly what value each parameter is getting.
It is preferred to use the Ansible Yaml syntax to pass in parameters so
that itโ€™s more clear what values are being passed for each parameter.
PARAMETERS TO ANSIBLE MODULES
SHOULD USE THE DICTIONARY FORMAT IF
LINES WOULD EXCEED 120 CHARACTERS
RULE
3
@KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Parameters-to-Ansible-modules-SHOULD-use-the-Yaml-dictionary-fo
rmat-when-the-line-length-exceeds-120-characters
# โœ˜ BAD
- get_url: url=http://example.com/path/file.conf
dest=/etc/foo.conf
sha256sum=b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b8
78ae4944c
# โœ” GOOD
- get_url:
url: http://example.com/path/file.conf
dest: /etc/foo.conf
Sha256sum:
B5bb9d8014a0f9b1d61e21e796d78dc...d32812f4850b878ae4944c
PARAMETERS TO ANSIBLE MODULES
SHOULD USE THE DICTIONARY FORMAT IF
LINES WOULD EXCEED 120 CHARACTERS
RULE
3
@KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Parameters-to-Ansible-modules-SHOULD-use-the-Yaml-dictionary-fo
rmat-when-the-line-length-exceeds-120-characters
Lines that are long quickly become a wall of text that isnโ€™t easily parsable.
It is preferred to use the Ansible Yaml syntax to pass in parameters so
that itโ€™s more clear what values are being passed for each parameter.
THE ANSIBLE COMMAND MODULE SHOULD
BE USED INSTEAD OF THE ANSIBLE SHELL
MODULE
RULE
4
@KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#The-Ansible-command-module-SHOULD-be-used-instead-of-the-Ans
ible-shell-module
# โœ˜ POOR
- name: Bare shell execution
shell: cat myfile
# BETTER
- name: Quoting templated variable to avoid injection
shell: cat {{ myfile | quote }}
# โœ” BEST
- name: Quoting templated variable to avoid injection
command: cat {{ myfile }}
THE ANSIBLE COMMAND MODULE SHOULD
BE USED INSTEAD OF THE ANSIBLE SHELL
MODULE
RULE
4
@KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#The-Ansible-command-module-SHOULD-be-used-instead-of-the-Ans
ible-shell-module
If you want to execute a command securely and predictably, it may be
better to use the command module instead, using the shell module only
when explicitly required.
The Ansible shell module can run most commands that can be run from a
bash CLI. This makes it extremely powerful, but it also opens our
playbooks up to being exploited by attackers.
When running ad-hoc commands, use your best judgement.
ANSIBLE PLAYBOOKS MUST BEGIN WITH
CHECKS FOR ANY VARIABLES THAT THEY
REQUIRE
RULE
5
@KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Ansible-playbooks-MUST-begin-with-checks-for-any-variables-that-th
ey-require
---
- hosts: localhost
gather_facts: no
tasks:
- fail: msg="Playbook requires g_env to be set and non empty"
when: g_env is not defined or g_env == ''
---
# tasks/main.yml
- fail: msg="Role requires arl_env to be set and non empty"
when: arl_env is not defined or arl_env == ''
ANSIBLE PLAYBOOKS MUST BEGIN WITH
CHECKS FOR ANY VARIABLES THAT THEY
REQUIRE
RULE
5
@KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Ansible-playbooks-MUST-begin-with-checks-for-any-variables-that-th
ey-require
If an Ansible playbook or role requires certain variables to be set, itโ€™s best
to check for these up front before any other actions have been performed.
In this way, the user knows exactly what needs to be passed into the
playbook.
ANSIBLE TASKS SHOULD NOT BE USED IN
ANSIBLE PLAYBOOKS. INSTEAD, USE
PRE_TASKS AND POST_TASKS
RULE
6
@KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Ansible-tasks-SHOULD-NOT-be-used-in-ansible-playbooks-Instead-
use-pre_tasks-and-post_tasks
# โœ˜ BAD
- hosts: localhost
tasks:
- name: Executes AFTER the example_role, so itโ€™s confusing
debug: msg="in tasks list"
roles:
- role: example_role
# โœ” GOOD
- hosts: localhost
pre_tasks:
- name: Executes BEFORE the example_role, so it makes sense
debug: msg="in pre_tasks list"
roles:
- role: example_role
ANSIBLE TASKS SHOULD NOT BE USED IN
ANSIBLE PLAYBOOKS. INSTEAD, USE
PRE_TASKS AND POST_TASKS
RULE
6
@KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Ansible-tasks-SHOULD-NOT-be-used-in-ansible-playbooks-Instead-
use-pre_tasks-and-post_tasks
An Ansible play is defined as a Yaml dictionary and because of that
Ansible doesnโ€™t know if the playโ€™s tasks list or roles list was specified first.
Therefore, Ansible always runs tasks after roles.
This can be quite confusing if the tasks list is defined in the playbook
before the roles list because people assume in order execution in
Ansible.
Therefore, we SHOULD use pre_tasks and post_tasks to make it more
clear when the tasks will be run.
ALL TASKS IN A ROLE SHOULD BE TAGGED
WITH THE ROLE NAME
RULE
7
@KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#All-tasks-in-a-role-SHOULD-be-tagged-with-the-role-name
# roles/example_role/tasks/main.yml
- debug: msg="in example_role"
tags:
- example_role
ALL TASKS IN A ROLE SHOULD BE TAGGED
WITH THE ROLE NAME
RULE
7
@KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#All-tasks-in-a-role-SHOULD-be-tagged-with-the-role-name
Ansible tasks can be tagged, and then these tags can be used to either
run or skip the tagged tasks using the --tags and --skip-tags
ansible-playbook options respectively.
This is very useful when developing and debugging new tasks. It can also
significantly speed up playbook runs if the user specifies only the roles
that changed.
THE ANSIBLE ROLES DIRECTORY MUST
MAINTAIN A FLAT STRUCTURE
RULE
8
@KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#The-Ansible-roles-directory-MUST-maintain-a-flat-structure
production # inventory file for production servers
staging # inventory file for staging environment
group_vars/
host_vars/
site.yml # master playbook
webservers.yml # playbook for webserver tier
dbservers.yml # playbook for dbserver tier
roles/
common/ # this hierarchy represents a "role"
tasks/, handlers/, templates/, files/, vars/, defaults/, meta/
THE ANSIBLE ROLES DIRECTORY MUST
MAINTAIN A FLAT STRUCTURE
RULE
8
@KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#The-Ansible-roles-directory-MUST-maintain-a-flat-structure
The purpose of this rule is to:
โ— Comply with the upstream best practices
โ— Make it familiar for new contributors
โ— Make it compatible with Ansible Galaxy
ANSIBLE ROLES SHOULD BE NAMED
TECH_COMPONENT[_SUBCOMPONENT]
RULE
9
@KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Ansible-Roles-SHOULD-be-named-like-technology_component_subc
omponent
roles/
# this hierarchy represents a "role"
common/
# โœ˜ BAD
database/
# โœ” GOOD
mysql_slave/
ANSIBLE ROLES SHOULD BE NAMED
TECH_COMPONENT[_SUBCOMPONENT]
RULE
9
@KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Ansible-Roles-SHOULD-be-named-like-technology_component_subc
omponent
For consistency, role names SHOULD follow the above naming pattern. It
is important to note that this is a recommendation for role naming, and
follows the pattern used by upstream.
Many times the technology portion of the pattern will line up with a
package name. It is advised that whenever possible, the package name
should be used.
THE DEFAULT FILTER SHOULD REPLACE
EMPTY STRINGS, LISTS, ETC
RULE
10
@KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#The-default-filter-SHOULD-replace-empty-strings-lists-etc
- hosts: localhost
gather_facts: no
vars:
somevar: ''
tasks:
- debug: var=somevar
# โœ˜ BAD
- name: "Will output 'somevar: []'"
debug: "msg='somevar: [{{ somevar | default('empty str') }}]'"
# โœ” GOOD
- name: "Will output 'somevar: [the string was empty]'"
debug: "msg='somevar: [{{ somevar | default('empty str', true)}}]'"
THE DEFAULT FILTER SHOULD REPLACE
EMPTY STRINGS, LISTS, ETC
RULE
10
@KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#The-default-filter-SHOULD-replace-empty-strings-lists-etc
When using the jinja2 default filter, unless the variable is a boolean,
specify true as the second parameter. This will cause the default filter to
replace empty strings, lists, etc with the provided default rather than only
undefined variables.
This is because it is preferable to either have a sane default set than to
have an empty string, list, etc. For example, it is preferable to have a
config value set to a sane default than to have it simply set as an empty
string.
AUTOMATION FOR EVERYONE
ANSIBLE IS DESIGNED AROUND THE WAY
PEOPLE WORK AND THE WAY PEOPLE
WORK TOGETHER.
RESOURCES
OPENSHIFT ORIGIN
https://www.openshift.org
OPENSHIFT ANSIBLE BEST PRACTICES
https://github.com/../docs/best_practices_guide
ANSIBLE MINNEAPOLIS MEETUP
https://www.meetup.com/Ansible-Minneapolis/
@KeithResar
@KeithResar
THANKS!

More Related Content

Viewers also liked

Viewers also liked (20)

Kubernetes and lastminute.com: our course towards better scalability and proc...
Kubernetes and lastminute.com: our course towards better scalability and proc...Kubernetes and lastminute.com: our course towards better scalability and proc...
Kubernetes and lastminute.com: our course towards better scalability and proc...
ย 
Coding in the context era
Coding in the context eraCoding in the context era
Coding in the context era
ย 
Red Hat OpenShift on Bare Metal and Containerized Storage
Red Hat OpenShift on Bare Metal and Containerized StorageRed Hat OpenShift on Bare Metal and Containerized Storage
Red Hat OpenShift on Bare Metal and Containerized Storage
ย 
Network Automation: Ansible 102
Network Automation: Ansible 102Network Automation: Ansible 102
Network Automation: Ansible 102
ย 
Automated Deployments with Ansible
Automated Deployments with AnsibleAutomated Deployments with Ansible
Automated Deployments with Ansible
ย 
Monitoring, Logging and Tracing on Kubernetes
Monitoring, Logging and Tracing on KubernetesMonitoring, Logging and Tracing on Kubernetes
Monitoring, Logging and Tracing on Kubernetes
ย 
How to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your NicheHow to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your Niche
ย 
DevConf 2017 - Realistic Container Platform Simulations
DevConf 2017 - Realistic Container Platform SimulationsDevConf 2017 - Realistic Container Platform Simulations
DevConf 2017 - Realistic Container Platform Simulations
ย 
What is Deep Learning?
What is Deep Learning?What is Deep Learning?
What is Deep Learning?
ย 
Docker Deployments for the Enterprise
Docker Deployments for the EnterpriseDocker Deployments for the Enterprise
Docker Deployments for the Enterprise
ย 
Ansible for Enterprise
Ansible for EnterpriseAnsible for Enterprise
Ansible for Enterprise
ย 
Automation and ansible
Automation and ansibleAutomation and ansible
Automation and ansible
ย 
The Marketer's Guide To Customer Interviews
The Marketer's Guide To Customer InterviewsThe Marketer's Guide To Customer Interviews
The Marketer's Guide To Customer Interviews
ย 
Software Defined Datacenter
Software Defined DatacenterSoftware Defined Datacenter
Software Defined Datacenter
ย 
DevOps: Arquitectura, Estrategia y Modelo
DevOps: Arquitectura, Estrategia y ModeloDevOps: Arquitectura, Estrategia y Modelo
DevOps: Arquitectura, Estrategia y Modelo
ย 
Red Hat OpenShift Container Platform Overview
Red Hat OpenShift Container Platform OverviewRed Hat OpenShift Container Platform Overview
Red Hat OpenShift Container Platform Overview
ย 
Automate with Ansible basic (3/e)
Automate with Ansible basic (3/e)Automate with Ansible basic (3/e)
Automate with Ansible basic (3/e)
ย 
Docker Berlin Meetup Nov 2015: Zalando Intro
Docker Berlin Meetup Nov 2015: Zalando IntroDocker Berlin Meetup Nov 2015: Zalando Intro
Docker Berlin Meetup Nov 2015: Zalando Intro
ย 
STIG Compliance and Remediation with Ansible
STIG Compliance and Remediation with AnsibleSTIG Compliance and Remediation with Ansible
STIG Compliance and Remediation with Ansible
ย 
From zero to exit: a full startup journey
From zero to exit: a full startup journeyFrom zero to exit: a full startup journey
From zero to exit: a full startup journey
ย 

Recently uploaded

๐Ÿ’š๐Ÿ˜‹ Salem Escort Service Call Girls, 9352852248 โ‚น5000 To 25K With AC๐Ÿ’š๐Ÿ˜‹
๐Ÿ’š๐Ÿ˜‹ Salem Escort Service Call Girls, 9352852248 โ‚น5000 To 25K With AC๐Ÿ’š๐Ÿ˜‹๐Ÿ’š๐Ÿ˜‹ Salem Escort Service Call Girls, 9352852248 โ‚น5000 To 25K With AC๐Ÿ’š๐Ÿ˜‹
๐Ÿ’š๐Ÿ˜‹ Salem Escort Service Call Girls, 9352852248 โ‚น5000 To 25K With AC๐Ÿ’š๐Ÿ˜‹
nirzagarg
ย 
Lucknow โคCALL GIRL 88759*99948 โคCALL GIRLS IN Lucknow ESCORT SERVICEโคCALL GIRL
Lucknow โคCALL GIRL 88759*99948 โคCALL GIRLS IN Lucknow ESCORT SERVICEโคCALL GIRLLucknow โคCALL GIRL 88759*99948 โคCALL GIRLS IN Lucknow ESCORT SERVICEโคCALL GIRL
Lucknow โคCALL GIRL 88759*99948 โคCALL GIRLS IN Lucknow ESCORT SERVICEโคCALL GIRL
imonikaupta
ย 
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...
SUHANI PANDEY
ย 
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
SUHANI PANDEY
ย 
Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...
Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...
Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...
SUHANI PANDEY
ย 
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
ย 
valsad Escorts Service โ˜Ž๏ธ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service โ˜Ž๏ธ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service โ˜Ž๏ธ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service โ˜Ž๏ธ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
Call Girls In Delhi Whatsup 9873940964 Enjoy Unlimited Pleasure
ย 
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
SUHANI PANDEY
ย 

Recently uploaded (20)

"Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency""Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
ย 
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
ย 
๐Ÿ’š๐Ÿ˜‹ Salem Escort Service Call Girls, 9352852248 โ‚น5000 To 25K With AC๐Ÿ’š๐Ÿ˜‹
๐Ÿ’š๐Ÿ˜‹ Salem Escort Service Call Girls, 9352852248 โ‚น5000 To 25K With AC๐Ÿ’š๐Ÿ˜‹๐Ÿ’š๐Ÿ˜‹ Salem Escort Service Call Girls, 9352852248 โ‚น5000 To 25K With AC๐Ÿ’š๐Ÿ˜‹
๐Ÿ’š๐Ÿ˜‹ Salem Escort Service Call Girls, 9352852248 โ‚น5000 To 25K With AC๐Ÿ’š๐Ÿ˜‹
ย 
Lucknow โคCALL GIRL 88759*99948 โคCALL GIRLS IN Lucknow ESCORT SERVICEโคCALL GIRL
Lucknow โคCALL GIRL 88759*99948 โคCALL GIRLS IN Lucknow ESCORT SERVICEโคCALL GIRLLucknow โคCALL GIRL 88759*99948 โคCALL GIRLS IN Lucknow ESCORT SERVICEโคCALL GIRL
Lucknow โคCALL GIRL 88759*99948 โคCALL GIRLS IN Lucknow ESCORT SERVICEโคCALL GIRL
ย 
Hireโ† Young Call Girls in Tilak nagar (Delhi) โ˜Ž๏ธ 9205541914 โ˜Ž๏ธ Independent Esc...
Hireโ† Young Call Girls in Tilak nagar (Delhi) โ˜Ž๏ธ 9205541914 โ˜Ž๏ธ Independent Esc...Hireโ† Young Call Girls in Tilak nagar (Delhi) โ˜Ž๏ธ 9205541914 โ˜Ž๏ธ Independent Esc...
Hireโ† Young Call Girls in Tilak nagar (Delhi) โ˜Ž๏ธ 9205541914 โ˜Ž๏ธ Independent Esc...
ย 
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
ย 
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...
ย 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53
ย 
20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf
ย 
Katraj ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
Katraj ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...Katraj ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...
Katraj ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
ย 
Real Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirtReal Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirt
ย 
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
ย 
Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...
Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...
Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...
ย 
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
ย 
Microsoft Azure Arc Customer Deck Microsoft
Microsoft Azure Arc Customer Deck MicrosoftMicrosoft Azure Arc Customer Deck Microsoft
Microsoft Azure Arc Customer Deck Microsoft
ย 
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
ย 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
ย 
valsad Escorts Service โ˜Ž๏ธ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service โ˜Ž๏ธ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service โ˜Ž๏ธ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service โ˜Ž๏ธ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
ย 
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
ย 
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
ย 

Ansible Best Practices at Scale

  • 1. ANSIBLE BEST PRACTICES AT SCALE LEARNING THE 10 BEST PRACTICES USED BY LEADING OSS THAT DEPEND ON ANSIBLE Keith Resar @KeithResar
  • 2. @KeithResar Keith Resar: Bio Wear many hats @KeithResar Keith.Resar@RedHat.com Coder Open Source Contributor and Advocate Infrastructure Architect
  • 3. ANSIBLE WAS MADE TO HELP MORE PEOPLE EXPERIENCE THE POWER OF AUTOMATION SO THEY COULD WORK BETTER AND FASTER TOGETHER
  • 4. The Open Source Container Application Platform. Built around a core of Docker container packaging and Kubernetes container cluster management, Origin is also augmented by application lifecycle management functionality and DevOps tooling. Origin provides a complete open source container application platform.
  • 5. YOUR LOOK INSIDE HOW OPENSHIFT DOES ANSIBLE
  • 6. ANSIBLE FILES SHOULD NOT USE JSON (USE PURE YAML INSTEAD) RULE 1 @KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Ansible-files-SHOULD-NOT-use-JSON-use-pure-YAML-instead - foo - bar: - baz - kwa - 1.0 - 2 [ "foo", { "bar": [ "baz", "kwa", 1, 2 ] } ] JSON YAML
  • 7. ANSIBLE FILES SHOULD NOT USE JSON (USE PURE YAML INSTEAD) RULE 1 @KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Ansible-files-SHOULD-NOT-use-JSON-use-pure-YAML-instead YAML is a superset of JSON, which means that Ansible allows JSON syntax to be interspersed. Even though YAML (and by extension Ansible) allows for this, JSON SHOULD NOT be used. Reasons: โ— Ansible is able to give clearer error messages when the files are pure YAML โ— YAML makes for nicer diffs as YAML tends to be multi-line, whereas JSON tends to be more concise โ— YAML reads more nicely (opinion?)
  • 8. 3 OR MORE PARAMETERS TO ANSIBLE MODULES SHOULD USE THE YAML DICTIONARY FORMAT RULE 2 @KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Parameters-to-Ansible-modules-SHOULD-use-the-Yaml-dictionary-fo rmat-when-3-or-more-parameters-are-being-passed # โœ˜ BAD - file: src=/file/to/link/to dest=/path/to/symlink owner=foo group=foo state=link # โœ” GOOD - file: src: /file/to/link/to dest: /path/to/symlink owner: foo group: foo state: link
  • 9. 3 OR MORE PARAMETERS TO ANSIBLE MODULES SHOULD USE THE YAML DICTIONARY FORMAT RULE 2 @KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Parameters-to-Ansible-modules-SHOULD-use-the-Yaml-dictionary-fo rmat-when-3-or-more-parameters-are-being-passed When a module has several parameters that are being passed in, itโ€™s hard to see exactly what value each parameter is getting. It is preferred to use the Ansible Yaml syntax to pass in parameters so that itโ€™s more clear what values are being passed for each parameter.
  • 10. PARAMETERS TO ANSIBLE MODULES SHOULD USE THE DICTIONARY FORMAT IF LINES WOULD EXCEED 120 CHARACTERS RULE 3 @KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Parameters-to-Ansible-modules-SHOULD-use-the-Yaml-dictionary-fo rmat-when-the-line-length-exceeds-120-characters # โœ˜ BAD - get_url: url=http://example.com/path/file.conf dest=/etc/foo.conf sha256sum=b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b8 78ae4944c # โœ” GOOD - get_url: url: http://example.com/path/file.conf dest: /etc/foo.conf Sha256sum: B5bb9d8014a0f9b1d61e21e796d78dc...d32812f4850b878ae4944c
  • 11. PARAMETERS TO ANSIBLE MODULES SHOULD USE THE DICTIONARY FORMAT IF LINES WOULD EXCEED 120 CHARACTERS RULE 3 @KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Parameters-to-Ansible-modules-SHOULD-use-the-Yaml-dictionary-fo rmat-when-the-line-length-exceeds-120-characters Lines that are long quickly become a wall of text that isnโ€™t easily parsable. It is preferred to use the Ansible Yaml syntax to pass in parameters so that itโ€™s more clear what values are being passed for each parameter.
  • 12. THE ANSIBLE COMMAND MODULE SHOULD BE USED INSTEAD OF THE ANSIBLE SHELL MODULE RULE 4 @KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#The-Ansible-command-module-SHOULD-be-used-instead-of-the-Ans ible-shell-module # โœ˜ POOR - name: Bare shell execution shell: cat myfile # BETTER - name: Quoting templated variable to avoid injection shell: cat {{ myfile | quote }} # โœ” BEST - name: Quoting templated variable to avoid injection command: cat {{ myfile }}
  • 13. THE ANSIBLE COMMAND MODULE SHOULD BE USED INSTEAD OF THE ANSIBLE SHELL MODULE RULE 4 @KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#The-Ansible-command-module-SHOULD-be-used-instead-of-the-Ans ible-shell-module If you want to execute a command securely and predictably, it may be better to use the command module instead, using the shell module only when explicitly required. The Ansible shell module can run most commands that can be run from a bash CLI. This makes it extremely powerful, but it also opens our playbooks up to being exploited by attackers. When running ad-hoc commands, use your best judgement.
  • 14. ANSIBLE PLAYBOOKS MUST BEGIN WITH CHECKS FOR ANY VARIABLES THAT THEY REQUIRE RULE 5 @KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Ansible-playbooks-MUST-begin-with-checks-for-any-variables-that-th ey-require --- - hosts: localhost gather_facts: no tasks: - fail: msg="Playbook requires g_env to be set and non empty" when: g_env is not defined or g_env == '' --- # tasks/main.yml - fail: msg="Role requires arl_env to be set and non empty" when: arl_env is not defined or arl_env == ''
  • 15. ANSIBLE PLAYBOOKS MUST BEGIN WITH CHECKS FOR ANY VARIABLES THAT THEY REQUIRE RULE 5 @KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Ansible-playbooks-MUST-begin-with-checks-for-any-variables-that-th ey-require If an Ansible playbook or role requires certain variables to be set, itโ€™s best to check for these up front before any other actions have been performed. In this way, the user knows exactly what needs to be passed into the playbook.
  • 16. ANSIBLE TASKS SHOULD NOT BE USED IN ANSIBLE PLAYBOOKS. INSTEAD, USE PRE_TASKS AND POST_TASKS RULE 6 @KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Ansible-tasks-SHOULD-NOT-be-used-in-ansible-playbooks-Instead- use-pre_tasks-and-post_tasks # โœ˜ BAD - hosts: localhost tasks: - name: Executes AFTER the example_role, so itโ€™s confusing debug: msg="in tasks list" roles: - role: example_role # โœ” GOOD - hosts: localhost pre_tasks: - name: Executes BEFORE the example_role, so it makes sense debug: msg="in pre_tasks list" roles: - role: example_role
  • 17. ANSIBLE TASKS SHOULD NOT BE USED IN ANSIBLE PLAYBOOKS. INSTEAD, USE PRE_TASKS AND POST_TASKS RULE 6 @KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Ansible-tasks-SHOULD-NOT-be-used-in-ansible-playbooks-Instead- use-pre_tasks-and-post_tasks An Ansible play is defined as a Yaml dictionary and because of that Ansible doesnโ€™t know if the playโ€™s tasks list or roles list was specified first. Therefore, Ansible always runs tasks after roles. This can be quite confusing if the tasks list is defined in the playbook before the roles list because people assume in order execution in Ansible. Therefore, we SHOULD use pre_tasks and post_tasks to make it more clear when the tasks will be run.
  • 18. ALL TASKS IN A ROLE SHOULD BE TAGGED WITH THE ROLE NAME RULE 7 @KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#All-tasks-in-a-role-SHOULD-be-tagged-with-the-role-name # roles/example_role/tasks/main.yml - debug: msg="in example_role" tags: - example_role
  • 19. ALL TASKS IN A ROLE SHOULD BE TAGGED WITH THE ROLE NAME RULE 7 @KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#All-tasks-in-a-role-SHOULD-be-tagged-with-the-role-name Ansible tasks can be tagged, and then these tags can be used to either run or skip the tagged tasks using the --tags and --skip-tags ansible-playbook options respectively. This is very useful when developing and debugging new tasks. It can also significantly speed up playbook runs if the user specifies only the roles that changed.
  • 20. THE ANSIBLE ROLES DIRECTORY MUST MAINTAIN A FLAT STRUCTURE RULE 8 @KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#The-Ansible-roles-directory-MUST-maintain-a-flat-structure production # inventory file for production servers staging # inventory file for staging environment group_vars/ host_vars/ site.yml # master playbook webservers.yml # playbook for webserver tier dbservers.yml # playbook for dbserver tier roles/ common/ # this hierarchy represents a "role" tasks/, handlers/, templates/, files/, vars/, defaults/, meta/
  • 21. THE ANSIBLE ROLES DIRECTORY MUST MAINTAIN A FLAT STRUCTURE RULE 8 @KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#The-Ansible-roles-directory-MUST-maintain-a-flat-structure The purpose of this rule is to: โ— Comply with the upstream best practices โ— Make it familiar for new contributors โ— Make it compatible with Ansible Galaxy
  • 22. ANSIBLE ROLES SHOULD BE NAMED TECH_COMPONENT[_SUBCOMPONENT] RULE 9 @KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Ansible-Roles-SHOULD-be-named-like-technology_component_subc omponent roles/ # this hierarchy represents a "role" common/ # โœ˜ BAD database/ # โœ” GOOD mysql_slave/
  • 23. ANSIBLE ROLES SHOULD BE NAMED TECH_COMPONENT[_SUBCOMPONENT] RULE 9 @KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#Ansible-Roles-SHOULD-be-named-like-technology_component_subc omponent For consistency, role names SHOULD follow the above naming pattern. It is important to note that this is a recommendation for role naming, and follows the pattern used by upstream. Many times the technology portion of the pattern will line up with a package name. It is advised that whenever possible, the package name should be used.
  • 24. THE DEFAULT FILTER SHOULD REPLACE EMPTY STRINGS, LISTS, ETC RULE 10 @KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#The-default-filter-SHOULD-replace-empty-strings-lists-etc - hosts: localhost gather_facts: no vars: somevar: '' tasks: - debug: var=somevar # โœ˜ BAD - name: "Will output 'somevar: []'" debug: "msg='somevar: [{{ somevar | default('empty str') }}]'" # โœ” GOOD - name: "Will output 'somevar: [the string was empty]'" debug: "msg='somevar: [{{ somevar | default('empty str', true)}}]'"
  • 25. THE DEFAULT FILTER SHOULD REPLACE EMPTY STRINGS, LISTS, ETC RULE 10 @KeithResarhttps://github.com/openshift/openshift-ansible/blob/master/docs/best_practices_guide.adoc#The-default-filter-SHOULD-replace-empty-strings-lists-etc When using the jinja2 default filter, unless the variable is a boolean, specify true as the second parameter. This will cause the default filter to replace empty strings, lists, etc with the provided default rather than only undefined variables. This is because it is preferable to either have a sane default set than to have an empty string, list, etc. For example, it is preferable to have a config value set to a sane default than to have it simply set as an empty string.
  • 26. AUTOMATION FOR EVERYONE ANSIBLE IS DESIGNED AROUND THE WAY PEOPLE WORK AND THE WAY PEOPLE WORK TOGETHER.
  • 27. RESOURCES OPENSHIFT ORIGIN https://www.openshift.org OPENSHIFT ANSIBLE BEST PRACTICES https://github.com/../docs/best_practices_guide ANSIBLE MINNEAPOLIS MEETUP https://www.meetup.com/Ansible-Minneapolis/