# using set\_fact with until

**URL:** <https://forum.ansible.com/t/using-set-fact-with-until/31252>\
**Category:** Ansible Project\
**Created:** [September 11, 2019, 7:22am UTC](https://forum.ansible.com/t/using-set-fact-with-until/31252 "2019-09-11T07:22:50Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kamesh\_Sampath1](https://avatars.discourse-cdn.com/v4/letter/k/90ced4/32.png) [@Kamesh\_Sampath1](https://forum.ansible.com/u/Kamesh_Sampath1)\
**Post date:** [September 11, 2019, 7:22am UTC](https://forum.ansible.com/t/using-set-fact-with-until/31252/1 "2019-09-11T07:22:50Z")

</div>

I am trying to use `set_fact` with `until` and `retries`, will the fact be evaluated and set during each iteration ? Sometimes I see the value is not being set and I get the failure after the retries. While I see the corresponding lookup returns value using another cli.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 11, 2019, 8:39am UTC](https://forum.ansible.com/t/using-set-fact-with-until/31252/2 "2019-09-11T08:39:49Z")

</div>

Yes. The the fact will be evaluated and set on each iteration. The problem is  
that "set\_fact" declares the variable "che\_keycloak". The condition

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;until: che\_keycloak.spec is defined

can't find the parameter "spec" because there isn't any.

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Kamesh\_Sampath1](https://avatars.discourse-cdn.com/v4/letter/k/90ced4/32.png) [@Kamesh\_Sampath1](https://forum.ansible.com/u/Kamesh_Sampath1)\
**Post date:** [September 11, 2019, 8:44am UTC](https://forum.ansible.com/t/using-set-fact-with-until/31252/3 "2019-09-11T08:44:50Z")

</div>

> > I am trying to use `set_fact` with `until` and `retries`, will the fact be  
> > evaluated and set during each iteration ? Sometimes I see the value is not  
> > being set and I get the failure after the retries. While I see the  
> > corresponding lookup returns value using another cli.
> > 
> > - name: “Get Che Keycloak facts”
> > 
> > > set\_fact:  
> > > che\_keycloak:  
> > > “{{ lookup(‘k8s’, api\_version=‘[route.openshift.io/v1](http://route.openshift.io/v1)’,kind=‘Route’,resource\_name=‘keycloak’,namespace=‘che’) }}”  
> > > register: che\_keycloak\_route\_result  
> > > until: che\_keycloak.spec is defined  
> > > retries: 30  
> > > delay: 10  
> > > when: state == ‘present’
> 
> Yes. The the fact will be evaluated and set on each iteration. The problem is  
> that “set\_fact” declares the variable “che\_keycloak”. The condition
> 
> until: che\_keycloak.spec is defined
> 
> can’t find the parameter “spec” because there isn’t any.

do you mean to say I should invert the condition for this logic to work ?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 11, 2019, 8:50am UTC](https://forum.ansible.com/t/using-set-fact-with-until/31252/4 "2019-09-11T08:50:10Z")

</div>

Take a look at the registered variable "che\_keycloak\_route\_result" to see  
what's going on

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: che\_keycloak\_route\_result

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 11, 2019, 9:06am UTC](https://forum.ansible.com/t/using-set-fact-with-until/31252/5 "2019-09-11T09:06:12Z")

</div>

No. I simply mean "spec" is not defined. The question is why? Take a look at  
the registered variable. This should provide you with this information.

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Kamesh\_Sampath1](https://avatars.discourse-cdn.com/v4/letter/k/90ced4/32.png) [@Kamesh\_Sampath1](https://forum.ansible.com/u/Kamesh_Sampath1)\
**Post date:** [September 11, 2019, 9:34am UTC](https://forum.ansible.com/t/using-set-fact-with-until/31252/6 "2019-09-11T09:34:44Z")

</div>

> > > Yes. The the fact will be evaluated and set on each iteration. The problem  
> > > is  
> > > that “set\_fact” declares the variable “che\_keycloak”. The condition
> > > 
> > > until: che\_keycloak.spec is defined
> > > 
> > > can’t find the parameter “spec” because there isn’t any.
> > 
> > do you mean to say I should invert the condition for this logic to work ?
> 
> No. I simply mean “spec” is not defined. The question is why? Take a look at  
> the registered variable. This should provide you with this information.

the spec becomes available only after few seconds; so I need to keep the lookup run until then. Thats why I had that until checking to see if spec is defined in the fact.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 11, 2019, 9:50am UTC](https://forum.ansible.com/t/using-set-fact-with-until/31252/7 "2019-09-11T09:50:26Z")

</div>

What is the failure (copy&paste) that "Sometimes I see the value is not being  
set and I get the failure after the retries."?

---

<div class="post-metadata">

**Author:** ![Kamesh\_Sampath1](https://avatars.discourse-cdn.com/v4/letter/k/90ced4/32.png) [@Kamesh\_Sampath1](https://forum.ansible.com/u/Kamesh_Sampath1)\
**Post date:** [September 11, 2019, 9:57am UTC](https://forum.ansible.com/t/using-set-fact-with-until/31252/8 "2019-09-11T09:57:51Z")

</div>

> > > > > that “set\_fact” declares the variable “che\_keycloak”. The condition
> > > > > 
> > > > > until: che\_keycloak.spec is defined
> > > > > 
> > > > > can’t find the parameter “spec” because there isn’t any.
> > > > 
> > > > do you mean to say I should invert the condition for this logic to work ?
> > > 
> > > No. I simply mean “spec” is not defined. The question is why? Take a look  
> > > the registered variable. This should provide you with this information.
> > 
> > the spec becomes available only after few seconds; so I need to keep the  
> > lookup run until then. Thats why I had that until checking to see if spec  
> > is defined in the fact.
> 
> What is the failure (copy&paste) that “Sometimes I see the value is not being  
> set and I get the failure after the retries.”?

Exactly I see the value is not being set inspite of that is available after few seconds . The failure throws an error like  
“fatal: [localhost]: FAILED! =\> {“ansible\_facts”: {“che\_keycloak”: }, “attempts”: 30, “changed”: false}”

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 11, 2019, 10:51am UTC](https://forum.ansible.com/t/using-set-fact-with-until/31252/9 "2019-09-11T10:51:00Z")

</div>

OK. Set "ignore\_errors: true", register and print "result". Post it.

&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;che\_keycloak: "{{ lookup('k8s',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;api\_version='route.openshift.io/v1',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;kind='Route',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resource\_name='keycloak',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;namespace='che') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: result  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;until: che\_keycloak.spec is  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;defined retries: 30 delay: 10  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: state == 'present'

&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: result

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 11, 2019, 10:55am UTC](https://forum.ansible.com/t/using-set-fact-with-until/31252/10 "2019-09-11T10:55:20Z")

</div>

Fixed syntax.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 11, 2019, 12:29pm UTC](https://forum.ansible.com/t/using-set-fact-with-until/31252/11 "2019-09-11T12:29:33Z")

</div>

With set\_fact and until it will not set the variable on each iteration unfortunately.  
I don't know of any workaround.

---

<div class="post-metadata">

**Author:** ![Kamesh\_Sampath1](https://avatars.discourse-cdn.com/v4/letter/k/90ced4/32.png) [@Kamesh\_Sampath1](https://forum.ansible.com/u/Kamesh_Sampath1)\
**Post date:** [September 11, 2019, 12:32pm UTC](https://forum.ansible.com/t/using-set-fact-with-until/31252/12 "2019-09-11T12:32:08Z")

</div>

> > I am trying to use `set_fact` with `until` and `retries`, will the fact  
> > be  
> > evaluated and set during each iteration ? Sometimes I see the value is  
> > not  
> > being set and I get the failure after the retries. While I see the  
> > corresponding lookup returns value using another cli.
> > 
> > - name: “Get Che Keycloak facts”
> > 
> > > set\_fact:  
> > > che\_keycloak:  
> > > “{{ lookup(‘k8s’,  
> > > api\_version=‘[route.openshift.io/v1](http://route.openshift.io/v1)’,kind=‘Route’,resource\_name=‘keycloak’,namespace=‘che’)  
> > > }}”  
> > > register: che\_keycloak\_route\_result  
> > > until: che\_keycloak.spec is defined  
> > > retries: 30  
> > > delay: 10  
> > > when: state == ‘present’
> 
> With set\_fact and until it will not set the variable on each iteration  
> unfortunately.

That is what I observe ☹

> I don’t know of any workaround.

any other better way you think of doing my task?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 11, 2019, 12:52pm UTC](https://forum.ansible.com/t/using-set-fact-with-until/31252/13 "2019-09-11T12:52:35Z")

</div>

You can add the pause module and just wait long enough.

I have not used the Kubernetes, but if it's a k8s module or and API with URI module that can do the same, that with register would function with until.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 11, 2019, 7:57pm UTC](https://forum.ansible.com/t/using-set-fact-with-until/31252/14 "2019-09-11T19:57:18Z")

</div>

The "lookup" plugin is evaluated only once before the module starts [1].  
Hence it's not possible to get it working with the until/retries construct.

FWIW, it is possible to put it together. The "brute-force" playbook below

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- hosts: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_continue: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_delay: 10  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_retries: 5  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- include\_tasks: include-lookup.yml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ range(0, my\_retries)|list }}"

with the included file include-lookup.yml

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: 'Get data'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_var: "{{ lookup('pipe', '/scratch/tmp/my\_script')|from\_yaml }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: my\_continue  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: 'Set continue=false when data OK'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_continue: false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- my\_var.spec is defined  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- my\_continue  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: 'Delay next iteration'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wait\_for:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timeout: "{{ my\_delay }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: my\_continue

does the job. The concept works. When the variable "my\_var.spec" is defined  
then the variable "continue" will be set to False and following tasks will be  
skipped.

[1] The "lookup" plugin is evaluated only once before the module starts. From  
&nbsp;&nbsp;&nbsp;&nbsp;the log written by "my\_script" cat be seen that "lookup" is evaluated  
&nbsp;&nbsp;&nbsp;&nbsp;only once.

&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- shell: "echo {{ lookup('pipe', '/scratch/tmp/my\_script') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;# - shell: "/scratch/tmp/my\_script"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: result  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;until: result.stdout == 'DUMMY'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retries: 3  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delay: 2

&nbsp;&nbsp;&nbsp;&nbsp;$ cat my\_script  
&nbsp;&nbsp;&nbsp;&nbsp;#!/bin/sh  
&nbsp;&nbsp;&nbsp;&nbsp;my\_data=/scratch/tmp/my\_data  
&nbsp;&nbsp;&nbsp;&nbsp;my\_log=/scratch/tmp/my\_script.log  
&nbsp;&nbsp;&nbsp;&nbsp;if [-e ${my\_data}]; then  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cat ${my\_data}  
&nbsp;&nbsp;&nbsp;&nbsp;fi  
&nbsp;&nbsp;&nbsp;&nbsp;date \>\> ${my\_log}

&nbsp;&nbsp;&nbsp;&nbsp;$ cat my\_data  
&nbsp;&nbsp;&nbsp;&nbsp;spec: 'Specification'

HTH. Cheers,

&nbsp;&nbsp;- vlado

---

<div class="post-metadata">

**Author:** ![flowerysong1](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@flowerysong1](https://forum.ansible.com/u/flowerysong1)\
**Post date:** [September 11, 2019, 9:50pm UTC](https://forum.ansible.com/t/using-set-fact-with-until/31252/15 "2019-09-11T21:50:24Z")

</div>

That seems overly complex. I would instead wait until the required data is available before running set\_fact:

`

- vars:  
che\_keycloak\_lookup: “{{ lookup(‘k8s’, api\_version=‘[route.openshift.io/v1’,kind=‘Route’,resource=‘keycloak’,namespace='che](http://route.openshift.io/v1',kind='Route',resource='keycloak',namespace='che)’) }}”  
block:

- debug:  
msg: Waiting for data propagation…  
until: “‘spec’ in che\_keycloak\_lookup”  
retries: 30  
delay: 10

- name: Get Che Keycloak facts  
set\_fact:  
che\_keycloak: “{{ che\_keycloak\_lookup }}”

`

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 12, 2019, 2:25am UTC](https://forum.ansible.com/t/using-set-fact-with-until/31252/16 "2019-09-12T02:25:42Z")

</div>

Great! This is the solution how to evaluate "lookup" in each iteration. Just  
put the evaluation into "until".

Thank you,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Kamesh\_Sampath1](https://avatars.discourse-cdn.com/v4/letter/k/90ced4/32.png) [@Kamesh\_Sampath1](https://forum.ansible.com/u/Kamesh_Sampath1)\
**Post date:** [September 12, 2019, 3:29am UTC](https://forum.ansible.com/t/using-set-fact-with-until/31252/17 "2019-09-12T03:29:17Z")

</div>

Thank you Vladmir and flowerysong, this solution works just as its needed.
