Ansible for DevOps Jeff Geerling 2024 scribd download
Ansible for DevOps Jeff Geerling 2024 scribd download
com
https://ebookmeta.com/product/ansible-for-devops-jeff-
geerling/
OR CLICK HERE
DOWLOAD NOW
https://ebookmeta.com/product/ansible-for-kubernetes-by-example-
automate-your-kubernetes-cluster-with-ansible-1st-edition-luca-berton/
ebookmeta.com
https://ebookmeta.com/product/ansible-for-kubernetes-by-example-
automate-your-kubernetes-cluster-with-ansible-1st-edition-luca-
berton-2/
ebookmeta.com
https://ebookmeta.com/product/ansible-for-vmware-by-examples-1st-
edition-luca-berton/
ebookmeta.com
https://ebookmeta.com/product/apple-cider-vinegar-1st-edition-deirdre-
layne-daniel-p-kray/
ebookmeta.com
https://ebookmeta.com/product/international-debt-statistics-2017-1st-
edition-world-bank/
ebookmeta.com
https://ebookmeta.com/product/principles-of-economics-8e-ise-robert-h-
frank/
ebookmeta.com
https://ebookmeta.com/product/deals-with-demons-01-0-speak-of-the-
demon-1st-edition-stacia-stark/
ebookmeta.com
https://ebookmeta.com/product/civil-engineering-procedure-8th-edition-
institution-of-civil-engineers/
ebookmeta.com
Tomorrow s Table Organic Farming Genetics and the Future
of Food 2nd Edition Pamela C. Ronald
https://ebookmeta.com/product/tomorrow-s-table-organic-farming-
genetics-and-the-future-of-food-2nd-edition-pamela-c-ronald/
ebookmeta.com
Ansible for DevOps
Server and configuration management for
humans
Jeff Geerling
This book is for sale at http://leanpub.com/ansible-for-devops
ISBN 978-0-9863934-3-3
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean
Publishing process. Lean Publishing is the act of publishing an in-progress ebook
using lightweight tools and many iterations to get reader feedback, pivot until you
have the right book and build traction once you do.
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii
Second Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iv
Who is this book for? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iv
Typographic conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iv
Please help improve this book! . . . . . . . . . . . . . . . . . . . . . . . . . . . vi
Current Published Book Version Information . . . . . . . . . . . . . . . vi
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vi
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii
In the beginning, there were sysadmins . . . . . . . . . . . . . . . . . . . . . vii
Modern infrastructure management . . . . . . . . . . . . . . . . . . . . . . . vii
Ansible and Red Hat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . viii
Ansible Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x
Other resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x
Accessing Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Host and Group variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Automatically-loaded group_vars and host_vars . . . . . . . . . 97
Magic variables with host and group variables and information . 98
Facts (Variables derived from system information) . . . . . . . . . . . . 99
Local Facts (Facts.d) . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Ansible Vault - Keeping secrets secret . . . . . . . . . . . . . . . . . . . 102
Variable Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
If/then/when - Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Jinja Expressions, Python built-ins, and Logic . . . . . . . . . . . . . . . 108
register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
when . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
changed_when and failed_when . . . . . . . . . . . . . . . . . . . . . . . 112
ignore_errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Delegation, Local Actions, and Pauses . . . . . . . . . . . . . . . . . . . . . . 114
Pausing playbook execution with wait_for . . . . . . . . . . . . . . . . 115
Running an entire playbook locally . . . . . . . . . . . . . . . . . . . . . 116
Prompts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Chapter 12 - Automating Your Automation with Ansible Tower and CI/CD 326
Installing Ansible AWX . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
CONTENTS
Afterword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
adding a new “DevOps” team in between existing development and operations teams
achieves that objective! (Oops, now I’m trying for a definition, aren’t I?)
Well, definitions aside, one of the first steps towards a DevOps environment is
choosing tools that can be consumed by both developers and operations engineers.
Ansible is one of those tools: you don’t have to be a software developer to use it, and
the playbooks that you write can easily be self-documenting. There have been a lot
of attempts at “write once, run anywhere” models of application development and
deployment, but I think Ansible comes the closest to providing a common language
that’s useful across teams and across clouds and different datacenters.
The author of this book, Jeff, has been a long-time supporter, contributor, and
advocate of Ansible, and he’s maintained a massive collection of impressive Ansible
roles in Galaxy, the public role-sharing service maintained by Ansible, Inc. Jeff has
used Ansible extensively in his professional career, and is eminently qualified to
write the end-to-end book on Ansible in a DevOps environment.
As you read this book, I hope you enjoy your journey into IT automation as much
as we have. Be well, do good work, and automate everything.
Tim Gerla Ansible, Inc. Co-Founder & CTO
Preface
Growing up, I had access to a world that not many kids ever get to enter. At the
local radio stations where my dad was chief engineer, I was fortunate to get to see
networks and IT infrastructure up close: Novell servers and old Mac and Windows
workstations in the ’90s; Microsoft and Linux-based servers; and everything in
between. Best of all, he brought home decommissioned servers and copies of Linux
burned to CD.
I began working with Linux and small-scale infrastructures before I started high
school, and my passion for infrastructure grew as I built a Cat5 wired network
and a small rack of networking equipment for a local grade school. When I started
developing full-time, what was once a hobby became a necessary part of my job, so
I invested more time in managing infrastructure efficiently. Over the past ten years,
I’ve gone from manually booting and configuring physical and virtual servers; to
using relatively complex shell scripts to provision and configure servers; to using
configuration management tools to manage thousands of cloud-based servers.
When I began converting my infrastructure to code, some of the best tools for testing,
provisioning, and managing my servers were still in their infancy, but they have since
matured into fully-featured, robust tools that I use every day. Vagrant is an excellent
tool for managing local virtual machines to mimic real-world infrastructure locally
(or in the cloud), and Ansible — the subject of this book — is an excellent tool for
provisioning servers, managing their configuration, and deploying applications, even
on my local workstation!
These tools are still improving rapidly, and I’m excited for what the future holds.
The time I invest in learning new infrastructure tools well will be helpful for years
to come.
In these pages, I’ll share with you all I’ve learned about Ansible: my favorite tool for
server provisioning, configuration management, and application deployment. I hope
you enjoy reading this book as much as I did writing it!
— Jeff Geerling, 2015
Preface iv
Second Edition
I’ve published 23 major revisions to the book since the original 1.0 release in 2015.
After major rewrites (and three new chapters) in 2019 and 2020 to reflect Ansible’s
changing architecture, I decided to publish the new content as a ‘2nd edition’.
I will continue to publish revisions in the future, to keep this book relevant for as
long as possible! Please visit the book’s website, at www.ansiblefordevops.com, for
the latest updates, or to subscribe to be notified of Ansible and book news!
— Jeff Geerling, 2020
Typographic conventions
Ansible uses a simple syntax (YAML) and simple command-line tools (using common
POSIX conventions) for all its powerful abilities. Code samples and commands will
be highlighted throughout the book either inline (for example: ansible [command]),
or in a code block (with or without line numbers) like:
Preface v
1 ---
2 # This is the beginning of a YAML file.
Some lines of YAML and other code examples require more than 70 characters per
line, resulting in the code wrapping to a new line. Wrapping code is indicated by a \
at the end of the line of code. For example:
When using the code, don’t copy the \ character, and make sure you don’t use a
newline between the first line with the trailing \ and the next line.
Links to pertinent resources and websites are added inline, like the following link to
Ansible¹, and can be viewed directly by clicking on them in eBook formats, or by
following the URL in the footnotes.
Sometimes, asides are added to highlight further information about a specific topic:
Warning asides will warn about common pitfalls and how to avoid them.
Tip asides will give tips for deepening your understanding or optimizing
your use of Ansible.
When displaying commands run in a terminal session, if the commands are run under
your normal/non-root user account, the commands will be prefixed by the dollar sign
($). If the commands are run as the root user, they will be prefixed with the pound
sign (#).
¹https://www.ansible.com/
Preface vi
DevOps is a loaded word; some people argue using the word to identify
both the movement of development and operations working more closely
to automate infrastructure-related processes, and the personnel who skew
slightly more towards the system administration side of the equation,
dilutes the word’s meaning. I think the word has come to be a rallying cry
for the employees who are dragging their startups, small businesses, and
enterprises into a new era of infrastructure growth and stability. I’m not
too concerned that the term has become more of a catch-all for modern
infrastructure management. My advice: spend less time arguing over the
definition of the word, and more time making it mean something to you.
ad-hoc task execution (Func, plain SSH), and wanted to see if there was a better way.
Ansible wraps up all three of these features into one tool, and does it in a way that’s
actually simpler and more consistent than any of the other task-specific tools!
Ansible aims to be:
1. Clear - Ansible uses a simple syntax (YAML) and is easy for anyone (developers,
sysadmins, managers) to understand. APIs are simple and sensible.
2. Fast - Fast to learn, fast to set up—especially considering you don’t need to
install extra agents or daemons on all your servers!
3. Complete - Ansible does three things in one, and does them very well. Ansible’s
‘batteries included’ approach means you have everything you need in one
complete package.
4. Efficient - No extra software on your servers means more resources for your
applications. Also, since Ansible modules work via JSON, Ansible is extensible
with modules written in a programming language you already know.
5. Secure - Ansible uses SSH, and requires no extra open ports or potentially-
vulnerable daemons on your servers.
Ansible also has a lighter side that gives the project a little personality. As an example,
Ansible’s major releases are named after Led Zeppelin songs (e.g. 2.0 was named
after 1973’s “Over the Hills and Far Away”, 1.x releases were named after Van Halen
songs). Additionally, Ansible uses cowsay, if installed, to wrap output in an ASCII
cow’s speech bubble (this behavior can be disabled in Ansible’s configuration).
Ansible, Inc.⁹ was founded by Saïd Ziouani (@SaidZiouani¹⁰ on Twitter), Michael
DeHaan, and Tim Gerla, and acquired by Red Hat in 2015. The Ansible team oversees
core Ansible development and provides services (such as Ansible Consulting¹¹) and
extra tooling (such as Ansible Tower¹²) to organizations using Ansible. Hundreds of
individual developers have contributed patches to Ansible, and Ansible is the most
starred infrastructure management tool on GitHub (with over 33,000 stars as of this
writing).
In October 2015, Red Hat acquired Ansible, Inc., and has proven itself to be a good
steward and promoter of Ansible. I see no indication of this changing in the future.
⁹https://www.ansible.com/
¹⁰https://twitter.com/SaidZiouani
¹¹https://www.ansible.com/products/consulting
¹²https://www.ansible.com/tower
Introduction x
Ansible Examples
There are many Ansible examples (playbooks, roles, infrastructure, configuration,
etc.) throughout this book. Most of the examples are in the Ansible for DevOps
GitHub repository¹³, so you can browse the code in its final state while you’re reading
the book. Some of the line numbering may not match the book exactly (especially if
you’re reading an older version of the book!), but I will try my best to keep everything
synchronized over time.
Other resources
We’ll explore all aspects of using Ansible to provision and manage your infrastructure
in this book, but there’s no substitute for the wealth of documentation and commu-
nity interaction that make Ansible great. Check out the links below to find out more
about Ansible and discover the community:
• Ansible Documentation¹⁴ - Covers all Ansible options in depth. There are few
open source projects with documentation as clear and thorough.
• Ansible Glossary¹⁵ - If there’s ever a term in this book you don’t seem to fully
understand, check the glossary.
• The Bullhorn¹⁶ - Ansible’s official newsletter.
• Ansible Mailing List¹⁷ - Discuss Ansible and submit questions with Ansible’s
community via this Google group.
• Ansible on GitHub¹⁸ - The official Ansible code repository, where the magic
happens.
• Ansible Example Playbooks on GitHub¹⁹ - Many examples for common server
configurations.
• Getting Started with Ansible²⁰ - A simple guide to Ansible’s community and
resources.
¹³https://github.com/geerlingguy/ansible-for-devops
¹⁴https://docs.ansible.com/ansible/
¹⁵https://docs.ansible.com/ansible/latest/reference_appendices/glossary.html
¹⁶https://us19.campaign-archive.com/home/?u=56d874e027110e35dea0e03c1&id=d6635f5420
¹⁷https://groups.google.com/forum/#!forum/ansible-project
¹⁸https://github.com/ansible/ansible
¹⁹https://github.com/ansible/ansible-examples
²⁰https://www.ansible.com/resources/get-started
Introduction xi
• Ansible Blog²¹
I’d like to especially highlight Ansible’s documentation (the first resource listed
above); one of Ansible’s greatest strengths is its well-written and extremely relevant
documentation, containing a large number of relevant examples and continuously-
updated guides. Very few projects—open source or not—have documentation as
thorough, yet easy-to-read. This book is meant as a supplement to, not a replacement
for, Ansible’s documentation!
²¹https://www.ansible.com/blog
Chapter 1 - Getting Started with
Ansible
Ansible and Infrastructure Management
Configuration management
Lucky for you, there are tools to help you avoid having these snowflake servers—
servers that are uniquely configured and impossible to recreate from scratch because
they were hand-configured without documentation. Tools like CFEngine²², Puppet²³
and Chef²⁴ became very popular in the mid-to-late 2000s.
But there’s a reason why many developers and sysadmins stick to shell scripting and
command-line configuration: it’s simple and easy-to-use, and they’ve had years of
experience using bash and command-line tools. Why throw all that out the window
and learn a new configuration language and methodology?
Enter Ansible. Ansible was built (and continues to be improved) by developers and
sysadmins who know the command line—and want to make a tool that helps them
manage their servers exactly the same as they have in the past, but in a repeatable
and centrally managed way. Ansible also has other tricks up its sleeve, making it a
true Swiss Army knife for people involved in DevOps (not just the operations side).
One of Ansible’s greatest strengths is its ability to run regular shell commands
verbatim, so you can take existing scripts and commands and work on converting
them into idempotent playbooks as time allows. For someone (like me) who was
comfortable with the command line, but never became proficient in more compli-
cated tools like Puppet or Chef (which both required at least a slight understanding
of Ruby and/or a custom language just to get started), Ansible was a breath of fresh
air.
Ansible works by pushing changes out to all your servers (by default), and requires
no extra software to be installed on your servers (thus no extra memory footprint,
and no extra daemon to manage), unlike most other configuration management tools.
²²http://cfengine.com/
²³http://puppetlabs.com/
²⁴http://www.getchef.com/chef/
Chapter 1 - Getting Started with Ansible 3
Installing Ansible
Ansible’s only real dependency is Python. Once Python is installed, the simplest way
to get Ansible running is to use pip, a simple package manager for Python.
If you’re on a Mac, installing Ansible is a piece of cake:
1. Check if pip is installed (which pip). If not, install it: sudo easy_install pip
2. Install Ansible: pip install ansible
You could also install Ansible via Homebrew²⁶ with brew install ansible. Either
way (pip or brew) is fine, but make sure you update Ansible using the same system
with which it was installed!
If you’re running Windows, it will take a little extra work to set everything up.
Typically, people run Ansible inside the Windows Subsystem for Linux. For detailed
instructions setting up Ansible under the WSL, see Appendix A - Using Ansible on
Windows workstations.
If you’re running Linux, chances are you already have Ansible’s dependencies
installed, but we’ll cover the most common installation methods.
²⁵http://en.wikipedia.org/wiki/Idempotence#Computer_science_meaning
²⁶http://brew.sh/
Chapter 1 - Getting Started with Ansible 4
Using pip allows you to upgrade Ansible with pip install --upgrade ansible.
Fedora/Red Hat Enterprise Linux/CentOS:
The easiest way to install Ansible on a Fedora-like system is to use the official dnf
package. If you’re running Red Hat Enterprise Linux (RHEL) or CentOS/Rocky/Alma
Linux, you need to install EPEL’s RPM before you install Ansible (see the info section
below for instructions):
# If you're on RHEL/CentOS 6:
$ rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/\ Debian/Ubuntu:
epel-release-6-8.noarch.rpm
# If you're on RHEL/CentOS 7:
$ yum install epel-release
# If you're on RHEL 8+/Fedora:
$ dnf install epel-release
The easiest way to install Ansible on a Debian or Ubuntu system is to use the official
apt package.
²⁷https://fedoraproject.org/wiki/EPEL
Random documents with unrelated
content Scribd suggests to you:
TRANSCRIBER’S NOTES
1. Silently corrected typographical errors and variations in
spelling.
2. Archaic, non-standard, and uncertain spellings retained
as printed.
3. Footnotes were re-indexed using numbers.
*** END OF THE PROJECT GUTENBERG EBOOK RACE
DISTINCTIONS IN AMERICAN LAW ***
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.