简体   繁体   English

ansible 剧本中 when 条件中的多个值

[英]Multiple values in when condition in ansible playbook

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. 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列表很大,导致剧本冗长。 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'

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM