# Trying to Store set fact variable based on when condtion

**URL:** <https://forum.ansible.com/t/trying-to-store-set-fact-variable-based-on-when-condtion/33700>\
**Category:** Ansible Project\
**Created:** [August 29, 2020, 11:06am UTC](https://forum.ansible.com/t/trying-to-store-set-fact-variable-based-on-when-condtion/33700 "2020-08-29T11:06:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Praveen\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@Praveen\_Kumar1](https://forum.ansible.com/u/Praveen_Kumar1)\
**Post date:** [August 29, 2020, 11:06am UTC](https://forum.ansible.com/t/trying-to-store-set-fact-variable-based-on-when-condtion/33700/1 "2020-08-29T11:06:45Z")

</div>

Hi,

Am writing a logic to get the list of servers running more than n number of day’s. I tried to store the result in a set\_fact variable but it’s not storing the values properly mean it including the skipped values in the set\_facts. How to fix this?

- name: Compare the times  
set\_fact:  
temp: “{{ ec2.instances | map(attribute=‘instance\_id’) | list }}”  
when: ((ansible\_date\_time.iso8601[:19] | to\_datetime(fmt) - item.launch\_time[:19] | to\_datetime(fmt)).days \>= 1)  
with\_items: “{{ ec2.instances }}”  
vars:  
fmt: “%Y-%m-%dT%H:%M:%S”  
register: timestamp1  
loop\_control:  
label: “{{ item.instance\_id }}”

Below is the ouput:

`TASK [Compare the times] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [localhost] =\> (item=Prod-scoring-ami-machine)  
ok: [localhost] =\> (item=stage-scoring-202007241050087142)  
ok: [localhost] =\> (item=stage-scoring-202007241051329817)  
ok: [localhost] =\> (item=stage-scoring-202007241129464655)  
ok: [localhost] =\> (item=stage-scoring-202007241123295962)  
ok: [localhost] =\> (item=stage-scoring-202008261826362494)  
skipping: [localhost] =\> (item=prod-scoring-202008282138514763)  
skipping: [localhost] =\> (item=prod-scoring-202008282300303548)

TASK [Debug the output] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: [  
“Prod-scoring-ami-machine”,  
“stage-scoring-202007241050087142”,  
“stage-scoring-202007241051329817”,  
“stage-scoring-202007241129464655”,  
“stage-scoring-202007241123295962”,  
“stage-scoring-202008261826362494”,  
“prod-scoring-202008282138514763”,  
“prod-scoring-202008282300303548”  
]  
}

Expected output:

TASK [Compare the times] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [localhost] =\> (item=Prod-scoring-ami-machine)  
ok: [localhost] =\> (item=stage-scoring-202007241050087142)  
ok: [localhost] =\> (item=stage-scoring-202007241051329817)  
ok: [localhost] =\> (item=stage-scoring-202007241129464655)  
ok: [localhost] =\> (item=stage-scoring-202007241123295962)  
ok: [localhost] =\> (item=stage-scoring-202008261826362494)  
skipping: [localhost] =\> (item=prod-scoring-202008282138514763)  
skipping: [localhost] =\> (item=prod-scoring-202008282300303548)

TASK [Debug the output] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: [  
“stage-scoring-202007241050087142”,  
“stage-scoring-202007241051329817”,  
“stage-scoring-202007241129464655”,  
“stage-scoring-202007241123295962”,  
“stage-scoring-202008261826362494”,  
]  
}

The above code returns all the values rather than condition met values. i mean it includes the skipped values. How to get only matched or task is ok values.

Any ideas much appreciated.

Thanks !

---

<div class="post-metadata">

**Author:** ![Praveen\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@Praveen\_Kumar1](https://forum.ansible.com/u/Praveen_Kumar1)\
**Post date:** [August 29, 2020, 11:09am UTC](https://forum.ansible.com/t/trying-to-store-set-fact-variable-based-on-when-condtion/33700/2 "2020-08-29T11:09:16Z")

</div>

Note: Security reasons i have replaced instance Id’s with names.
