Run a task only if the host does not belong to the group

I would like to start the task only if the host of the current play does not belong to a specific group. In the floor pseudo-code:

- name: my command command: echo stuff when: "if {{ ansible_hostname }} not in {{ ansible_current_groups }}" 

How should I do it?

+56
ansible
Jan 08 '14 at
source share
2 answers

Here is another way to do this:

 - name: my command command: echo stuff when: "'groupname' not in group_names" 

group_names is the magic variable described here: http://docs.ansible.com/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts :

group_names is a list (array) of all groups in which the current host is located.

+106
Mar 24 '14 at 15:50
source share

You can set a control variable in the vars files located in group_vars/ , or directly in the hosts file as follows:

 [vagrant:vars] test_var=true [location-1] 192.168.33.10 hostname=apollo [location-2] 192.168.33.20 hostname=zeus [vagrant:children] location-1 location-2 

And complete the following tasks:

 - name: "test" command: "echo {{test_var}}" when: test_var is defined and test_var 
+11
Jan 08 '14 at 23:37
source share



All Articles