2

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']?

4

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
0

Generic task version

- name: run on all but the first host in a play
  some_module: some_param=some_value
  when: inventory_hostname != play_hosts[0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.