Validate case insensitive values

46 views
Skip to first unread message

PluftPlayzRoblox

unread,
Jan 22, 2021, 10:53:02 AM1/22/21
to Ansible Project

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.

Matt Martz

unread,
Jan 22, 2021, 10:55:13 AM1/22/21
to ansible...@googlegroups.com
when: version|upper not in environments

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1e820f7b-da58-4aa4-bc66-dc885350946bn%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net

PluftPlayzRoblox

unread,
Jan 24, 2021, 9:27:57 PM1/24/21
to Ansible Project
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?

Dick Visser

unread,
Jan 25, 2021, 1:11:22 AM1/25/21
to ansible...@googlegroups.com
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? 

--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

PluftPlayzRoblox

unread,
Jan 25, 2021, 8:54:34 AM1/25/21
to ansible...@googlegroups.com
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.

You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/Qn7B7315cho/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAL8fbwOWt7HDUkYZcPvFFt8cPz1YfZ3LcB1-_r2YHH12BawQfg%40mail.gmail.com.

Dick Visser

unread,
Jan 25, 2021, 10:08:09 AM1/25/21
to ansible...@googlegroups.com
On Mon, 25 Jan 2021 at 14:54, PluftPlayzRoblox <eshte...@gmail.com> wrote:
>
> 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":

---
- hosts: localhost
connection: local
gather_facts: no

vars:
environments:
- DEV
- UAT
- PROD

env: Acceptance

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





--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

PluftPlayzRoblox

unread,
Jan 25, 2021, 12:01:22 PM1/25/21
to ansible...@googlegroups.com
    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.


--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/Qn7B7315cho/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

Dick Visser

unread,
Jan 25, 2021, 12:20:59 PM1/25/21
to ansible...@googlegroups.com
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]
*******************************************************************************************

TASK [fail] ************************************************************************************************
fatal: [localhost]: FAILED! => changed=false
msg: Not a valid value

PLAY RECAP *************************************************************************************************
localhost : ok=0 changed=0 unreachable=0
failed=1 skipped=0 rescued=0 ignored=0
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAN0Enz0p340XFu5yd-C1%2BygsB9Mqo-8Ry9oAv84ia%3Dxo1F6YHQ%40mail.gmail.com.

PluftPlayzRoblox

unread,
Jan 31, 2021, 1:34:12 PM1/31/21
to Ansible Project
The reason it works for you seems to be that you are not changing case for environments.
This works:

---
- hosts: localhost
  connection: local
  gather_facts: no

vars:
   environments:
      - DeV
      - Uat
      - PROD

env: d


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

But this does not:
---
- hosts: localhost
  connection: local
  gather_facts: no

vars:
   environments:
      - DeV
      - Uat
      - PROD

env: d


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

Dick Visser

unread,
Jan 31, 2021, 5:34:44 PM1/31/21
to ansible...@googlegroups.com
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]
*************************************************************************************************************

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

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


And for substring it does not match:

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

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

TASK [fail] ******************************************************************************************************************
fatal: [localhost]: FAILED! => changed=false
msg: Your env 'd' was NOT found in environments

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

> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/bc7ec796-652c-4357-ac60-ec4a397c5f3an%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages