Hi,
I have a couple of hosts that have a ‘profiles’ variable that contains a list of, well, profiles that I want to apply. I want to use group_by to turn those into groups. However, it is not working. (I am doing this on the current devel branch, by the way.)
This test case isolates the issue:
hosts
I’m not positive group_by can be used with with_items.
I think we may have proposed making this raise an error, which seems easy to do if this is the case.
If you feel you can boil this down to a minimal reproducer example please file a bug in github and we can take a look. I’d probably just do it with a hard coded list for starters, just to make sure variables have nothing to do with it (as it would provide more information if they did).
Sidenote - it’s very unusual to put variables below your tasks section. I’d suggest not doing that as it makes playbooks less readable.
This seems to be doing what you are telling it to do to me.
Pseudocode:
For every host in the inventory
For every host-inventoryname
Add the host to the host-inventoryname group.
The one part I'm not sure about is whether host-inventoryname is being defined once and they are only being put in one group or if they are being put in multiple groups each.
Adam
I'm not positive group_by can be used with with_items.
I think we may have proposed making this raise an error, which seems easy to
do if this is the case.
Well, using a fixed list does work, adding every item as a group. It's
when a fact (host-specific variable) is used that a problem occurs:
the value for the first host is used for all hosts.
If you feel you can boil this down to a minimal reproducer example please
file a bug in github and we can take a look. I'd probably just do it with
a hard coded list for starters, just to make sure variables have nothing to
do with it (as it would provide more information if they did).
I have removed the test_items variable and submitted an issue:
https://github.com/ansible/ansible/issues/7540
Sidenote - it's very unusual to put variables below your tasks section. I'd
suggest not doing that as it makes playbooks less readable.
Yeah, that was probably because I went through stripped down a role to
a playbook in alphabetical order.
“It’s
when a fact (host-specific variable) is used that a problem occurs:
the value for the first host is used for all hosts.”
group_by was definitely intended to work with fact based variables, in fact, this is the most common pattern:
- group_by: key=os-{{ ansible_os_family }}
We’ll have to look into the looping scope question.
Ticket is filed here for those interested: https://github.com/ansible/ansible/issues/7540