Validate case insensitive values

Trying to validate that a specific variable must have one of pre-determined values, mimicking dropdown in survey in ansible tower/awx.

I have the following code but it only matches for upper case:

vars:
environments: [DEV, UAT, PROD]

env: DEV

tasks:

  • fail: msg=“Not valid value”
    when: version not in environments

Is there a way to achieve the survey’s dropdown functionality that is case insensitive.

Thanks in advance.

when: version|upper not in environments

This matches even when part of the version is in an element of environments, eg

vars:
environments: ['DEV Managed, UAT in the datacenter, PROD in the cloud and data center]

env: NA

tasks:

  • fail: msg=“Not valid value”
    when: version|upper not in environments |upper

As NA is in Managed, it matches and fail does not fail.

I tried quoting the elements in environments and env in ‘when clause’ but it did not work.

Is there a way to match the whole element instead of part of it?

Your environments variable definition seems to be incorrectly quoted so I’m not even sure what that list looks like now.
You can also try the yaml list syntax:

environments:

  • DEV Managed
  • UAT somewhere else
  • PROD blah

Also you compare ‘version’ but your example mentions ‘env’ (which is also inconsistently indented).

Could you fix those and check again?

Trying this:

vars:
environments: ['DEV Managed, UAT in the datacenter, PROD (in the cloud and data center)’]

env: NA

tasks:

  • fail: msg=“Not valid value”

when: env |upper not in environments |upper

and with the yaml list, esult is the same, NA matched Managed.

What I found is that if env is a single letter, it will match if that letter is in environments.

Trying this:

vars:
    environments: ['DEV Managed, UAT in the datacenter, PROD (in the cloud and data center)’]

As I've explained this is incorrect and does not mean what you think it means.

   env: NA

tasks:
- fail: msg="Not valid value"
  when: env |upper not in environments |upper

This again is different from the solution that was handed to you.

and with the yaml list, esult is the same, NA matched Managed.

What I found is that if env is a single letter, it will match if that letter is in environments.

That is because you didn't use the answers that were given, but
instead kept on using the incorrect syntax.

To come back to your original question, this "works for me":

You have env: Acceptance in there but if you had it DEV or UAT or PROD, it would have worked. I mean If you use DEV or UAT or PROD as value of env, then it’s all good which is fine. The problem is when the value of env is just D (first letter of DEV) ie env: D, it still matches DEV.

No it doesn't. It works as you want.

(ansible-2.9.15) dick.visser@mbp ~$ ansible-playbook
work/tasks/list3.yml -e env=DEV
[WARNING]: No inventory was parsed, only implicit localhost is available

PLAY [localhost] ***************************************************************

TASK [fail] ********************************************************************
skipping: [localhost]

PLAY RECAP *********************************************************************
localhost : ok=0 changed=0 unreachable=0
failed=0 skipped=1 rescued=0 ignored=0

And:

(ansible-2.9.15) dick.visser@geant-ams-049 ~$ ansible-playbook
work/tasks/list3.yml -e env=D
[WARNING]: No inventory was parsed, only implicit localhost is available

PLAY [localhost]

The reason it works for you seems to be that you are not changing case for environments.
This works:

I see.
I think 'upper' should be used on a list using 'map':

- hosts: localhost
  connection: local
  gather_facts: no

  vars:
    environments:
      - DEv
      - UAt
      - PrOd

  tasks:
    - fail:
        msg: Your env '{{ env }}' was NOT found in environments
      when: env|upper not in environments|map('upper')

~$ ansible-playbook work/tasks/list3.yml -e env=deV
[WARNING]: No inventory was parsed, only implicit localhost is available

PLAY [localhost]