# \[feat req\] Improving set\_fact scoping and lifetime

**URL:** <https://forum.ansible.com/t/feat-req-improving-set-fact-scoping-and-lifetime/22065>\
**Category:** Ansible Developer\
**Created:** [July 20, 2016, 7:01pm UTC](https://forum.ansible.com/t/feat-req-improving-set-fact-scoping-and-lifetime/22065 "2016-07-20T19:01:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Danny\_Kimsey](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@Danny\_Kimsey](https://forum.ansible.com/u/Danny_Kimsey)\
**Post date:** [July 20, 2016, 7:01pm UTC](https://forum.ansible.com/t/feat-req-improving-set-fact-scoping-and-lifetime/22065/1 "2016-07-20T19:01:04Z")

</div>

tl;dr Add a `scope` parameter to `set_fact` module, improving usability.

The current behavior of `set_fact` is to create a run-time only variable, in 2.x with `private_role_vars` this variable is scoped(\*) to the role it is defined within.

It is helpful to be able to specify global facts in a few circumstances, such as passing information between roles or [implementing once-per-playbook roles](https://groups.google.com/d/topic/ansible-project/erqBH5SmPUo/discussion).

What I’d like to see is the `set_fact` module extended to support a `scope` parameter:

- playbook
- runtime lifetime.
- 1.x like behavior.
- 2.x behavior when `private_role_vars` is not enabled.
- role
- runtime lifetime.
- 2.x behavior when `private_role_vars` is enabled.
- corollary: should roles maintain state (set\_fact’ed variables loaded back up) within the same playbook?

Additionally, the process for creating persistent facts in a role is a wee bit clunky (write out a json blob in facts.d, and force a potentially expensive fact reload).

So, a `lifetime` parameter:

- runtime
- default behavior
- persistent
- record fact to host’s facts.d, erroring if unable to do so.
- add fact to host’s in-memory ansible\_local facts.
- optimization: does not trigger a fact reload.

(\*) “Scoped” might be the wrong word here as the variable is deleted when the role ends. If the role is executed later in the playbook, the variable will not be defined.
