adplus-dvertising

how does one combine conditionals into one "when" statement?

Asked 3 months ago
Viewed 16 times

Ansible 2.10.x

I looked at How to define multiple when conditions in Ansible, and similar posts.

I'm trying to test if 2 different substring are in a variable. I've tried

default/main.yml
----------------
# Default path can be overridden in task
repo_url: "https://someUrl/development"

tasks/main.yml
--------------
- debug:
    msg: "URL={{ repo_url }}"
- name: Override default path
  set_fact:
    repo_url: "https://someUrl/releases"
  when: ('"development" not in web_version') and 
        ('"feature" not in web_version')
- debug:
    msg: "URL={{ repo_url }}"

I use above task like this for example

$ ansible-playbook ... -e web_version=development_ myTask.yml

But I get

TASK [exa-web : debug] *************************************************
ok: [10.227.x.x] => {
    "msg": "URL=https://someUrl/development"
}
TASK [exa-web : Override default path] *************************************************
ok: [10.227.x.x]

TASK [exa-web : debug] *************************************************
ok: [10.227.x.x] => {
    "msg": "URL=https://someUrl/releases"
}

I don't expect the set_fact task to run, but it does; hence it overrides the default repo_url. So apparently I'm setting my when condition wrong.

I've also tried this to no avail.

- name: Override default path
  set_fact:
    repo_url: "https://someUrl/releases"
  when: '"development_" not in web_version and 
         "feature_" not in web_version'

Essentially, I need the task to run if I execute my playbook like this

$ ansible-playbook ... -e web_version=1.4.44 myTask.yml

What's the correct syntax? TIA

UPDATE

Seems like when doesn't like ()? I just simplified the condition for now, and this works

- name: Override default path
  set_fact:
    repo_url: "https://someUrl/releases"
  when: '"development" not in web_version'

but not this?

- name: Override default path
  set_fact:
    repo_url: "https://someUrl/releases"
  when: ('"development" not in web_version')

Really???

asked 3 months ago

Correct Answer

Your second attempt...

- name: Override default path
  set_fact:
    repo_url: "https://someUrl/releases"
  when: '"development_" not in web_version and 
         "feature_" not in web_version'

...seems syntactically correct. In a playbook like this:

- hosts: localhost
  gather_facts: false
  vars:
    repo_url: "https://someUrl/development"
  tasks:
  - name: Override default path
    set_fact:
      repo_url: "https://someUrl/releases"
    when: '"development_" not in web_version and 
           "feature_" not in web_version'

  - debug:
      msg: "URL={{ repo_url }}"

If we run it like this:

ansible-playbook -e web_version=development_ playbook.yaml

We see as output:

TASK [Override default path] ****************************************************************************
skipping: [localhost]

TASK [debug] ********************************************************************************************
ok: [localhost] => {
    "msg": "URL=https://someUrl/development"
}

And if we run it like this:

ansible-playbook -e web_version=1.4.44 playbook.yaml

We see:

TASK [Override default path] ****************************************************************************
ok: [localhost]

TASK [debug] ********************************************************************************************
ok: [localhost] => {
    "msg": "URL=https://someUrl/releases"
}

That seems to do exactly what you want. Note that you're looking for the string development_ (with a trailing underscore) in your when statement, rather than development as in the first example, but that's an easy fix.


While your code works just fine, I find it helpful to use one of YAML's quote operators for writing multi-line when statements, since it avoids me getting confused by nested quotes in the expression:

- hosts: localhost
  gather_facts: false
  vars:
    repo_url: "https://someUrl/development"
  tasks:
  - name: Override default path
    set_fact:
      repo_url: "https://someUrl/releases"
    when: >-
      "development_" not in web_version and
      "feature_" not in web_version

  - debug:
      msg: "URL={{ repo_url }}"

Re: your update, this doesn't work...

- name: Override default path
  set_fact:
    repo_url: "https://someUrl/releases"
  when: ('"development" not in web_version')

...because of bad quoting. You are effectively writing:

when: ("a string")

And a non-empty string evaluates as true in a boolean expression. Always put the quotes at the beginning of the expression. E.g., this works just fine:

when: >-
  ("development" not in web_version)

As does the syntactically identical:

when: '("development" not in web_version)'
answered 3 months ago