Using Ansible Locally as a Parallel Scripting Engine (Part 2)

In my previous post, I dealt with how to use Ansible as a parallel scripting engine, using the most basic example. However, with a properly designed inventory, this can be used to perform complex tasks.

Let’s consider another example, using a /etc/hosts file, an inventory file, and a playbook.

First, the /etc/hosts entries:

127.0.0.2 dev dev.localdomain 127.0.0.3 test test.localdomain

Second, the inventory:

---

all:
  hosts:
    dev:
      var1: /var
    test:
      var1: /usr
  vars:      
    ansible_connection: local

Third, the playbook:

---

- hosts: all
  tasks:
    - shell: "ls {{ var1 }}"

Fourth, the end results:

Screenshot of Ansible output

In the above example, the two “hosts” execute different commands at roughly the same time. Both run ls on the directory specified in var1, which is different in both cases. This principle can be applied on a much larger scale if necessary.