I’m working on a project where I’m using Ansible playbooks to spin up AWS EC2 instances. Because I’m in the early stages, I’m starting and terminating hosts and often want to run a small playbook on these ephemeral servers.

Instead of using dynamic inventory and worrying about production hosts that I don’t want to accidentally run the playbook against, or have to faff about with static inventory files, you can use an instance’s IP address with the ansible-playbook command line:

ansible-playbook test-play.yml --inventory="1.2.3.4," -l 1.2.34

The key being the trailing comma in the --inventory switch which expects a comma-delimited set of hosts. Then you can -l limit the playbook that host.

The playbook itself needs a change, so instead of hosts: all (or hosts: webservers, etc) you have:

hosts: "{{ ansible_limit | default(omit) }}"

This ensures you limit the play to the host you specified with the -l switch. If none is found, then the default is omit which prevents the play from running. Handy for these early stages of playbook development!