bc81
bc81

Reputation: 187

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

- 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? What am i missing?

Upvotes: 1

Views: 517

Answers (1)

techraf
techraf

Reputation: 68449

Python (hence Ansible) differentiates between an undefined variable and a variable with an empty value.

You define the proxyHost variable in Ansible, but in case when $proxyHost is undefined in shell/environment, you assign proxyHost an empty value.

You need, for example, to compare it to an empty string:

when: proxyHost != ''

Upvotes: 3

Related Questions