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

Testing with a playbook

One of the most complex things in the IT field is not creating software and systems but debugging them when they have problems. Ansible is no exception. No matter how good you are at creating Ansible playbooks, sooner or later, you’ll find yourself debugging a playbook that is not behaving as you thought it would.

The simplest way of performing basic tests is to print out the values of variables during execution. Let’s learn how to do this with Ansible:

  1. First of all, we need a playbook called debug.yaml with the following content:
    ---
    - hosts: localhost
      tasks:
      - ansible.builtin.shell: /usr/bin/uptime
        register: result
      - ansible.builtin.debug:
          var: result
  2. Run it with the following command:
    $ ansible-playbook debug.yaml

You will receive an output similar to the following:

PLAY [localhost] *******************************************...