Mastering Ansible

Tags: Ansible

A complete guide to configuration management and orchestration with Ansible.

Last updated 2022-01-10 | 4.4

- Execute ad-hoc commands against servers using Ansible
- Write Ansible configuration playbooks to deploy a 3-tier web application
- Configure Ansible roles with tasks
- handlers
- files
- templates
- and default variables

What you'll learn

Execute ad-hoc commands against servers using Ansible
Write Ansible configuration playbooks to deploy a 3-tier web application
Configure Ansible roles with tasks
handlers
files
templates
and default variables
Write operational playbooks to check cluster status and perform a cluster restart.
Optimize Ansible playbooks to reduce playbook execution time.
Test and troubleshoot Ansible playbook execution.

* Requirements

* You should have a Linux or Mac OS X computer
* or access to at lease one Linux virtual machine for installing Ansible.
* You'll need a code editor or IDE of your choice.
* You'll need a terminal and ssh client for running Ansible against target hosts.
* You should have access to 5 Linux servers (bare-metal or virtual machine) if you want to setup the course environment and follow along step-by-step.

Description

Mastering Ansible is a step-by-step journey of learning Ansible for configuration management and orchestration.

The course is designed as a journey through configuring a realistic application stack from the ground up. Instead of going page-by-page through the Ansible documentation, topics are ordered to align with the growing complexity of our application as we build it up and refactor it.

In addition to the core concepts of configuration with Ansible, we spend time on building tools to help us maintain and troubleshoot our application. The goal is to have a workflow where all of the configuration and troubleshooting is done through ansible playbooks that can be committed to a repository and improved over time.

The course is divided into 6 sections, starting with initial installation and foundational concepts. Starting in section 3, we build up a sample application environment layer-by-layer, learning a new concept in each lecture. After the application is up and running, we refactor our setup in section 4 with an emphasis on modularity and encapsulation. In section 5, we optimize our code and learn about techniques to reduce the playbook execution time. The course finishes with a final section on troubleshooting and testing.

For each lecture, we introduce a new Ansible concept and apply it to our playbooks. For most lectures, we execute the new concept in the demo environment so you can see what the output should look like. In the notes of each lecture, you'll find a link to the relevant documentation pages, along with a snapshot of the codebase at the end of that lecture.

This course was built with Ansible version 1.9.3, applied to a 3-tiered web application environment leveraging NGINX, Apache2, Python, and MySQL on Ubuntu Linux servers. We do not cover administering Windows servers in this course.

This course is designed as an introduction to Ansible, but also as a guide for engineers interested in configuration management, orchestration, infrastructure-as-code, and devops principles.

Who this course is for:

  • The course is designed for students who have little or no experience with Ansible, but are familiar with Linux systems administration concepts.
  • No programming or previous automation experience is required.
  • If you've never logged into a Linux shell and run commands before, you will learn the Ansible syntax but you may not understand the 'why' behind the tasks that we configure. We try to explain all systems concept that we cover, but we aren't starting from the beginning with Linux in this course.

Course content

8 sections • 59 lectures

Introduction Preview 02:49

Welcome to Mastering Ansible! This lecture covers the course goals and format. The course is divided into 6 sections, starting with Environment Setup and Foundational Concepts. For the bulk of the course, we configure a realistic 3-tier web application, refactor it, and then optimize it. We finish the course with a section on testing and troubleshooting. Prior linux systems administration experience is expected.

Configuration Management & Orchestration Preview 05:53

Understand broad automation concepts: configuration management and orchestration. Configuration management is concerned with getting a single server into a desired state. Orchestration is focused on executing tasks in a specific order across one or many hosts. Throughout this course, we'll use Ansible to do both.

Environment Setup Preview 05:09

In this course, we will implement a 3-tier web application topology using one (1) NGINX load-balancer, two (2) application nodes running Apache2 and Python, one (1) MySQL database server, and one (1) control machine for running Ansible. If you want to follow along, you can setup an environment that fits these requirements:

  1. Access to a control machine that can run Ansible
  2. Servers running Ubuntu Trusty (14.04) - 5 in total
  3. SSH keypair trusts from the control machine to all other nodes (so no password is required to login via ssh)
  4. Access via a user that has superuser (sudo) privileges on the end hosts


In the Appendix (Lecture 55), I provide details on the docker setup that I use throughout the course.

In the materials below, is a Vagrantfile that can be used to create a similar vagrant setup. Rename "Vagrantfile.txt" to "Vagrantfile" and read the "README" comments inside of the file to get started.

Installation Preview 03:05

The first step in using Ansible is installing Ansible! In this lecture, we will:

  • Install the Ansible tools using a package manager (apt-get for the Ubuntu-based demo environment)
  • Test the installed binaries by checking the version on the command line

Inventory Pt 1 Preview 03:44

Ansible needs to know all of the hosts that it can connect to and manage. In this lecture, we will cover:

  • The concept of the inventory file and how Ansible uses it
  • The default Ansible inventory file format and location (/etc/ansible/hosts).

Inventory Pt 2 Preview 04:53

We need to populate an inventory file so that Ansible can reach our environment hosts. Rather than populate the default location on the control machine, we can create a simple file in our local repository and tell Ansible to use it, by default. In this lecture, we will:

  • Configure a local inventory file with hostnames and groups (dev)
  • Configure a local config file to use the new inventory file by default (ansible.cfg)

Host Selection Preview 03:45

Once the inventory is populated, you can tell Ansible to execute against all or some subset of the included hosts. In this lecture, we'll:

  • Use host patterns to target a subset of the full inventory: all, *, <hostname>, <groupname>, :, !

Tasks Preview 05:06

It's time to execute Ansible against our environment hosts. In this lecture, we will:

  • Learn about the basic building block of Ansible: the task.
  • Understand the structure of tasks: modules and arguments.
  • Execute ad-hoc tasks using the ansible command with the ping and command modules.

Plays Preview 04:35

Running ad-hoc Ansible commands are great for troubleshooting and discovery, but the real power of Ansible is in coding those tasks into playbooks that you can save and run, as needed. In this lecture, we will:

  • Group tasks for a common set of hosts together into plays.
  • Create our first playbook (hostname.yml) to encode the command task into an Ansible YAML file.

Playbook Execution Preview 05:33

With our playbook written, it's time to execute it and review the output. In this lecture, we will:

  • Execute the hostname.yml playbook against hosts using the ansible-playbook command.
  • Review the output of the ansible-playbook command.

Foundation Concepts

Playbooks Introduction Preview 04:42

Now that you understand the framework for encoding tasks and executing playbooks, we will start building up our application environment. For every task, we'll find a relevant module and add the necessary parameters to our playbooks. You can browse the module documentation if you're looking for the right module or parameters to fit your needs.

Packages: apt Preview 04:33

We start our configuration with installing the necessary packages for each tier. In this lecture, we will:

  • Install packages with the apt module
  • Set package installed state using one of: present, latest, absent.
  • Ensure the apt cache is updated before package installation with update_cache=yes

Packages: become Preview 04:07

While executing Ansible tasks, you may run into "permission denied" errors. This generally means the task you're executing requires some sort of superuser or sudo privilege. In this lecture, we will:

  • Enable privilege escalation in Ansible with become/sudo at the play level. This will resolve "permission denied" errors on the end hosts where superuser privileges are required.
  • Execute the configuration playbooks to apply the package installation changes on the load-balancer and database hosts.

Packages: with_items Preview 05:08

For the webserver hosts, we need to install multiple packages via apt. Rather than create four individual apt tasks, we'll learn about a new Ansible feature: loops using with_items. In this lecture, we will:

  • Loop through a task multiple times by providing a list of inputs using with_items
  • Install multiple packages with a single task on the webserver hosts using with_items

Services: service Preview 06:49

With the packages installed, we want to get a handle on the services we'll need to get up and keep running. In this lecture, we will:

  • Manage running service state with the service module
  • Enable services to start on system startup with enabled=yes

Support Playbook 1 - Stack Restart Preview 03:08

  • Write a support playbook to do a full stack restart using the service module

Services: apache2_module, handlers, notify Preview 05:39

  • Enable apache2 modules using the apache2_module ansible module.
  • Configure handlers to restart services only when needed.
  • Trigger handlers after configuration changes using notify.

Files: copy Preview 05:54

  • Copy files and directories from a local machine to a target host
  • Set file attributes on the target host such as owner, group, and file mode

Application Modules: pip Preview 03:11

  • Install python dependencies using the pip module and a requirements.txt file
  • Setup a python virtual environment to host the python web application

Files: file Preview 05:27

  • Create and remove symbolic links using the file module.

Files: template Preview 06:31

  • Inject variables into files before copying to the target host with the template module.
  • Implement for loops in a jinja2 template.

Files: lineinfile Preview 06:49

  • Edit a single line in a file on the target host using regular expressions and the lineinfile module.

Application Modules: mysql_db, mysql_user Preview 04:57

  • Create a mysql database using the mysql_db module.
  • Create a mysql user and set the password and allowed host using the mysql_user module.

Support Playbook 2 - Stack Status: wait_for Preview 09:25

  • Write a support playbook to check the status of the application stack.
  • Check that a host is listening on a specific TCP port and interface using the wait_for module.

Support Playbook 2 - Stack Status: uri, register, fail, when Preview 11:10

  • Extend the stack status playbook to perform http requests against the application using the uri module.
  • Save the contents of the HTTP request using the register module
  • Check the contents of a variable and conditionally cause a failure using the when and fail modules.

Playbooks Summary Preview 03:13

  • Review the progress made on configuring the demo application environment.

Playbook Concepts

Roles Overview Preview 04:41

  • Understand the purpose and structure of roles in ansible.
  • Use the ansible-galaxy command to create a role skeleton.

Converting to Roles: tasks, handlers Preview 05:02

  • Convert an ansible playbook to a role by copying tasks and handlers.
  • Address ordering issues by moving service tasks after configuration tasks.

Converting to Roles: files, templates Preview 05:50

  • Convert an ansible playbook to a role by copying files and templates.
  • Address ordering issues by moving service tasks after configuration tasks.

Site.yml: include Preview 02:59

  • Group multiple playbooks into a single playbook using the include module.

Variables: facts Preview 06:09

  • Use system facts as variables to configure host-specific values.
  • Query a target host for system facts using the setup module.

Variables: defaults Preview 05:20

  • Provide default values for variables defined in a role.

Variables: vars Preview 08:20

  • Understand variable precedence in ansible.
  • Define variable values using vars.
  • Define variable values when including roles to override role defaults.

Variables: with_dict Preview 07:12

  • Use a dictionary/hash and with_dict to execute a task multiple times.

Selective Removal: shell, register, with_items, when Preview 05:45

  • Execute a shell command on a target host with the shell module.
  • Capture the output of a shell command with the register module.
  • Loop through the items in a registered array variable using with_items.
  • Conditionally perform an action on the target host using when.

Variables - continued Preview 06:25

  • Convert the remaining playbooks to roles.
  • Convert a file to a template to parameterize values and leverage passed variables.

Variables: vars_files, group_vars Preview 06:34

  • Define variables in a separate file using group_vars

Variables: vault Preview 10:49

  • Encrypt variable values using ansible-vault.
  • Configure a vault password file to store the vault passphrase.
  • Add a configuration default to use a vault password file automatically.

External Roles & Galaxy Preview 04:41

  • Learn about the ansible public roles repository: galaxy.ansible.com.
  • Understand tradeoffs and considerations of using public roles.

Variables

Test your knowledge on Ansible variables

Advanced Execution Introduction Preview 02:58

Now that our application is configured, and we've completed the bulk of our refactoring into roles, we will look at ways to optimize our playbooks and speed up our execution time.

Removing Unnecessary Steps: gather_facts Preview 03:29

  • Disable fact gathering to speed up execution using gather_facts.

Extracting Repetitive Tasks: cache_valid_time Preview 04:10

  • Increase apt cache timer with the apt module and cache_valid_time.
  • Extract a repeated sequential task and perform it in parallel across all hosts to reduce execution time.

Limiting Execution by Hosts: limit Preview 02:40

  • Limit playbook execution to a subset of hosts without modifying plays

Limiting Execution by Tasks: tags Preview 06:37

  • Add user-defined tags to tasks.
  • Limit playbook execution by including (--tags) or excluding (--skip-tags) defined tags.

Idempotence: changed_when, failed_when Preview 06:23

  • Override ansible's built-in logic to define when a task is marked as changed.
  • Use a python function (.keys()) in an ansible when clause to determine the task will be marked as changed.

Accelerated Mode and Pipelining Preview 04:43

  • Learn about ansible transport optimizations to reduce execution time and overhead.
  • Learn about when to use accelerated mode and ssh pipelining.

Troubleshooting Ordering Problems Preview 05:47

  • Learn how to resolve ordering problems that cause playbook execution dead-ends
  • Skip problematic tasks using ignore_errors.

Jumping to Specific Tasks: list-tasks, step, start-at-task Preview 04:00

  • Step through tasks one-by-one using --step.
  • Display a list of tasks included in a playbook using --list-tasks.
  • Jump to a task and begin execution at an arbitrary point using --start-at-task.

Retrying Failed Hosts Preview 02:43

  • Use the ansible provided retry host list to re-run a playbook on failed or unreachable hosts.

Syntax-Check & Dry-Run: syntax-check, check Preview 05:20

  • Validate the syntax of an ansible playbook using --syntax-check.
  • Perform a dry-run (no-op) of an execution against a live host using --check

Debugging: debug Preview 04:18

  • Print a msg or the contents of a variable using the debug module.

Testing Concepts

Authentication with SSH Keys Preview 06:47

  • Understand SSH public/private key architecture.
  • Learn how to create an RSA keypair.
  • Learn how to setup an SSH trust to login to a host without using a password.

A Brief Overview of YAML Preview 06:11

  • Understand the basics of the YAML format: files, keys/values, hashes, and lists.

A Brief Overview of Jinja2 Preview 05:58

  • Understand the basics of the Jinja2 templating language: variables, templates, and loops

Dev Environment with Docker Preview 01:33

  • Setup a development environment to test ansible using docker-machine and docker-compose.