# Update string variable if the value matches in Ansible Play

**URL:** <https://forum.ansible.com/t/update-string-variable-if-the-value-matches-in-ansible-play/31254>\
**Category:** Ansible Project\
**Created:** [September 11, 2019, 11:02am UTC](https://forum.ansible.com/t/update-string-variable-if-the-value-matches-in-ansible-play/31254 "2019-09-11T11:02:49Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [September 11, 2019, 11:02am UTC](https://forum.ansible.com/t/update-string-variable-if-the-value-matches-in-ansible-play/31254/1 "2019-09-11T11:02:49Z")

</div>

I pass a variable fRegion to my ansible playbook.

I wish to set fRegion to this string → ‘prd’ OR region LIKE ‘dr’

if the passed value is either “prd\_and\_dr” or “dr\_and\_prd”.

I call the playbook like this :

`ansible-playbook site.yml -e "fRegion=dr_and_prd"`

Here is my attempted playbook that does not help.

`

- name: “Play 1-Find the details here”

hosts: localhost  
any\_errors\_fatal: True  
vars:

fRegion: “‘prd’ OR region LIKE ‘dr’”  
when: fRegion == “prd\_and\_dr” or fRegion == “dr\_and\_prd”  
ansible\_ssh\_extra\_args: -o StrictHostKeyChecking=no

debug:  
msg: “{{ fRegion }}”  
`

Can you please suggest ?

---

<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:** [September 11, 2019, 3:45pm UTC](https://forum.ansible.com/t/update-string-variable-if-the-value-matches-in-ansible-play/31254/2 "2019-09-11T15:45:45Z")

</div>

I am truly confused about the logic.

You’re trying to set a variable based on itself, OR itself?

Could you maybe explain what you are trying to achieve?

Even within this information I would definitely restructure things…

Dick

---

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [September 11, 2019, 4:03pm UTC](https://forum.ansible.com/t/update-string-variable-if-the-value-matches-in-ansible-play/31254/3 "2019-09-11T16:03:14Z")

</div>

@Dick hi,

We can definitely use a different variable here

`fRegion: "'prd' OR region LIKE 'dr'"`

I wanted to have less variables hence kept it the same.

The variable is used in a complex SQL query that is run by my playbook and as we know SQL queries need single quotes (')

Thus, the fRegion variable or any other should be assigned the string which will embed in the SQL query to get it to work.

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [September 11, 2019, 4:28pm UTC](https://forum.ansible.com/t/update-string-variable-if-the-value-matches-in-ansible-play/31254/4 "2019-09-11T16:28:37Z")

</div>

If I understand correctly you have a variable called fRegion that contains a value.

You want to change that variable to contain the string “‘prd’ OR region LIKE ‘dr’” if the variable initially contains either the value “prd\_or\_dr” or the value “dr\_or\_prd”.

- set\_fact:  
fRegion: “‘prd’ OR region LIKE ‘dr’”  
when: fRegion == ‘prd\_or\_dr’ or fRegion == ‘dr\_or\_prod’

The problem with doing this is that you have lost the original value of fRegion now. And you are IMHO misusing the variable; a variable should in general either be a control variable (one that controls program flow) or data. Using the same variable to contain both control information and data at different times is confusing and leads to error. This would be nicer:

- set\_fact:  
sqlFragment: “‘some default value’”

- set\_fact:  
sqlFragment: “‘prd’ OR region LIKE ‘dr’”  
when: fRegion == ‘prd\_or\_dr’ or fRegion == ‘dr\_or\_prd’

Then it is completely clear that you are testing fRegion to find out what yor SQL fragment should be.

Regards, K.

---

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [September 11, 2019, 4:33pm UTC](https://forum.ansible.com/t/update-string-variable-if-the-value-matches-in-ansible-play/31254/5 "2019-09-11T16:33:28Z")

</div>

@Karl you have correctly understood my requirement.

However, can you propose a solution with vars instead of using set\_fact ?

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [September 11, 2019, 5:31pm UTC](https://forum.ansible.com/t/update-string-variable-if-the-value-matches-in-ansible-play/31254/6 "2019-09-11T17:31:10Z")

</div>

In this case you could use a ternary filter:

vars:  
f1: “‘prd’ OR region LIKE ‘dr’”  
f2: “some\_default”  
sqlFragment: “{{(fRegion == ‘prd\_or\_dr’ or fRegion == ‘dr\_or\_prd’)|ternary(f1, f2)}}”

You could also use membership in a list of values as your condition:

vars:  
f1: “‘prd’ OR region LIKE ‘dr’”  
f2: “some\_default”  
vals: [‘dr\_or\_prd’, ‘prd\_or\_dr’]

sqlFragment: “{{(fRegion in vals) |ternary(f1, f2)}}”

BTW I used the f1 and f2 variables because it’s the easiest way (maybe the only way) to get single quotes into a variable.

IMHO this is not a good solution. The set\_facts solution is much easier to understand (and you can combine it with the list of values idea to simplify it).

Regards, K.

---

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [September 12, 2019, 4:59am UTC](https://forum.ansible.com/t/update-string-variable-if-the-value-matches-in-ansible-play/31254/7 "2019-09-12T04:59:41Z")

</div>

@Karl your set\_fact recoomendation has syntax issues …

Below is how it fails.

`- name: "Play 1-Find the details here"`

`hosts: localhost`  
`any_errors_fatal: True`  
`vars:`  
`ansible_ssh_extra_args: -o StrictHostKeyChecking=no`  
`set_fact:`  
`fRegion: "'prd' OR region LIKE 'dr'"`  
`when: fRegion == 'prd_and_dr' or fRegion == 'dr_and_prd'`

`debug:`  
`msg: "{{ fRegion }}"`

Output:

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [September 12, 2019, 5:15am UTC](https://forum.ansible.com/t/update-string-variable-if-the-value-matches-in-ansible-play/31254/8 "2019-09-12T05:15:37Z")

</div>

That error is in code you added. I’m sure you’ll figure it out.

The fragments I supply here are tested unless I say otherwise.

Regards, K.

---

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [September 12, 2019, 9:38am UTC](https://forum.ansible.com/t/update-string-variable-if-the-value-matches-in-ansible-play/31254/9 "2019-09-12T09:38:40Z")

</div>

@Karl I tried set\_fact as suggested but the assigned value is not getting substituted to the variable fRegion. Please look at the red bold text in the output below.

Below is the playbook:

cat finalattempt.yml

`

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [September 12, 2019, 10:02am UTC](https://forum.ansible.com/t/update-string-variable-if-the-value-matches-in-ansible-play/31254/10 "2019-09-12T10:02:30Z")

</div>

I’m sorry, I just don’t know what’s wrong with that. Please provide the exact SQL statement you WANT to see, so we can compare it with the SQL statement you are actually getting.

I can’t help feeling it would be simpler to provide just two booleans “dr” and “prod” and pass in one or both, rather than comparing strings.

Regards, K.

---

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [September 12, 2019, 11:11am UTC](https://forum.ansible.com/t/update-string-variable-if-the-value-matches-in-ansible-play/31254/11 "2019-09-12T11:11:15Z")

</div>

@Karl, the user passes fRegion=prd\_and\_dr as evident from my previous post.

`  
ansible-playbook -vv finalattempt.yml -e “My\_Number=54321 Layer=APP Environment=UAT fRegion=prd\_and\_dr”

`

In my playbook the below condition should be met (true)

`when: fRegion == 'prd_and_dr' or fRegion == 'dr_and_prd'`

Thus, the fRegion variable should now get the value below instead of the one passed to the playbook.

`fRegion: "'prd_only' OR region LIKE 'dr_only'"`

However, in the output you can see ‘{{ fRegion }}’ the persists dr\_and\_prd value marked in **BOLD RED** in my previous post instead of printing (substituting) the new value i.e. ‘prd\_only’ OR region LIKE ‘dr\_only’

**Can you let me know how fRegion variable can be overwritten once the when condition is met ?**

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [September 12, 2019, 1:45pm UTC](https://forum.ansible.com/t/update-string-variable-if-the-value-matches-in-ansible-play/31254/12 "2019-09-12T13:45:48Z")

</div>

My script uses inline variables, your script is passing in variables on the command line.

It isn’t obvious, but the fRegion you are setting is actually a different variable to the one you are using later! To see this in action, try this script using the command line “ansible-playbook -v scriptname -e envvar=fred”:

---

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [September 12, 2019, 4:25pm UTC](https://forum.ansible.com/t/update-string-variable-if-the-value-matches-in-ansible-play/31254/13 "2019-09-12T16:25:11Z")

</div>

Works !! and thank you for the explanation 🙂 Anyways, i can thank you ??

Below is the final working script.

`
