Suppose, however, you needed to create ‘x’ number of instances which were
transient in nature and had no existing details available to populate an
inventory file for Ansible to utilise? If ‘x’ is a small number, you could
easily hand-craft the inventory file, but once this number gets into tens
of instances it becomes onerous.

To get around this problem, we can make use of Ansible’s ability to populate an
in-memory inventory, using the add_host module, with information it
generates while creating new instances.

The number of instances to be created is controlled by a ‘count’ variable. As
the instances are created, the results are captured in a registered variable
called ‘newnodes’. This is in turn iterated over using a ‘with_items’ loop to
add the required details to the in-memory inventory, as shown in this snippet:

The newly created group ‘created_nodes’ is used to iterate over the new
instances. Firstly it checks to see that SSH is responding and then it begins
the configuration of the nodes.

For the purposes of this example, the configuration involves simply installing
some packages onto each node. Once this has been completed, the playbook is
paused and will remain that way until told to either continue or abort.