3

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)

  • I deleted my answer, because, as you pointed out, it was not applicable. If this code runs on the host system rather than on the virtual machines themselves, I don't know how you can do it without with_items. However, a real example instead of "# action" would have made your question clearer and it might make others think of alternative ways to do what you are trying. – Antonis Christofides Oct 26 '14 at 19:47
  • I tried to do that with include and with_dict. But unfortunatly thats deprecated. Now I'm searching for a solution, too. – Subito Feb 23 '15 at 9:19
4

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:

---
- hosts: vhosts
  name: Tasks run on vhosts
  roles:
    - { role: somerole }

Keep in mind that you can have multiple plays in your playbook, so it's perfectly fine to do something like this:

---
- hosts: all
  name: Run roles for all servers
  roles:
    - { role: role1 }
    - { role: role2 }
    - etc.

- hosts: vhosts
  name: Run roles specific to vhosts
  roles:
    - { role: vhost-role }

- hosts: all
  name: Post-vhost roles for all servers
  roles:
    - { role: role3 }
    - etc.

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:

---
- hosts: "{{ somevar }}"
  name: Run roles against a user specified set of hosts
  roles:
    - { role: foo }

And then you'd invoke this last one in one of these ways:

$ ansible-playbook playbook.yml --extra-vars "somevar=vhosts"

$ ansible-playbook playbook.yml --extra-vars "somevar=host1,host2"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.