Skip to content

Commit

Permalink
Remove facts and fact from extra_vars (#345)
Browse files Browse the repository at this point in the history
Also add ruleset and rule to extra_vars

Resolves AAP-8787
  • Loading branch information
bzwei authored Feb 17, 2023
2 parents f0c7e58 + b68a758 commit 47f6a06
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 17 deletions.
4 changes: 0 additions & 4 deletions ansible_rulebook/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,9 +834,5 @@ def _collect_extra_vars(
eda_vars["events"] = variables["events"]
if "event" in variables:
eda_vars["event"] = variables["event"]
if "facts" in variables:
eda_vars["facts"] = variables["events"]
if "fact" in variables:
eda_vars["fact"] = variables["event"]
extra_vars[KEY_EDA_VARS] = eda_vars
return extra_vars
2 changes: 0 additions & 2 deletions ansible_rulebook/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,14 +385,12 @@ async def _call_action(
variables_copy = variables.copy()
if single_match is not None:
variables_copy["event"] = single_match
variables_copy["fact"] = single_match
event = single_match
if "meta" in event:
if "hosts" in event["meta"]:
hosts = parse_hosts(event["meta"]["hosts"])
else:
variables_copy["events"] = multi_match
variables_copy["facts"] = multi_match
new_hosts = []
for event in variables_copy["events"].values():
if "meta" in event:
Expand Down
4 changes: 2 additions & 2 deletions docs/events_and_facts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ Instead, the ``all`` operator must be used:
.. note::
To use facts you may use either ``event`` or ``facts`` keys interchangeably.
To use facts you may use either ``events`` or ``facts`` keys interchangeably.


You can combine `set_fact <actions.html#set-fact>`_ and `retract_fact <actions.html#retract-fact>`_ actions to manage the global state during the lifecycle of your rulebook.

The text above describes how to use ``events`` or ``facts`` in a rulebook. A single matched ``event`` or multiple matched ``events`` are also
sent to a playbook through extra_vars under namespace ``ansible_eda`` when a run_playbook or run_job_template action is executed. So in a playbook
you should reference them as ``ansible_eda.event`` or ``ansible_eda.events``.
you should reference them as ``ansible_eda.event`` or ``ansible_eda.events``. Facts are not sent to playbooks.
8 changes: 4 additions & 4 deletions tests/asts/test_set_facts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
name: playbooks/validate_args_playbook.yml
extra_vars:
my_vars:
fact.alpha: 1
fact.beta.location: "{{ Naboo }}"
fact.beta.name: R2D2
fact.msg: hello world
event.alpha: 1
event.beta.location: "{{ Naboo }}"
event.beta.name: R2D2
event.msg: hello world
condition:
AllCondition:
- EqualsExpression:
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_actions_sanity():
'ruleset': 'Test actions sanity',
'source_rule_name': 'debug',
'source_ruleset_name': 'Test actions sanity',
'variables': {'event': {'action': 'debug'}, 'fact': {'action': 'debug'}}}""" # noqa: E501
'variables': {'event': {'action': 'debug'}}}""" # noqa: E501

assert event_debug_expected_output in result.stdout, "debug action failed"

Expand Down
8 changes: 4 additions & 4 deletions tests/rules/test_set_facts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
name: playbooks/validate_args_playbook.yml
extra_vars:
my_vars:
fact.msg: hello world
fact.alpha: 1
fact.beta.name: R2D2
fact.beta.location: "{{ Naboo }}"
event.msg: hello world
event.alpha: 1
event.beta.name: R2D2
event.beta.location: "{{ Naboo }}"
copy_files: True

0 comments on commit 47f6a06

Please sign in to comment.