Need clarification on set_fact module

186 views
Skip to first unread message

Vikram S

unread,
Jun 21, 2021, 1:09:08 PM6/21/21
to Ansible Project

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

Dick Visser

unread,
Jun 21, 2021, 1:27:01 PM6/21/21
to ansible...@googlegroups.com
Are these plays run against the same host?

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/14fc7b95-754f-4ca4-a85f-a7ff1978b79an%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Vikram S

unread,
Jun 21, 2021, 1:46:51 PM6/21/21
to Ansible Project
Hi,

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

Regards,
Vikram

Dick Visser

unread,
Jun 21, 2021, 2:26:30 PM6/21/21
to ansible...@googlegroups.com
On Mon, 21 Jun 2021 at 19:09, Vikram S <vikrams...@gmail.com> wrote:

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."

So, did you enable fact caching?



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/14fc7b95-754f-4ca4-a85f-a7ff1978b79an%40googlegroups.com.
Message has been deleted

Vikram S

unread,
Jun 21, 2021, 2:58:11 PM6/21/21
to Ansible Project
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

Dick Visser

unread,
Jun 21, 2021, 2:59:11 PM6/21/21
to ansible...@googlegroups.com
On Mon, 21 Jun 2021 at 20:54, Vikram S <vikrams...@gmail.com> wrote:
>
> Hi,
>
> Yes, i did. As i have given in above script, below command is added to the task.
> cacheable: yes


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


--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT
Reply all
Reply to author
Forward
0 new messages