Leanpub Header

Skip to main content

Ansible for DevOps

Server and configuration management for humans

Ansible is a simple, but powerful, server and configuration management tool. Learn to use Ansible effectively, whether you manage one server—or thousands.

The author is letting you choose the price you pay for this book!

Pick Your Price...
PDF
EPUB
WEB
111,944
Readers
470
Pages
91,580Words
About

About

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.

Price

Pick Your Price...

Minimum price

$9.99

$9.99

You pay

$9.99

Author earns

$7.99
$

All prices are in US $. You can pay in US $ or in your local currency when you check out.

EU customers: prices exclude VAT, which is added during checkout.

...Or Buy With Credits!

Number of credits (Minimum 1)

1
The author will earn $12.00 from your purchase!
You can get credits monthly with a Reader Membership

Author

About the Author

Jeff Geerling

Jeff Geerling is a developer who has worked in programming and DevOps-related positions for companies with thousands of servers. He also manages infrastructure for services offered by Midwestern Mac, LLC, and has been using Ansible to manage infrastructure since early 2013, and Kubernetes since 2017.

Leanpub Podcast

Episode 25

An Interview with Jeff Geerling

Contents

Table of Contents

Foreword

Preface

  1. Second Edition
  2. Who is this book for?
  3. Typographic conventions
  4. Please help improve this book!
  5. Current Published Book Version Information
  6. About the Author

Introduction

  1. In the beginning, there were sysadmins
  2. Modern infrastructure management
  3. Ansible and Red Hat
  4. Ansible Examples
  5. Other resources

Chapter 1 - Getting Started with Ansible

  1. Ansible and Infrastructure Management
  2. On snowflakes and shell scripts
  3. Configuration management
  4. Installing Ansible
  5. Fedora/Red Hat Enterprise Linux
  6. Debian/Ubuntu
  7. Creating a basic inventory file
  8. Running your first Ad-Hoc Ansible command
  9. Summary

Chapter 2 - Local Infrastructure Development: Ansible and Vagrant

  1. Prototyping and testing with local virtual machines
  2. Your first local server: Setting up Vagrant
  3. Using Ansible with Vagrant
  4. Your first Ansible playbook
  5. Cleaning Up
  6. Summary

Chapter 3 - Ad-Hoc Commands

  1. Conducting an orchestra
  2. Build infrastructure with Vagrant for testing
  3. Inventory file for multiple servers
  4. Your first ad-hoc commands
  5. Discover Ansible’s parallel nature
  6. Learning about your environment
  7. Make changes using Ansible modules
  8. Configure groups of servers, or individual servers
  9. Configure the Application servers
  10. Configure the Database servers
  11. Make changes to just one server
  12. Manage users and groups
  13. Manage packages
  14. Manage files and directories
  15. Get information about a file
  16. Copy a file to the servers
  17. Retrieve a file from the servers
  18. Create directories and files
  19. Delete directories and files
  20. Run operations in the background
  21. Update servers asynchronously with asynchronous jobs
  22. Check log files
  23. Manage cron jobs
  24. Deploy a version-controlled application
  25. Ansible’s SSH connection history
  26. Paramiko
  27. OpenSSH (default)
  28. Faster OpenSSH with Pipelining
  29. Summary

Chapter 4 - Ansible Playbooks

  1. Power plays
  2. Running Playbooks with ansible-playbook
  3. Limiting playbooks to particular hosts and groups
  4. Setting user and sudo options with ansible-playbook
  5. Other options for ansible-playbook
  6. Real-world playbook: Rocky Linux Node.js app server
  7. Add extra repositories
  8. Deploy a Node.js app
  9. Launch a Node.js app
  10. Node.js app server summary
  11. Real-world playbook: Ubuntu LAMP server with Drupal
  12. Include a variables file, and discover pre_tasks and handlers
  13. Basic LAMP server setup
  14. Configure Apache
  15. Configure PHP with lineinfile
  16. Configure MySQL
  17. Install Composer
  18. Create a Drupal project with Composer
  19. Install Drupal with Drush
  20. Drupal LAMP server summary
  21. Real-world playbook: Ubuntu server with Solr
  22. Include a variables file, and more pre_tasks
  23. Install Java
  24. Install Apache Solr
  25. Apache Solr server summary
  26. Summary

Chapter 5 - Ansible Playbooks - Beyond the Basics

  1. Handlers
  2. Environment variables
  3. Per-task environment variables
  4. Variables
  5. Playbook Variables
  6. Inventory variables
  7. Registered Variables
  8. Accessing Variables
  9. Host and Group variables
  10. Automatically-loaded group_vars and host_vars
  11. Magic variables with host and group variables and information
  12. Facts (Variables derived from system information)
  13. Local Facts (Facts.d)
  14. Ansible Vault - Keeping secrets secret
  15. Variable Precedence
  16. If/then/when - Conditionals
  17. Jinja Expressions, Python built-ins, and Logic
  18. register
  19. when
  20. changed_when and failed_when
  21. ignore_errors
  22. Delegation, Local Actions, and Pauses
  23. Pausing playbook execution with wait_for
  24. Running an entire playbook locally
  25. Prompts
  26. Tags
  27. Blocks
  28. Summary

Chapter 6 - Playbook Organization - Roles, Includes, and Imports

  1. Imports
  2. Includes
  3. Dynamic includes
  4. Handler imports and includes
  5. Playbook imports
  6. Complete includes example
  7. Roles
  8. Role scaffolding
  9. Building your first role
  10. More flexibility with role vars and defaults
  11. Other role parts: handlers, files, and templates
  12. Handlers
  13. Files and Templates
  14. Organizing more complex and cross-platform roles
  15. Ansible Galaxy
  16. Getting roles from Galaxy
  17. Using role requirements files to manage dependencies
  18. A LAMP server in nine lines of YAML
  19. A Solr server in seven lines of YAML
  20. Helpful Galaxy commands
  21. Contributing to Ansible Galaxy
  22. Summary

Chapter 7 - Ansible Plugins and Content Collections

  1. Creating our first Ansible Plugin — A Jinja Filter
  2. The history of Ansible Content Collections
  3. The Anatomy of a Collection
  4. Putting our Plugin into a Collection
  5. Going deeper developing collections
  6. Collections on Automation Hub and Ansible Galaxy
  7. Collection version constraints
  8. Where are collections installed?
  9. Summary

Chapter 8 - Inventories

  1. A real-world web application server inventory
  2. Non-prod environments, separate inventory files
  3. Inventory variables
  4. host_vars
  5. group_vars
  6. Ephemeral infrastructure: Dynamic inventory
  7. Dynamic inventory with DigitalOcean
  8. DigitalOcean account prerequisites
  9. Connecting to your DigitalOcean account
  10. Creating a droplet with Ansible
  11. DigitalOcean dynamic inventory with digital_ocean.py
  12. Dynamic inventory with AWS
  13. Inventory on-the-fly: add_host and group_by
  14. Multiple inventory sources - mixing static and dynamic inventories
  15. Creating custom dynamic inventories
  16. Building a Custom Dynamic Inventory in Python
  17. Building a Custom Dynamic Inventory in PHP
  18. Managing a PaaS with a Custom Dynamic Inventory
  19. Summary

Chapter 9 - Ansible Cookbooks

  1. Highly-Available Infrastructure with Ansible
  2. Directory Structure
  3. Individual Server Playbooks
  4. Main Playbook for Configuring All Servers
  5. Getting the required roles
  6. Vagrantfile for Local Infrastructure via VirtualBox
  7. Provisioner Configuration: DigitalOcean
  8. Provisioner Configuration: Amazon Web Services (EC2)
  9. AWS EC2 Dynamic inventory plugin
  10. Summary
  11. ELK Logging with Ansible
  12. ELK Playbook
  13. Forwarding Logs from Other Servers
  14. Summary
  15. GlusterFS Distributed File System Configuration with Ansible
  16. Configuring Gluster - Basic Overview
  17. Configuring Gluster with Ansible
  18. Summary
  19. Mac Provisioning with Ansible and Homebrew
  20. Running Ansible playbooks locally
  21. Automating Homebrew package and app management
  22. Configuring macOS through dotfiles
  23. Summary

Chapter 10 - Deployments with Ansible

  1. Deployment strategies
  2. Simple single-server deployments
  3. Provisioning a Ruby on Rails server
  4. Deploying a Rails app to the server
  5. Provisioning and Deploying the Rails App
  6. Deploying application updates
  7. Zero-downtime multi-server deployments
  8. Ensuring zero downtime with serial and integration tests
  9. Deploying to app servers behind a load balancer
  10. Capistrano-style and blue-green deployments
  11. Additional Deployment Features
  12. Summary

Chapter 11 - Server Security and Ansible

  1. A brief history of SSH and remote access
  2. Telnet
  3. rlogin, rsh and rcp
  4. SSH
  5. The evolution of SSH and the future of remote access
  6. Use secure and encrypted communication
  7. Disable root login and use sudo
  8. Remove unused software, open only required ports
  9. Use the principle of least privilege
  10. User account configuration
  11. File permissions
  12. Update the OS and installed software
  13. Automating updates
  14. Automating updates for RHEL systems
  15. Automating updates for Debian-based systems
  16. Use a properly-configured firewall
  17. Configuring a firewall with ufw on Debian or Ubuntu
  18. Configuring a firewall with firewalld on Fedora, RHEL and RHEL-derivatives
  19. Make sure log files are populated and rotated
  20. Monitor logins and block suspect IP addresses
  21. Use SELinux (Security-Enhanced Linux) or AppArmor
  22. Summary and further reading

Chapter 12 - Automating Your Automation with Ansible Tower and CI/CD

  1. Installing Ansible AWX
  2. Using AWX
  3. Uninstalling AWX
  4. Other Tower Features of Note
  5. Tower Alternatives
  6. Jenkins CI
  7. Build a local Jenkins server with Ansible
  8. Create an Ansible playbook on the Jenkins server
  9. Create a Jenkins job to run an Ansible Playbook
  10. Summary

Chapter 13 - Testing and CI for Ansible Content

  1. Unit, Integration, and Functional Testing
  2. Debugging and Asserting
  3. The debug module
  4. The fail and assert modules
  5. Linting YAML with yamllint
  6. Performing a --syntax-check
  7. Linting Ansible content with ansible-lint
  8. Automated testing and development with Molecule
  9. Testing a role with Molecule.
  10. Testing a playbook with Molecule
  11. Adjusting Molecule to use more flexible test containers
  12. Verifying a playbook with Molecule
  13. Adding lint configuration to Molecule
  14. Molecule Summary
  15. Running your playbook in check mode
  16. Automated testing on GitHub using GitHub Actions
  17. Automated testing in other CI environments
  18. Real-world examples
  19. Functional testing using serverspec or testinfra
  20. Summary

Chapter 14 - Automating HTTPS and TLS Certificates

  1. Generating Self-Signed Certificates with Ansible
  2. Idempotent Nginx HTTPS playbook with a self-signed cert
  3. Automating Let’s Encrypt with Ansible for free Certs
  4. Use Galaxy roles to get things done faster
  5. Create the playbook
  6. Create a server and configure DNS
  7. Point the playbook inventory at the server
  8. Access your server over HTTPS!
  9. Configuring Nginx to proxy HTTP traffic and serve it over HTTPS
  10. Modify the Nginx configuration to proxy traffic
  11. Summary

Chapter 15 - Docker and Ansible

  1. A brief introduction to Docker containers
  2. Using Ansible to build and manage containers
  3. Building a Flask app with Ansible and Docker
  4. Data storage container
  5. Flask container
  6. MySQL container
  7. Ship it!
  8. Building containers with Ansible from the outside
  9. Build a Hubot Slack bot container with ansible_connection: docker
  10. Hubot and Slack
  11. Building a Docker container with Ansible
  12. Building the hubot-slack role
  13. Building and running the Hubot Slack bot container
  14. Summary
  15. Summary

Afterword

Appendix A - Using Ansible on Windows workstations

  1. Method 1 - Use the Windows Subsystem for Linux
  2. Installing Ansible inside WSL
  3. Method 2 - When WSL is not an option
  4. Prerequisites
  5. Set up an Ubuntu Linux Virtual Machine
  6. Log into the Virtual Machine
  7. Install Ansible
  8. Summary

Appendix B - Ansible Best Practices and Conventions

  1. Playbook Organization
  2. Write comments and use name liberally
  3. Include related variables and tasks
  4. Use Roles to bundle logical groupings of configuration
  5. Use role defaults and vars correctly
  6. YAML Conventions and Best Practices
  7. YAML for Ansible tasks
  8. Three ways to format Ansible tasks
  9. Shorthand/one-line (key=value)
  10. Structured map/multi-line (key:value)
  11. Folded scalars/multi-line (>)
  12. Using | to format multiline variables
  13. Using ansible-playbook
  14. Use Ansible Tower
  15. Install Galaxy dependencies local to your playbook
  16. Discriminate wisely when choosing community dependencies
  17. Specify --forks for playbooks running on > 5 servers
  18. Use Ansible’s Configuration file
  19. Summary

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

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.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, 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

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub