# Need clarification on set\_fact module

**URL:** <https://forum.ansible.com/t/need-clarification-on-set-fact-module/35383>\
**Category:** Ansible Project\
**Created:** [June 21, 2021, 5:09pm UTC](https://forum.ansible.com/t/need-clarification-on-set-fact-module/35383 "2021-06-21T17:09:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vikram\_S](https://avatars.discourse-cdn.com/v4/letter/v/90db22/32.png) [@Vikram\_S](https://forum.ansible.com/u/Vikram_S)\
**Post date:** [June 21, 2021, 5:09pm UTC](https://forum.ansible.com/t/need-clarification-on-set-fact-module/35383/1 "2021-06-21T17:09:07Z")

</div>

Ansible documentation for set\_fact module says “Set cacheable to yes to save variables across executions using a fact cache”. So my understanding is that setting ‘cacheable: yes’ would make the contents of custom variable available across differnt playbooks (as opposed to default behaviour of custom variable output being available only for current playbook).

However when i tried to use a custom variable saved in 1 playbook in another playbook, it didn’t work. So i wonder if my understanding is correct? Below is playbook:

**PLAYBOOK 1:** I define a custom variable called ‘my\_name’ and save it in fact cache using cacheable: yes

tasks:

- name: GATHER INTERFACE INFO

ios\_command:

commands: “show ip int brief”

register: show\_ip

- set\_fact:

my\_name: “{{ show\_ip }}”

cacheable: yes

**PLAYBOOK 2:** I reference earlier custom variable in 2nd playbook for comparison but doesn’t work.

tasks:

- name: GATHER INTERFACE INFO AFTER RELOAD

ios\_command:

commands: “show ip int brief”

register: show\_ip\_new

- assert:

that:

- show\_ip.stdout[0] | regex\_findall(‘up’) | length == show\_ip\_new.stdout[0] | regex\_findall(‘up’) | length

fail\_msg: “THERE IS INTERFACE MISMATCH. PLAYBOOK IS ENDING FOR CURRENT HOST.”

success\_msg: “INTERFACE STATUS MATCHED”

Thanks,  
Vikram

---

<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:** [June 21, 2021, 5:26pm UTC](https://forum.ansible.com/t/need-clarification-on-set-fact-module/35383/2 "2021-06-21T17:26:34Z")

</div>

Are these plays run against the same host?

---

<div class="post-metadata">

**Author:** ![Vikram\_S](https://avatars.discourse-cdn.com/v4/letter/v/90db22/32.png) [@Vikram\_S](https://forum.ansible.com/u/Vikram_S)\
**Post date:** [June 21, 2021, 5:46pm UTC](https://forum.ansible.com/t/need-clarification-on-set-fact-module/35383/3 "2021-06-21T17:46:51Z")

</div>

Hi,

Yes, i run them against the same host. But both these playbooks are executed separately.

Regards,  
Vikram

---

<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:** [June 21, 2021, 6:26pm UTC](https://forum.ansible.com/t/need-clarification-on-set-fact-module/35383/4 "2021-06-21T18:26:07Z")

</div>

> Ansible documentation for set\_fact module says “Set cacheable to yes to save variables across executions using a fact cache”.

It also says “This boolean converts the variable into an actual ‘fact’ which will also be added to the fact cache, if fact caching is enabled.”

[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/set\_fact\_module.html#parameter-cacheable](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/set_fact_module.html#parameter-cacheable)

So, did you enable fact caching?

---

<div class="post-metadata">

**Author:** ![Vikram\_S](https://avatars.discourse-cdn.com/v4/letter/v/90db22/32.png) [@Vikram\_S](https://forum.ansible.com/u/Vikram_S)\
**Post date:** [June 21, 2021, 6:58pm UTC](https://forum.ansible.com/t/need-clarification-on-set-fact-module/35383/5 "2021-06-21T18:58:11Z")

</div>

Hi,

Do youmean that if in addition to adding ‘cacheable: yes’ in set\_fact task, i should also be adding below config in ansible.cfg? If so, i didn’t add it but i can test.

gathering = smart  
fact\_caching = jsonfile  
fact\_caching\_connection = /tmp/facts\_cache  
fact\_caching\_timeout = 7200

Regards,  
Vikram

---

<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:** [June 21, 2021, 6:58pm UTC](https://forum.ansible.com/t/need-clarification-on-set-fact-module/35383/6 "2021-06-21T18:58:44Z")

</div>

Setting cacheable means that it will be cached if you enable it.  
Those are two different things.  
[https://docs.ansible.com/ansible/latest/plugins/cache.html#enabling-fact-cache-plugins](https://docs.ansible.com/ansible/latest/plugins/cache.html#enabling-fact-cache-plugins)
