简体   繁体   English

Ansible中的条件如何使用?

[英]How to use when condition in Ansible?

I cannot figure out this Ansible task 我无法弄清楚这个艰巨的任务

i run my playbook ansible-playbook play.yml -e proxyHost=$proxyHost -e proxyPort=$proxyPort 我运行我ansible-playbook play.yml -e proxyHost=$proxyHost -e proxyPort=$proxyPort

- name: Set proxy when provided
  set_fact: proxyproperty=" -Dhttp.proxyHost={{ proxyHost }} -Dhttp.proxyPort={{ proxyPort }} -Dhttps.proxyHost={{ proxyHost }} -Dhttps.proxyPort={{ proxyPort }} -Dhttp.nonProxyHosts=localhost|127.0.0.1|{{ ip }}|{{ fqdn }}|{{ hostName }}"
  when: proxyHost is defined

So why is it when i have not set $ProxyHost this ansible task is still being triggered? 那么,为什么在我未设置$ProxyHost仍会触发该任务呢? What am i missing? 我想念什么?

Python (hence Ansible) differentiates between an undefined variable and a variable with an empty value. Python(因此为Ansible)区分未定义的变量和具有空值的变量。

You define the proxyHost variable in Ansible, but in case when $proxyHost is undefined in shell/environment, you assign proxyHost an empty value. 您定义proxyHost在Ansible变量,但在当案$proxyHost壳/环境是不确定的,你指定proxyHost空值。

You need, for example, to compare it to an empty string: 例如,您需要将其与空字符串进行比较:

when: proxyHost != ''

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

相关问题 如何在ansible的vars文件中使用if条件 - How can i use if condition in vars file in ansible 如何处理ansible中注册变量的多个when条件 - How to deal with multiple when condition for registered variable in ansible 如何添加在ansible-playbook中满足条件时增加的变量? - How to add a variable that increases when a condition is fulfilled inside an ansible-playbook? 如何在 with_itema 循环中的条件下设置 Ansible 变量的值 - How to set value for Ansible variable upon when condition in with_itema loop 如何使用通配符输出Ansible - How to use wildcard for ansible output 如何在Ansible中使用mongoDB集合输出作为变量 - How to use mongoDB collection output as variables in ansible 如何在模板中使用Ansible库存变量 - How to use Ansible inventory variables in templates 我需要满足条件时使用ansible replace模块替换jinja2模板中变量的内容 - I need to replace the content of the variable in jinja2 template using ansible replace module when it satisfies when condition 从远程主机上的JSON文件中读取变量,并将其用作ansible剧本中任务的条件 - Read variable from JSON file on remote host and use it as condition for the task in ansible playbook 如何在MATLAB中使用if条件变量? - How to use if condition on a variable in MATLAB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM