How Do You Iterate through a List of Servers When Running an Ansible Playbook?

Problem scenario
You want an Ansible playbook to run on specific managed nodes.  You can create a list of the servers you want the playbook to apply to.  How do you provide a list of specific hostnames and have the Ansible playbook run on each server?

This assumes that you have installed Ansible and have it working with the managed nodes you want to run a playbook on.  For directions on deploying Ansible, see this posting.

You could modify the inventory file on your Ansible server.  But if you want a list for an ad hoc run of an Ansible playbook, do the following:

1.  In the .yaml or .yml file of your playbook, create a stanza like this:

hosts:  coolgroupofservers

2.  Create a file with an arbitrary name in the same directory as your playbook.  Call it something like foobar.txt

3.  Have foobar.txt have the following content:


4.  Run the Ansible playbook (e.g., goodpb.yaml) with the -i flag to call the foobar.txt file:

ansible-playbook goodpb.yaml -i foobar.txt

Leave a comment

Your email address will not be published. Required fields are marked *