Working With Playbooks — Ansible Documentation

- AnsibleFest
- Products
- Community
- Webinars & Training
- Blog

Documentation

# Working With Playbooks

Playbooks are Ansible’s configuration, deployment, and orchestration language. 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.

While there’s a lot of information here, there’s no need to learn everything at once. You can start small and pick up more features over time as you need them.

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.

- Intro to Playbooks
  - About Playbooks
  - Playbook Language Example
  - Basics
  - Action Shorthand
  - Handlers: Running Operations On Change
  - Executing A Playbook
  - Ansible-Pull
  - Linting playbooks
  - Other playbook verification options
- Creating Reusable Playbooks
  - Including and Importing
  - Roles
  - Dynamic vs. Static
  - Differences Between Dynamic and Static
  - Tradeoffs and Pitfalls Between Includes and Imports
- Using Variables
  - Creating valid variable names
  - Defining variables in inventory
  - Defining variables in a playbook
  - Defining variables in included files and roles
  - Using variables with Jinja2
  - Transforming variables with Jinja2 filters
  - Hey wait, a YAML gotcha
  - Variables discovered from systems: Facts
  - Registering variables
  - Accessing complex variable data
  - Accessing information about other hosts with magic variables
  - Defining variables in files
  - Passing variables on the command line
  - Variable precedence: Where should I put a variable?
  - Using advanced variable syntax
- Templating (Jinja2)
  - Filters
  - Tests
  - Lookups
  - Python Version and Templating
  - Get the current time
- Conditionals
  - The When Statement
  - Loops and Conditionals
  - Loading in Custom Facts
  - Applying ‘when’ to roles, imports, and includes
  - Conditional Imports
  - Selecting Files And Templates Based On Variables
  - Register Variables
  - Commonly Used Facts
- Loops
  - Comparing `loop` and `with_*`
  - Standard loops
  - Registering variables with a loop
  - Complex loops
  - Ensuring list input for `loop`: `query` vs. `lookup`
  - Adding controls to loops
  - Migrating from with_X to loop
- Blocks
  - Blocks error handling
- Advanced Playbooks Features
  - Understanding privilege escalation: become
  - Asynchronous Actions and Polling
  - Check Mode (“Dry Run”)
  - Playbook Debugger
  - Delegation, Rolling Updates, and Local Actions
  - Setting the Environment (and Working With Proxies)
  - Working With Language-Specific Version Managers
  - Error Handling In Playbooks
  - Advanced Syntax
  - Working With Plugins
  - Prompts
  - Tags
  - Using Vault in playbooks
  - Start and Step
  - Playbook Keywords
  - Lookups
  - Module defaults
- Controlling playbook execution: strategies and more
  - Selecting a strategy
  - Setting the number of forks
  - Using keywords to control execution
- Best Practices
  - Content Organization
  - Staging vs Production
  - Rolling Updates
  - Always Mention The State
  - Group By Roles
  - Operating System and Distribution Variance
  - Bundling Ansible Modules With Playbooks
  - Whitespace and Comments
  - Always Name Tasks
  - Keep It Simple
  - Version Control
  - Variables and Vaults
- Playbook Example: Continuous Delivery and Rolling Upgrades
  - What is continuous delivery?
  - Site deployment
  - Reusable content: roles
  - Configuration: group variables
  - The rolling upgrade
  - Managing other load balancers
  - Continuous delivery end-to-end