Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add set_fact_persistent action and module. #26153

Merged
merged 4 commits into from
Aug 2, 2017
Merged

Add set_fact_persistent action and module. #26153

merged 4 commits into from
Aug 2, 2017

Conversation

alikins
Copy link
Contributor

@alikins alikins commented Jun 27, 2017

SUMMARY

Used just like set_fact, except facts set with set_fact_persistent
will be stored in the fact cache if fact caching is enabled.

set_fact only sets facts in the non_persistent_fact_cache, so they
are lost between invocations.

The implementations here are pretty much exact copies of set_fact.
The module likely needs to do that, but the action plugin could in theory
import set_fact and subclass it (all it needs is a different action name...)

ISSUE TYPE
  • Feature Pull Request
  • New Module Pull Request
  • Bugfix Pull Request
  • Docs Pull Request
COMPONENT NAME

modules/utilities/logic/set_fact.py
plugins/strategy/
plugins/action/

ANSIBLE VERSION
ansible 2.4.0 (set_fact_persistent c1e8b05ade) last updated 2017/08/02 10:00:27 (GMT -400)
  config file = /home/adrian/src/ansible/ansible.cfg
  configured module search path = [u'/home/adrian/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/adrian/src/ansible/lib/ansible
  executable location = /home/adrian/src/ansible/bin/ansible
  python version = 2.7.13 (default, May 10 2017, 20:04:28) [GCC 6.3.1 20161221 (Red Hat 6.3.1-1)]


ADDITIONAL INFORMATION

@ansibot ansibot added affects_2.4 This issue/PR affects Ansible v2.4 c:plugins/action community_review In order to be merged, this PR must follow the community review workflow. feature_pull_request module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. new_module This PR includes a new module. labels Jun 27, 2017
@ansibot
Copy link
Contributor

ansibot commented Jun 27, 2017

The test ansible-test sanity --test validate-modules failed with the following error:

lib/ansible/modules/utilities/logic/set_fact_persistent.py:0:0: E319 RETURN.ansible_facts.contains: required key not provided @ data['contains']. Got None

click here for bot help

@ansibot ansibot added ci_verified Changes made in this PR are causing tests to fail. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed community_review In order to be merged, this PR must follow the community review workflow. labels Jun 27, 2017
@bcoca
Copy link
Member

bcoca commented Jun 27, 2017

why not add a cacheable property to set_facts

@alikins
Copy link
Contributor Author

alikins commented Jun 27, 2017

why not add a cacheable property to set_facts

Mainly because this approach doesn't require changes to plugins/strategy/*
(strategies could check for a property/attr on the action instead of the explicit check for 'action == "set_fact"' currently used in strategy)

@bcoca
Copy link
Member

bcoca commented Jun 27, 2017

using a 'cacheable' flag would be more flexible than the current 'set_fact' match, other modules could create non persistent/persistent facts

@alikins
Copy link
Contributor Author

alikins commented Jun 27, 2017

@bcoca can add a cacheable Attribute to Task, and set it from set_fact.py (or normal.py) and check it in strategy

set_fact module assumes all params passed to it are key=value pairs however, so adding a 'cacheable' arg to the module could cause conflicts (seemingly even 'args').

If we avoid the potential name conflict and dont add a module arg for skip_facts, then it will have to default to false and we need another module that could take a 'cacheable' arg. But at the moment, all other modules are already cacheable, but would still need a set_fact_persistent that enables cacheable and set_fact.

Or we could make 'cacheable' a prohibited fact name to use with set_fact...

@bcoca
Copy link
Member

bcoca commented Jun 27, 2017

anyone setting 'args' as a var is already creating issues for themselves, other 'raw' parameter modules allow options (see creates= in shell/command) so it would not be a first.

my worry about introducing a new module is also that it introduces a new precedence step, while introducing a toggle between the 2 types of facts does not.

@alikins alikins closed this Jun 28, 2017
@alikins alikins reopened this Jun 28, 2017
@ansibot ansibot removed the ci_verified Changes made in this PR are causing tests to fail. label Jun 28, 2017
@ansibot
Copy link
Contributor

ansibot commented Jun 28, 2017

@ansibot ansibot added c:playbook/task c:plugins/strategy and removed needs_triage Needs a first human triage before being processed. new_module This PR includes a new module. labels Jun 28, 2017
@alikins
Copy link
Contributor Author

alikins commented Jun 28, 2017

Updated.

Not sure if the behavior on regular set_fact is correct. Logic was updated so that set_fact always sets non_persistent_facts and if cacheable, also sets host_facts.

That seems to do the right thing, but not sure about the details of the precedence.

Without the change, the behavior when using caching and non-caching set_fact on the same fact name is a little odd. A fact set non caching, and then later set with caching, the non caching value wins because of the higher precedence of non_persistent facts. Adding to non persistent facts and host_facts seems to DWIM there, but unsure of consequences elsewhere.

@bcoca
Copy link
Member

bcoca commented Jun 28, 2017

its odd, but it is documented, set_fact wins over 'actual facts' .. the real difference being cacheable/non-cacheable.

I don't think this works well as task keyword as most tasks don't return facts, only module that return 'ansible_facts' do.

@ansibot ansibot added core_review In order to be merged, this PR must follow the core review workflow. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jun 28, 2017
@alikins
Copy link
Contributor Author

alikins commented Jun 28, 2017

using a 'cacheable' flag would be more flexible than the current 'set_fact' match, other modules could create non persistent/persistent facts

I don't think this works well as task keyword as most tasks don't return facts, only module that return 'ansible_facts' do.

So, just a '_ansible_fact_cacheable' in module/action return and checked by strategy?

@ansibot ansibot added the support:core This issue/PR relates to code supported by the Ansible Engineering Team. label Jun 28, 2017
@jmighion
Copy link
Member

jmighion commented Jul 6, 2017

I like this approach. It would allow me to cache facts that are important to me, but probably not important enough to make upstream changes to a specific fact module and eliminate maintaining local changes to modules over the course of Ansible releases.

@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Jul 18, 2017
@isopy
Copy link
Contributor

isopy commented Aug 1, 2017

Definitely gotta +1 a cacheable option for set_facts.

@bpleines
Copy link
Contributor

bpleines commented Aug 1, 2017

+1

1 similar comment
@harrytruman
Copy link

+1

@alikins alikins added this to the 2.4.0 milestone Aug 2, 2017
Used just like set_fact, except facts set with cacheable: true
will be stored in the fact cache if fact caching is enabled.

set_fact normally only sets facts in the non_persistent_fact_cache, so they
are lost between invocations.
We dont want to use 'ansible_facts_cacheable' result item
or 'cacheable' arg as actual facts, so pop them out of the
dicts.
@ansibot ansibot removed the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Aug 2, 2017
@alikins alikins merged commit 6fbd0a8 into ansible:devel Aug 2, 2017
schunduri pushed a commit to schunduri/ansible that referenced this pull request Aug 4, 2017
* Add 'cacheable' param to  set_fact action and module.

Used just like set_fact, except facts set with cacheable: true
will be stored in the fact cache if fact caching is enabled.

set_fact normally only sets facts in the non_persistent_fact_cache, so they
are lost between invocations.

* update set_facts docs

* use 'ansible_facts_cacheable' in module/actions result

* pop fact cacheable related items out of args/results

We dont want to use 'ansible_facts_cacheable' result item
or 'cacheable' arg as actual facts, so pop them out of the
dicts.
@@ -511,12 +511,14 @@ def parent_handler_match(target_handler, handler_name):
for target_host in host_list:
self._variable_manager.set_host_variable(target_host, var_name, var_value)
else:
cacheable = result_item.pop('ansible_facts_cacheable', True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mess around in strategy often so I don't know if this is normal but,, why is the default here True but False everywhere else?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because set_fact is only module in which this was not true, all other 'ansible_facts' should be cacheable by default.

@ansibot ansibot added feature This issue/PR relates to a feature request. and removed feature_pull_request labels Mar 5, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.4 This issue/PR affects Ansible v2.4 c:playbook/task c:plugins/action c:plugins/strategy core_review In order to be merged, this PR must follow the core review workflow. feature This issue/PR relates to a feature request. module This issue/PR relates to a module. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants