I want to exclude all openvz guests the task being run. I’ve tried “when: not (ansible_virtualization_type == ‘openvz’ and ansible_virtualization_role == ‘guest’)” and some variations, but it doesn’t work (“error while evaluating conditional”), seemingly because the variables are not defined. So how can I actually do it?
you can put them into a group, either statically or dynamically with group_by (in a pre play).
then in the play that runs those tasks you can hosts: “all:!openvz”, or if only for a task
when: inventory_hostname not in groups[‘openvz’]
otherwise, add ‘is defined’ to the vars you are checking.
I don’t know the values ahead, so I can’t use a static group. Adding “is defined” won’t change the fact that it fails due to variable being undefined. I understand that it’s the case for dynamic grouping based on a variable, as well.
“Adding “is defined” won’t change the fact that it fails due to variable being undefined.”
Don’t believe that’s true. Conditionals will still short circuit fine.
Also {{ x | default(‘none’) }} == ‘open_vz_whatever’
may also be an option
Yes, I think I confused it with another software that had a similar issue. Thanks.
This is what seems to work for me:
not_openvz_guest: ansible_virtualization_type is not defined or (ansible_virtualization_type is defined and not (ansible_virtualization_type == ‘openvz’ and ansible_virtualization_role == ‘guest’))
shorter version
not_openvz_guest: ansible_virtualization_type is not defined or not
(ansible_virtualization_type == 'openvz' and ansible_virtualization_role ==
'guest')