# How to Work conditionals with "when" an variables

**URL:** <https://forum.ansible.com/t/how-to-work-conditionals-with-when-an-variables/37603>\
**Category:** Ansible Project\
**Created:** [February 1, 2023, 4:39pm UTC](https://forum.ansible.com/t/how-to-work-conditionals-with-when-an-variables/37603 "2023-02-01T16:39:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [February 1, 2023, 4:39pm UTC](https://forum.ansible.com/t/how-to-work-conditionals-with-when-an-variables/37603/1 "2023-02-01T16:39:46Z")

</div>

Hi all, i need to understand how to work with this scenario.

I have this:

- name: “Creating DNS Record In CloudFlare”  
hosts: localhost  
gather\_facts: no  
vars:  
“prod”:  
clfrecord-prod: “prod-{{ carrier }}”  
“stage”:  
clfrecord-stage: “stage-{{ carrier }”  
tasks:  
community.general.cloudflare\_dns:  
zone: [domain.net](http://domain.net)  
record: “{{HERE ADD clfrecord}}”  
type: CNAME  
value: [x-x-xxx.elb.x-x-x.amazonaws.com](http://x-x-xxx.elb.x-x-x.amazonaws.com)  
account\_email:  
account\_api\_key:  
proxied: yes

I passed the vars “carriers” and “env” from console.

I need to compare the console variable “{{ env }}” and if it matches prod add the value of clfrecord-prod to the “record” field, and if it is stage its corresponding value.

any helps?

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [February 1, 2023, 4:49pm UTC](https://forum.ansible.com/t/how-to-work-conditionals-with-when-an-variables/37603/2 "2023-02-01T16:49:02Z")

</div>

You’ve got some convoluted variable definitions. Consider doing this instead  
vars:  
clfrecord:  
prod: “prod-{{ carrier }}”  
stage: “stage-{{ carrier }}”  
Then in your task you can say

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [February 1, 2023, 4:56pm UTC](https://forum.ansible.com/t/how-to-work-conditionals-with-when-an-variables/37603/3 "2023-02-01T16:56:54Z")

</div>

_[I detest the groups interface, btw]_

Let’s try that again.

You’ve got some convoluted variable definitions. Consider doing this instead

vars:  
clfrecord:  
prod: “prod-{{ carrier }}”  
stage: “stage-{{ carrier }}”

Then in your task you can say

record: “{{ clfrecord[env] }}”

Cheers,
