Working with playbooks
Playbooks record and execute Ansible’s configuration, deployment, and orchestration functions. They can describe a policy you want your remote systems to enforce, or a set of steps in a general IT process.
If Ansible modules are the tools in your workshop, playbooks are your instruction manuals, and your inventory of hosts are your raw material.
At a basic level, playbooks can be used to manage configurations of and deployments to remote machines. At a more advanced level, they can sequence multi-tier rollouts involving rolling updates, and can delegate actions to other hosts, interacting with monitoring servers and load balancers along the way.
Playbooks are designed to be human-readable and are developed in a basic text language. There are multiple ways to organize playbooks and the files they include, and we’ll offer up some suggestions on that and making the most out of Ansible.
You should look at Example Playbooks while reading along with the playbook documentation. These illustrate best practices as well as how to put many of the various concepts together.
- Templating (Jinja2)
- Using filters to manipulate data
- Handling undefined variables
- Defining different values for true/false/null (ternary)
- Managing data types
- Formatting data: YAML and JSON
- Combining and selecting data
- Randomizing data
- Managing list variables
- Selecting from sets or lists (set theory)
- Calculating numbers (math)
- Managing network interactions
- Hashing and encrypting strings and passwords
- Manipulating text
- Manipulating strings
- Managing UUIDs
- Handling dates and times
- Getting Kubernetes resource names
- Tests
- Lookups
- Python3 in templates
- The now function: get the current time
- Loops
- Controlling where tasks run: delegation and local actions
- Conditionals
- Blocks
- Handlers: running operations on change
- Error handling in playbooks
- Setting the remote environment
- Working with language-specific version managers
- Re-using Ansible artifacts
- Roles
- Module defaults
- Interactive input: prompts
- Using Variables
- Creating valid variable names
- Simple variables
- When to quote variables (a YAML gotcha)
- Boolean variables
- List variables
- Dictionary variables
- Registering variables
- Referencing nested variables
- Transforming variables with Jinja2 filters
- Where to set variables
- Variable precedence: Where should I put a variable?
- Using advanced variable syntax
- Discovering variables: facts and magic variables
- Playbook Example: Continuous Delivery and Rolling Upgrades