Book Image

Practical Ansible - Second Edition

By : James Freeman, Fabio Alessandro Locati, Daniel Oh
Book Image

Practical Ansible - Second Edition

By: James Freeman, Fabio Alessandro Locati, Daniel Oh

Overview of this book

Ansible empowers you to automate a myriad of tasks, including software provisioning, configuration management, infrastructure deployment, and application rollouts. It can be used as a deployment tool as well as an orchestration tool. While Ansible provides simple yet powerful features to automate multi-layer environments using agentless communication, it can also solve other critical IT challenges, such as ensuring continuous integration and continuous deployment (CI/CD) with zero downtime. In this book, you'll work with the latest release of Ansible and learn how to solve complex issues quickly with the help of task-oriented scenarios. You'll start by installing and configuring Ansible on Linux and macOS to automate monotonous and repetitive IT tasks and learn concepts such as playbooks, inventories, and roles. As you progress, you'll gain insight into the YAML syntax and learn how to port between Ansible versions. Additionally, you'll understand how Ansible enables you to orchestrate multi-layer environments such as networks, containers, and the cloud. By the end of this Ansible book, you'll be well versed in writing playbooks and other related Ansible code to overcome all your IT challenges, from infrastructure-as-a-code provisioning to application deployments and handling mundane day-to-day maintenance tasks.
Table of Contents (21 chapters)
Free Chapter
1
Part 1:Learning the Fundamentals of Ansible
6
Part 2:Expanding the Capabilities of Ansible
12
Part 3:Using Ansible in an Enterprise

Placing tags in the plays and tasks

We have discussed, at many points in this book, that as your confidence and experience with Ansible grows, it is likely that your playbooks will grow, both in scale and complexity. While this is undoubtedly a good thing, there may be times when you only want to run a subset of a playbook, rather than running it from beginning to end. We discussed how to conditionally run tasks based on the value of a variable or fact, but is there a way we can run them based on a selection made when the playbook is run?

Tags in Ansible plays are the solution to this, and in this section, we will build a simple playbook with two tasks—each bearing a different tag—to show you how tags work. We will work with the two simple host inventories that we worked with previously:

  1. Create the following simple playbook to perform two tasks—one to install the nginx package and the other to deploy a configuration file from a template:
    ---
    - name: Simple...