Ansible: Playbooks Deep Dive
May 13th, 2019
Course Development Director in Content
This course is the second in a series on Ansible where we focus in on writing Ansible playbooks.
While the first course in the series focused on installation, configuration, and ad-hoc commands, this course discusses what a play and playbook are in Ansible. Many key features of Ansible Playbooks are highlighted as we work through the course and a few new modules are introduced along the way.
Naturally, there are many demonstrations provided and some discussion on potential uses cases for the various features that are covered. Hands-on labs are provided throughout the course for students to practice their knowledge. After completing this course, students should be capable of writing Ansible playbooks for a number of various scenarios!
Interactive Diagram: https://interactive.linuxacademy.com/diagrams/AnsiblePlaybooks.html
About the Course
This lesson provides an overview of the topics covered in the Ansible: Playbooks Deep Dive course.
About the Training Architect
Get to know your Training Architect for the Ansible: Playbooks Deep Dive course.
Course Features and Tools
This lesson goes over some of the tools made availalbe to students as they work through the course content. Be sure to watch so you know how to make the most of your subscription!
About Ansible Playbooks
We look at a simple Ansible playbook to get an idea of what exactly an Ansible playbook is and what it does. A brief discussion of plays and playbooks is also provided as compared to Ansible ad-hoc commands.
Advanced Inventory Configuration
Note: The inventory referenced in this lesson (as well as future lessons in this course) was created in the "Ansible Configure, Setup, and Ad Hoc Commands Deep Dive" course. See https://linuxacademy.com/cp/courses/lesson/course/3928/lesson/2/module/317 for additional information. This lesson talks about some more advanced options when it comes to an Ansible inventory. Dynamic inventories are covered as well as how they differ from static inventories. A brief introduction to inventory variables is provided with elaboration on variables in a coming lesson.
Using YAML for Ansible Playbooks
YAML is the language used to craft Ansible playbooks. In this lesson, we go over some of the more commonly used YAML components in Ansible as well as some of the common hang-ups you may run into while working with YAML in an Ansible playbook.
Creating an Ansible Play
This lesson provides a broad overview of an Ansible play. A very simple play is constructed and each component is briefly discussed. Playbook formatting is demonstrated and the difference between a play and playbook is also covered.
The `ansible-playbook` Command
The ansible-playbook command is used to execute playbooks in Ansible. This lesson talks about a few common options used with the command, as well as its general behavior.
Understanding Playbook Tasks
The task is the base unit of work in an Ansible playbook. Essential task formatting and properties are demonstrated in this lesson. Best practices regarding general playbook writing are also discussed.
Essential Playbook Syntax
Using Variables in Playbooks
This video lesson demonstrates how to define and use variables in Ansible in a broad sense. Variables allow for enhanced automation via loops and runtime status checks. Variables also allow for creating configuration file templates, among other useful features. More information will be provided on variables as the course progresses.
Working with Templates
Ansible templates provide a simple means of maintaining various configuration files that may be customized on deployment. This lesson demonstrates how to use the template module in Ansible to deploy a Jinja template file. In addition, the template file format is discussed.
Using Ansible Facts
Ansible facts are a great way to collect system properties at runtime! This lesson shows how Ansible facts may be used in playbooks to use runtime system properties in host configuration. There is also a discussion on how Ansible may be configured to cache facts.
Conditional Execution in Playbooks
Decision making is a key part of any automation solution. Ansible allows for decision making with the when keyword. Besides introducing the stat module, this lesson demonstrates how conditionals may be used in Ansible for conditional task execution.
Using Loops in Ansible
System configuration frequently requires repeating various tasks using different parameters. Ansible helps us tackle this use-case with a built-in loop feature. Watch this lesson to see how to leverage loops in Ansible!
Working with Handlers in Ansible
Ansible handlers allow for execution of tasks that are contingent on another tasks making changes. This lesson describes how you can configure and utilize Ansible handlers in your own playbooks!
Advanced Playbook Syntax
Executing Selective Parts of a Playbook
This lesson talks about Ansible Tags. Tags may be used in Ansible to execute selective parts of a given playbook. Tags are a handy way to target specific, related tasks of a single playbook.
Working with Sensitive Data Using Ansible Vault
A common need in configuration management is secure storage for sensitive information, such as passwords. Ansible provides a means for us to protect such data using the ansible-vault command. Ansible vaults are encrypted files which the ansible-playbook command can be configured to unencrypt at playbook runtime. We discuss how to use these features of Ansible within this lesson!
Error Handling in a Playbook - limit, ignore_errors, changed_when, and failed_when
Ansible has a number of features for dealing with errors and even defining what an error is! This lesson talks about keywords that can be used to define change conditions, failure conditions, and weather or not errors should be ignored.
Error Handling in a Playbook - Block Groups and The Debug Module
Ansible block groups may be used within Ansible playbooks to attempt certain tasks and provide conditional steps based on the attempted task. Ansible also provides a core debug module that allows for printing debug information to stdout during play execution. These two features are essential for general error handling in Ansible and are discussed in this lesson.
Asynchronous Tasks within a Playbook
By default, Ansible waits for each task to complete on a target node before continuing on to the next task. There are occasions where that behavior is not ideal. The async and poll keywords allow us to configure Ansible to "fire and forget" a task that may be long running. The keywords also allow us to get around SSH timeout settings when necessary. These features are demonstrated in this lesson.
Delegating Playbook Execution with `delegate_to` and `local_action`
Ansible allows for the execution of tasks exclusively on specific hosts as needed. The delegate_to keyword takes a host which a task will be ran against. The delegation of a task with localhost may be delegated to using the shorthand local_action. Both approaches to this are discussed in this lesson.
Parallelism in Playbooks
After a brief review of Ansible forks, this lesson talks about the serial and max_fail_percentage keywords. The serial keyword allows for batching of hosts for execution instead of targeting all hosts simultaneously. The max_fail_percentage keyword can configure a play to abort if too many target hosts fail play execution. For a refresher on basic parallelism in Ansible, check out this lesson:https://linuxacademy.com/cp/courses/lesson/course/3933/lesson/2/module/317
The run_once keyword allows us to mark a task for only a single execution in a play, no matter how many hosts are targeted. This has select use cases, such as running a script that may only need to be ran one time for a cluster. The keyword is demonstrated in this lesson and a brief discussion is provided as to how run_once interacts with delegate_to and serial.
Overview of Ansible Roles
Ansible roles allow for enhanced organization of more complex Ansible playbooks. They also are easily shareable via Ansible Galaxy. This lesson provides a broad overview of the Ansible role structure and a brief look at the ansible-galaxy command.
Ansible Role Demo
This lesson goes over creating a very simple role using several of the role directories. The syntax for engaging a role is also discussed, as well as the difference between static and dynamic includes in Ansible. See https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse.html for more detail on the impact of static vs. dynamic includes.
Course Wrap Up
Conclusion and Next Steps
Thanks for seeing the course through! In this video, we look at some other topics that may interest students who have completed this course.