action_plugins and ansible_facts

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 – playbook example
https://pastebin.com/aeAWyZhp – set_contact_id.py action_plugin
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

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

The documentation covers this:

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.

Okay. I didn’t put together that “ansible_facts” are host facts. Thanks.

–John