TIL: How to specify a host or hosts for an ansible-playbook without an inventory file
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!
All links, in order of mention:
- Ansible playbooks: https://docs.ansible.com/ansible/latest/user_guide/playbooks.html
- AWS EC2 instances: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html
- dynamic inventory: https://docs.ansible.com/ansible/latest/user_guide/intro_dynamic_inventory.html
- static inventory files: https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#inventory-basics-formats-hosts-and-groups
- ansible-playbook: https://docs.ansible.com/ansible/latest/cli/ansible-playbook.html
Recent posts:
- Patch for aarch64 (aka arm64) openssl 1.0.2 'relocation R_AARCH64_PREL64 against symbol OPENSSL_armcap_P error'
- TIL: the `NO_COLOR` informal standard to suppress ANSI colour escape codes
- Copy the contents of a branch into an existing git branch without merging
- Adding search to a static Jekyll site using pagefind
- asdf, python and automatically enabling virtual envs