Multiple values in when condition in ansible playbook

Viewed 365

I have to run a shell script only if selected server (derived from external vars) is not equal to given ip (in when condition ) , The problem here is there are many ip i can add all the ip list in the when condition but the list is huge which results in lengthy playbook . Is a best way to reduce the line of code . The present script is

 - name: Copy self sign certificate script
      copy:
        src: ../playbooks/self_sign_cert.sh
        dest: /tmp/self_sign_cert.sh
        mode: 0755
      ignore_errors: yes
      when:  selected_server != '10.0.0.1' and selected_server != '10.0.0.2' and  selected_server != '10.0.0.3' and selected_server != '10.0.0.4' and selected_server != '10.0.0.4' and selected_server != '10.0.0.5' and selected_server != '10.0.0.6' and selected_server != '10.0.0.7'
1 Answers

For example

- hosts: 10.0.0.2,10.0.0.98,10.0.0.99
  vars:
    black_list:
      - 10.0.0.1
      - 10.0.0.2
      - 10.0.0.3
  tasks:
    - debug:
        msg: "Copy file to {{ inventory_hostname }}"
      when: inventory_hostname not in black_list

gives

TASK [debug] *********************************************************************
skipping: [10.0.0.2]
ok: [10.0.0.98] => 
  msg: Copy file to 10.0.0.98
ok: [10.0.0.99] => 
  msg: Copy file to 10.0.0.99
Related