I've got an Ansible role with several tasks that I run for several virtual hosts. My playbook works, and it looks like this:
---
- name: Create vhost configuration
# action
with_items: vhosts
- name: Ensure vhost is enabled
# action
notify: restart apache2
with_items: vhosts
# (Many more actions)
Is there a way to avoid having to write with_items
for every action? Can I iterate over the vhost items for the whole playbook rather than running each command for all vhosts sequentially? (as I am with the above code)
Best Answer
From what you're asking, it sounds like you just want to specify the vhosts group as the hosts to run a specific role on. So your playbook should start something like this:
Keep in mind that you can have multiple plays in your playbook, so it's perfectly fine to do something like this:
If you have a single playbook that you want to run on different host groups at different times then you can use a variable for the hosts entry:
And then you'd invoke this last one in one of these ways: