# action\_plugins and ansible\_facts

**URL:** <https://forum.ansible.com/t/action-plugins-and-ansible-facts/35948>\
**Category:** Ansible Project\
**Created:** [November 2, 2021, 1:59pm UTC](https://forum.ansible.com/t/action-plugins-and-ansible-facts/35948 "2021-11-02T13:59:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Ratliff](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/john_ratliff/32/3007_2.png) [@John\_Ratliff](https://forum.ansible.com/u/John_Ratliff)\
**Post date:** [November 2, 2021, 1:59pm UTC](https://forum.ansible.com/t/action-plugins-and-ansible-facts/35948/1 "2021-11-02T13:59:58Z")

</div>

I’m using action plugins to collect data from an internal API. We’ve been setting the “ansible\_facts” key of the results dictionary to return values. Most of the time, this seems to work as if we had called set\_fact. But if we use set\_fact on the same variable, ansible\_facts doesn’t overwrite the value. Is this expected behavior?

[https://pastebin.com/553q7PNH](https://pastebin.com/553q7PNH) – playbook example  
[https://pastebin.com/aeAWyZhp](https://pastebin.com/aeAWyZhp) – set\_contact\_id.py action\_plugin  
[https://pastebin.com/zrh2MLKZ](https://pastebin.com/zrh2MLKZ) - ansible results

From the results, you can see that the contact\_id is initially unset, so the display is 0.  
Then the action\_plugin uses ansible\_facts to set it to 15731, which is displayed.  
Then I call set\_fact, which changes the value to 22124.  
Finally, I try to update the value by calling the action\_plugin again. This time, I see ansible\_facts has the contact\_id value set to 11131, but when I display it, I still see 22124. The ansible\_facts from the action plugin did not overwrite the value. Is this expected behavior, or am I doing something wrong?

Thanks.

–John

---

<div class="post-metadata">

**Author:** ![John\_Ratliff](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/john_ratliff/32/3007_2.png) [@John\_Ratliff](https://forum.ansible.com/u/John_Ratliff)\
**Post date:** [November 2, 2021, 2:39pm UTC](https://forum.ansible.com/t/action-plugins-and-ansible-facts/35948/2 "2021-11-02T14:39:15Z")

</div>

I feel like this has to be a bug of some kind. I can use the exact code for the set\_fact action plugin and I get no difference in the results. It certainly looks like I am doing exactly what ansible is doing internally, but getting different results.

–John

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [November 2, 2021, 2:46pm UTC](https://forum.ansible.com/t/action-plugins-and-ansible-facts/35948/3 "2021-11-02T14:46:44Z")

</div>

The documentation covers this:

[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#variable-precedence-where-should-i-put-a-variable](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable)

1. `ansible_facts` are `host facts` which are #11
2. `set_fact` (without cacheable=yes) is #19

So `set_fact` via default use always wins.

---

<div class="post-metadata">

**Author:** ![John\_Ratliff](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/john_ratliff/32/3007_2.png) [@John\_Ratliff](https://forum.ansible.com/u/John_Ratliff)\
**Post date:** [November 2, 2021, 2:59pm UTC](https://forum.ansible.com/t/action-plugins-and-ansible-facts/35948/4 "2021-11-02T14:59:03Z")

</div>

Okay. I didn’t put together that “ansible\_facts” are host facts. Thanks.

–John
