Ansible for DevOps
Ansible for DevOps
Server and configuration management for humans
About the Book
Ansible is a simple, but powerful, server and configuration management tool (with a few other tricks up its sleeve). This book helps those familiar with the command line and basic shell scripting start using Ansible to provision and manage anywhere from one to thousands of servers.
The book begins with fundamentals, like installing Ansible, setting up a basic inventory file, and basic concepts, then guides you through Ansible's many uses, including ad-hoc commands, basic and advanced playbooks, application deployments, multiple-provider server provisioning, and even Docker orchestration! Everything is explained with pertinent real-world examples, often using Vagrant-managed virtual machines.
Examples in the book are tested with the latest version of Ansible.
Bundles that include this book
Table of Contents
- Foreword
-
Preface
- Second Edition
- Who is this book for?
- Typographic conventions
-
Please help improve this book!
- Current Published Book Version Information
- About the Author
-
Introduction
- In the beginning, there were sysadmins
- Modern infrastructure management
- Ansible and Red Hat
- Ansible Examples
- Other resources
-
Chapter 1 - Getting Started with Ansible
-
Ansible and Infrastructure Management
- On snowflakes and shell scripts
- Configuration management
- Installing Ansible
- Creating a basic inventory file
- Running your first Ad-Hoc Ansible command
- Summary
-
Ansible and Infrastructure Management
-
Chapter 2 - Local Infrastructure Development: Ansible and Vagrant
- Prototyping and testing with local virtual machines
- Your first local server: Setting up Vagrant
- Using Ansible with Vagrant
- Your first Ansible playbook
- Cleaning Up
- Summary
-
Chapter 3 - Ad-Hoc Commands
- Conducting an orchestra
- Build infrastructure with Vagrant for testing
- Inventory file for multiple servers
-
Your first ad-hoc commands
- Discover Ansible’s parallel nature
- Learning about your environment
- Make changes using Ansible modules
-
Configure groups of servers, or individual servers
- Configure the Application servers
- Configure the Database servers
- Make changes to just one server
- Manage users and groups
- Manage packages
-
Manage files and directories
- Get information about a file
- Copy a file to the servers
- Retrieve a file from the servers
- Create directories and files
- Delete directories and files
-
Run operations in the background
- Update servers asynchronously with asynchronous jobs
- Check log files
- Manage cron jobs
- Deploy a version-controlled application
-
Ansible’s SSH connection history
- Paramiko
- OpenSSH (default)
- Faster OpenSSH with Pipelining
- Summary
-
Chapter 4 - Ansible Playbooks
- Power plays
-
Running Playbooks with
ansible-playbook
- Limiting playbooks to particular hosts and groups
-
Setting user and sudo options with
ansible-playbook
-
Other options for
ansible-playbook
-
Real-world playbook: Rocky Linux Node.js app server
- Add extra repositories
- Deploy a Node.js app
- Launch a Node.js app
- Node.js app server summary
-
Real-world playbook: Ubuntu LAMP server with Drupal
-
Include a variables file, and discover
pre_tasks
andhandlers
- Basic LAMP server setup
- Configure Apache
-
Configure PHP with
lineinfile
- Configure MySQL
- Install Composer
- Create a Drupal project with Composer
- Install Drupal with Drush
- Drupal LAMP server summary
-
Include a variables file, and discover
-
Real-world playbook: Ubuntu server with Solr
-
Include a variables file, and more
pre_tasks
- Install Java
- Install Apache Solr
- Apache Solr server summary
-
Include a variables file, and more
- Summary
-
Chapter 5 - Ansible Playbooks - Beyond the Basics
- Handlers
-
Environment variables
- Per-task environment variables
-
Variables
- Playbook Variables
- Inventory variables
- Registered Variables
- Accessing Variables
-
Host and Group variables
-
Automatically-loaded
group_vars
andhost_vars
- Magic variables with host and group variables and information
-
Automatically-loaded
-
Facts (Variables derived from system information)
- Local Facts (Facts.d)
- Ansible Vault - Keeping secrets secret
- Variable Precedence
-
If/then/when - Conditionals
- Jinja Expressions, Python built-ins, and Logic
-
register
-
when
-
changed_when
andfailed_when
-
ignore_errors
-
Delegation, Local Actions, and Pauses
-
Pausing playbook execution with
wait_for
- Running an entire playbook locally
-
Pausing playbook execution with
- Prompts
- Tags
- Blocks
- Summary
-
Chapter 6 - Playbook Organization - Roles, Includes, and Imports
-
Imports
- Includes
- Dynamic includes
- Handler imports and includes
- Playbook imports
- Complete includes example
-
Roles
- Role scaffolding
- Building your first role
- More flexibility with role vars and defaults
-
Other role parts: handlers, files, and templates
- Handlers
- Files and Templates
- Organizing more complex and cross-platform roles
-
Ansible Galaxy
-
Getting roles from Galaxy
- Using role requirements files to manage dependencies
- A LAMP server in nine lines of YAML
- A Solr server in seven lines of YAML
- Helpful Galaxy commands
- Contributing to Ansible Galaxy
-
Getting roles from Galaxy
- Summary
-
Imports
-
Chapter 7 - Ansible Plugins and Content Collections
- Creating our first Ansible Plugin — A Jinja Filter
- The history of Ansible Content Collections
-
The Anatomy of a Collection
- Putting our Plugin into a Collection
- Going deeper developing collections
-
Collections on Automation Hub and Ansible Galaxy
- Collection version constraints
- Where are collections installed?
- Summary
-
Chapter 8 - Inventories
-
A real-world web application server inventory
- Non-prod environments, separate inventory files
-
Inventory variables
-
host_vars
-
group_vars
-
-
Ephemeral infrastructure: Dynamic inventory
-
Dynamic inventory with DigitalOcean
- DigitalOcean account prerequisites
- Connecting to your DigitalOcean account
- Creating a droplet with Ansible
-
DigitalOcean dynamic inventory with
digital_ocean.py
- Dynamic inventory with AWS
-
Inventory on-the-fly:
add_host
andgroup_by
- Multiple inventory sources - mixing static and dynamic inventories
-
Creating custom dynamic inventories
- Building a Custom Dynamic Inventory in Python
- Building a Custom Dynamic Inventory in PHP
- Managing a PaaS with a Custom Dynamic Inventory
-
Dynamic inventory with DigitalOcean
- Summary
-
A real-world web application server inventory
-
Chapter 9 - Ansible Cookbooks
-
Highly-Available Infrastructure with Ansible
- Directory Structure
- Individual Server Playbooks
- Main Playbook for Configuring All Servers
- Getting the required roles
- Vagrantfile for Local Infrastructure via VirtualBox
- Provisioner Configuration: DigitalOcean
-
Provisioner Configuration: Amazon Web Services (EC2)
- AWS EC2 Dynamic inventory plugin
- Summary
-
ELK Logging with Ansible
- ELK Playbook
- Forwarding Logs from Other Servers
- Summary
-
GlusterFS Distributed File System Configuration with Ansible
- Configuring Gluster - Basic Overview
- Configuring Gluster with Ansible
- Summary
-
Mac Provisioning with Ansible and Homebrew
- Running Ansible playbooks locally
- Automating Homebrew package and app management
- Configuring macOS through dotfiles
- Summary
-
Highly-Available Infrastructure with Ansible
-
Chapter 10 - Deployments with Ansible
- Deployment strategies
-
Simple single-server deployments
- Provisioning a Ruby on Rails server
- Deploying a Rails app to the server
- Provisioning and Deploying the Rails App
- Deploying application updates
-
Zero-downtime multi-server deployments
-
Ensuring zero downtime with
serial
and integration tests - Deploying to app servers behind a load balancer
-
Ensuring zero downtime with
- Capistrano-style and blue-green deployments
- Additional Deployment Features
- Summary
-
Chapter 11 - Server Security and Ansible
-
A brief history of SSH and remote access
- Telnet
- rlogin, rsh and rcp
- SSH
- The evolution of SSH and the future of remote access
- Use secure and encrypted communication
-
Disable root login and use
sudo
- Remove unused software, open only required ports
-
Use the principle of least privilege
- User account configuration
- File permissions
-
Update the OS and installed software
- Automating updates
- Automating updates for RHEL systems
- Automating updates for Debian-based systems
-
Use a properly-configured firewall
-
Configuring a firewall with
ufw
on Debian or Ubuntu -
Configuring a firewall with
firewalld
on Fedora, RHEL and RHEL-derivatives
-
Configuring a firewall with
- Make sure log files are populated and rotated
- Monitor logins and block suspect IP addresses
- Use SELinux (Security-Enhanced Linux) or AppArmor
- Summary and further reading
-
A brief history of SSH and remote access
-
Chapter 12 - Automating Your Automation with Ansible Tower and CI/CD
-
- Installing Ansible AWX
- Using AWX
- Uninstalling AWX
- Other Tower Features of Note
- Tower Alternatives
-
Jenkins CI
- Build a local Jenkins server with Ansible
- Create an Ansible playbook on the Jenkins server
- Create a Jenkins job to run an Ansible Playbook
- Summary
-
-
Chapter 13 - Testing and CI for Ansible Content
- Unit, Integration, and Functional Testing
-
Debugging and Asserting
-
-
The
debug
module
-
The
-
The
fail
andassert
modules
-
-
Linting YAML with
yamllint
-
Performing a
--syntax-check
-
Linting Ansible content with
ansible-lint
-
Automated testing and development with Molecule
- Testing a role with Molecule
-
Testing a playbook with Molecule
- Adjusting Molecule to use more flexible test containers
- Verifying a playbook with Molecule
- Adding lint configuration to Molecule
- Molecule Summary
- Running your playbook in check mode
-
Automated testing on GitHub using GitHub Actions
- Automated testing in other CI environments
- Real-world examples
- Functional testing using serverspec or testinfra
- Summary
-
Chapter 14 - Automating HTTPS and TLS Certificates
-
Generating Self-Signed Certificates with Ansible
- Idempotent Nginx HTTPS playbook with a self-signed cert
-
Automating Let’s Encrypt with Ansible for free Certs
- Use Galaxy roles to get things done faster
- Create the playbook
- Create a server and configure DNS
- Point the playbook inventory at the server
- Access your server over HTTPS!
-
Configuring Nginx to proxy HTTP traffic and serve it over HTTPS
- Modify the Nginx configuration to proxy traffic
- Summary
-
Generating Self-Signed Certificates with Ansible
-
Chapter 15 - Docker and Ansible
- A brief introduction to Docker containers
- Using Ansible to build and manage containers
-
Building a Flask app with Ansible and Docker
- Data storage container
- Flask container
- MySQL container
- Ship it!
-
Building containers with Ansible from the outside
-
Build a Hubot Slack bot container with
ansible_connection: docker
- Hubot and Slack
- Building a Docker container with Ansible
-
Building the
hubot-slack
role - Building and running the Hubot Slack bot container
- Summary
-
Build a Hubot Slack bot container with
- Summary
- Afterword
-
Appendix A - Using Ansible on Windows workstations
-
Method 1 - Use the Windows Subsystem for Linux
- Installing Ansible inside WSL
-
Method 2 - When WSL is not an option
- Prerequisites
- Set up an Ubuntu Linux Virtual Machine
- Log into the Virtual Machine
- Install Ansible
- Summary
-
Method 1 - Use the Windows Subsystem for Linux
-
Appendix B - Ansible Best Practices and Conventions
-
Playbook Organization
-
Write comments and use
name
liberally - Include related variables and tasks
- Use Roles to bundle logical groupings of configuration
- Use role defaults and vars correctly
-
Write comments and use
-
YAML Conventions and Best Practices
- YAML for Ansible tasks
-
Three ways to format Ansible tasks
-
Shorthand/one-line (
key=value
) -
Structured map/multi-line (
key:value
) -
Folded scalars/multi-line (
>
)
-
Shorthand/one-line (
-
Using
|
to format multiline variables
-
Using
ansible-playbook
- Use Ansible Tower
-
Install Galaxy dependencies local to your playbook
- Discriminate wisely when choosing community dependencies
-
Specify
--forks
for playbooks running on > 5 servers - Use Ansible’s Configuration file
- Summary
-
Playbook Organization
- Notes
Other books by this author
The Leanpub 60-day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
80% Royalties. Earn $16 on a $20 book.
We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earned$12,307,240writing, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them
Top Books
OpenIntro Statistics
David Diez, Christopher Barr, Mine Cetinkaya-Rundel, and OpenIntroA complete foundation for Statistics, also serving as a foundation for Data Science.
Leanpub revenue supports OpenIntro (US-based nonprofit) so we can provide free desk copies to teachers interested in using OpenIntro Statistics in the classroom and expand the project to support free textbooks in other subjects.
More resources: openintro.org.
Personal Finance
Jason AndersonThis textbook provides an in-depth analysis on personal finance that is both practical and straightforward in its approach. It has been written in such a way that the readers can gain knowledge without getting overwhelmed by the technical terms. Suitable for both beginners and advanced learners.
Getting to Know IntelliJ IDEA
Trisha Gee and Helen ScottIf we treat our IDE as a text editor, we are doing ourselves a disservice. Using a combination of tutorials and a questions-and-answers approach, Getting to Know IntelliJ IDEA will help you find ways to use IntelliJ IDEA that enable you to work comfortably and productively as a professional developer.
C++20 - The Complete Guide
Nicolai M. JosuttisAll new language and library features of C++20 (for those who know previous C++ versions).
The book presents all new language and library features of C++20. Learn how this impacts day-to-day programming, to benefit in practice, to combine new features, and to avoid all new traps.
Buy early, pay less, free updates.
Other books:
Mastering STM32 - Second Edition
Carmine NovielloWith more than 1200 microcontrollers, STM32 is probably the most complete ARM Cortex-M platform on the market. This book aims to be the most complete guide around introducing the reader to this exciting MCU portfolio from ST Microelectronics and its official CubeHAL and STM32CubeIDE development environment.
R Programming for Data Science
Roger D. PengThis book brings the fundamentals of R programming to you, using the same material developed as part of the industry-leading Johns Hopkins Data Science Specialization. The skills taught in this book will lay the foundation for you to begin your journey learning data science. Printed copies of this book are available through Lulu.
Machine Learning Q and AI
Sebastian Raschka, PhDHave you recently completed a machine learning or deep learning course and wondered what to learn next? With 30 questions and answers on key concepts in machine learning and AI, this book provides bite-sized bits of knowledge for your journey to becoming a machine learning expert.
Stats One
William FooteThe Rails 7 Way
Obie Fernandez, Lucas Dohmen, and Tom Henrik AadlandThe Rails™ 7 Way is the comprehensive, authoritative reference guide for professionals delivering production-quality code using modern Ruby on Rails. It illuminates the entire Rails 7 API, its most powerful idioms, design approaches, and libraries. Building on the previous editions, this edition has been heavily refactored and updated.
Gradual Modularization for Ruby and Rails
Stephan HagemannGet yourself a new tool to manage your Rails application and your growing engineering organization! Prevent the ball-of-mud (and fix it!). Go for microservices or SOA if it makes sense not just because you don't have any other tool. Do all this through a low-overhead tool: packages. Enable better conversations to make practical changes today.
Top Bundles
- #1
Software Architecture
2 Books
"Software Architecture for Developers" is a practical and pragmatic guide to modern, lightweight software architecture, specifically aimed at developers. You'll learn:The essence of software architecture.Why the software architecture role should include coding, coaching and collaboration.The things that you really need to think about before... - #2
CCIE Service Provider Ultimate Study Bundle
2 Books
Piotr Jablonski, Lukasz Bromirski, and Nick Russo have joined forces to deliver the only CCIE Service Provider training resource you'll ever need. This bundle contains a detailed and challenging collection of workbook labs, plus an extensively detailed technical reference guide. All of us have earned the CCIE Service Provider certification... - #3
1500 QUIZ COMMENTATI (3 libri)
3 Books
Tre libri dei QUIZ MMG Commentati al prezzo di DUE! I QUIZ dei concorsi ufficiali di Medicina Generale relativi agli anni: 2000-2001-2003-2012-2013-2014-2015-2016-2017-2018-2019-2020-2021 +100 inediti Raccolti in unico bundle per aiutarvi nello studio e nella preparazione al concorso. All'interno di ogni libro i quiz sono stati suddivisi per... - #4
Pattern-Oriented Memory Forensics and Malware Detection
2 Books
This training bundle for security engineers and researchers, malware and memory forensics analysts includes two accelerated training courses for Windows memory dump analysis using WinDbg. It is also useful for technical support and escalation engineers who analyze memory dumps from complex software environments and need to check for possible... - #5
Practical FP in Scala + Functional event-driven architecture
2 Books
Practical FP in Scala (A hands-on approach) & Functional event-driven architecture, aka FEDA, (Powered by Scala 3), together as a bundle! The content of PFP in Scala is a requirement to understand FEDA so why not take advantage of this bundle!? - #6
Modern C++ Collection
3 Books
Get All about Modern C++C++ Standard Library, including C++20Concurrency with Modern C++, including C++20C++20Each book has about 200 complete code examples. Updates are included. When I update one of the books, you immediately get the updated bundle. You can expect significant updates to each new C++ standard (C++23, C++26, .. ) and also... - #7
Linux Administration Complet
4 Books
Ce lot comprend les quatre volumes du Guide Linux Administration :Linux Administration, Volume 1, Administration fondamentale : Guide pratique de préparation aux examens de certification LPIC 1, Linux Essentials, RHCSA et LFCS. Administration fondamentale. Introduction à Linux. Le Shell. Traitement du texte. Arborescence de fichiers. Sécurité... - #9
Development and Deployment of Multiplayer Online Games, Part ARCH. Architecture (Vol. I-III)
3 Books
What's the Big Idea? The idea behind this book is to summarize the body of knowledge that already exists on multiplayer games but is not available in one single place.And quite a fewof the issues discussed within this series (planned as three nine volumes ~300 pages each), while known in the industry, have not been published at all (except for... - #10
Growing Agile: The Complete Coach's Guide
7 Books
Growing Agile: Coach's Guide Series This bundle provides a collection of training and workshop plans for a variety of agile topics. The series is aimed at agile coaches, trainers and ScrumMasters who often find themselves needing to help teams understand agile concepts. Each book in the series provides the plans, slides, handouts and activity...