Run ansible task on other members of a group, but not the current member

ansible

So I have a group of nginx server:

[nginx_internal_servers]
n01.local
n02.local
n03.local

And I have a pre deploy task to run. I'm running in serial:1 mode, and I only want this pre deploy task to run on everyone other than me.

Currently my task looks like this, which runs on all nginx servers perfectly:

pre_tasks:
    - name: Take service out of nginx upstream pools
          local_action: command {{ playbook_dir }}/scripts/nginx-upstream-management.sh -s {{ item[0] }} -r {{ item[2] }} -g {{ item[1] }}
          with_nested:
            - groups['nginx_internal_servers']
            - services_endpoints.keys()
            - ansible_all_ipv4_addresses|last

Any ideas how to exclude the current node from the list groups['nginx_internal_servers']?

Best Answer

Got it! Use a when :)

pre_tasks:
    - name: Take service out of nginx upstream pools
          local_action: command {{ playbook_dir }}/scripts/nginx-upstream-management.sh -s {{ item[0] }} -r {{ item[2] }} -g {{ item[1] }}
          with_nested:
            - groups['nginx_internal_servers']
            - services_endpoints.keys()
            - ansible_all_ipv4_addresses|last
          when: item[0] != inventory_hostname