# Validate case insensitive values

**URL:** <https://forum.ansible.com/t/validate-case-insensitive-values/34493>\
**Category:** Ansible Project\
**Tags:** awx\
**Created:** [January 22, 2021, 3:53pm UTC](https://forum.ansible.com/t/validate-case-insensitive-values/34493 "2021-01-22T15:53:02Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amjad\_Saleem1](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Amjad\_Saleem1](https://forum.ansible.com/u/Amjad_Saleem1)\
**Post date:** [January 22, 2021, 3:53pm UTC](https://forum.ansible.com/t/validate-case-insensitive-values/34493/1 "2021-01-22T15:53:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [January 22, 2021, 3:54pm UTC](https://forum.ansible.com/t/validate-case-insensitive-values/34493/2 "2021-01-22T15:54:19Z")

</div>

when: version|upper not in environments

---

<div class="post-metadata">

**Author:** ![Amjad\_Saleem1](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Amjad\_Saleem1](https://forum.ansible.com/u/Amjad_Saleem1)\
**Post date:** [January 25, 2021, 2:27am UTC](https://forum.ansible.com/t/validate-case-insensitive-values/34493/3 "2021-01-25T02:27:57Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 25, 2021, 6:10am UTC](https://forum.ansible.com/t/validate-case-insensitive-values/34493/4 "2021-01-25T06:10:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Amjad\_Saleem1](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Amjad\_Saleem1](https://forum.ansible.com/u/Amjad_Saleem1)\
**Post date:** [January 25, 2021, 1:54pm UTC](https://forum.ansible.com/t/validate-case-insensitive-values/34493/5 "2021-01-25T13:54:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 25, 2021, 3:07pm UTC](https://forum.ansible.com/t/validate-case-insensitive-values/34493/6 "2021-01-25T15:07:41Z")

</div>

> Trying this:
> 
> vars:  
> &nbsp;&nbsp;&nbsp;&nbsp;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.

> &nbsp;&nbsp;&nbsp;env: NA
> 
> tasks:  
> - fail: msg="Not valid value"  
> &nbsp;&nbsp;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":

---

<div class="post-metadata">

**Author:** ![Amjad\_Saleem1](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Amjad\_Saleem1](https://forum.ansible.com/u/Amjad_Saleem1)\
**Post date:** [January 25, 2021, 5:00pm UTC](https://forum.ansible.com/t/validate-case-insensitive-values/34493/7 "2021-01-25T17:00:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 25, 2021, 5:20pm UTC](https://forum.ansible.com/t/validate-case-insensitive-values/34493/8 "2021-01-25T17:20:37Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![Amjad\_Saleem1](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Amjad\_Saleem1](https://forum.ansible.com/u/Amjad_Saleem1)\
**Post date:** [January 31, 2021, 6:34pm UTC](https://forum.ansible.com/t/validate-case-insensitive-values/34493/9 "2021-01-31T18:34:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 31, 2021, 10:34pm UTC](https://forum.ansible.com/t/validate-case-insensitive-values/34493/10 "2021-01-31T22:34:18Z")

</div>

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

- hosts: localhost  
&nbsp;&nbsp;connection: local  
&nbsp;&nbsp;gather\_facts: no

&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;environments:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- DEv  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- UAt  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- PrOd

&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- fail:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: Your env '{{ env }}' was NOT found in environments  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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]
