Pre Ansible 2
I'm not the only one who was caught out by this. See here, here and here.
Why this wasn't supported really puzzled me. Some on my team concluded that it must be due to the way Ansible works internally and would perhaps require a lot of re-writing to support the feature. However, Michael DeHaan said here that there was a "fundamental reason" it couldn't be done:
"...the role must be applied in the same definition (but not the same variables) to each host in the host group. Ergo, you can't have different numbers of roles applied to different hosts in the same group based on some kind of inventory variance."
But I don't understand why you can't. The hack here suggests to me that you can.
Should the docs have told me what's not possible?
I also wondered if the ansible documentaion on loops should have mentioned its limitations. Funnily enough, Michael DeHaan wrote an article on Technical Documentation As Marketing which said "your documentation and front material is there to both be honest, communicate how to use something, and also sell what you are doing". The docs certainly didn't lie to me, but they could have been more honest about limitations.
We looked at a few options:
- Hack - Way too ugly!
- Loop over each task within a role separately - This was not possible for us since some tasks depended on output from previous tasks.
- Convert role (or set of tasks) into a single task - No appetite to learn and code more ansible specific stuff at a time when we felt so let down by it!
- Move logic away from ansible into something external that ansible calls (e.g. a bash script).
We opted for the least bad, which to us was option four.
Post Ansible 2
According to slide 15 here, in Ansible 2 you can loop over multiple tasks which is really good to hear. However, it seems you can only loop over tasks, and not roles. I don't understand why you can't loop over roles, but am really encouraged to see the feature included. It's also great to hear that Ansible 2 is backwards compatible with version 1. I look forward to using it.