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

fix: aap-17232 ensure job_args.limit for awx actions #605

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
### Added

### Fixed
- Job_template and workflow_template actions honor custom hosts limit

### Removed

Expand Down
7 changes: 5 additions & 2 deletions ansible_rulebook/action/run_job_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
JobTemplateNotFoundException,
)
from ansible_rulebook.job_template_runner import job_template_runner
from ansible_rulebook.util import run_at
from ansible_rulebook.util import process_controller_host_limit, run_at

from .control import Control
from .helper import Helper
Expand All @@ -46,7 +46,10 @@ def __init__(self, metadata: Metadata, control: Control, **action_args):
self.organization = self.action_args["organization"]
self.job_id = str(uuid.uuid4())
self.job_args = self.action_args.get("job_args", {})
self.job_args["limit"] = ",".join(self.helper.control.hosts)
self.job_args["limit"] = process_controller_host_limit(
self.job_args,
self.helper.control.hosts,
)
self.controller_job = {}

async def __call__(self):
Expand Down
7 changes: 5 additions & 2 deletions ansible_rulebook/action/run_workflow_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
WorkflowJobTemplateNotFoundException,
)
from ansible_rulebook.job_template_runner import job_template_runner
from ansible_rulebook.util import run_at
from ansible_rulebook.util import process_controller_host_limit, run_at

from .control import Control
from .helper import Helper
Expand All @@ -46,7 +46,10 @@ def __init__(self, metadata: Metadata, control: Control, **action_args):
self.organization = self.action_args["organization"]
self.job_id = str(uuid.uuid4())
self.job_args = self.action_args.get("job_args", {})
self.job_args["limit"] = ",".join(self.helper.control.hosts)
self.job_args["limit"] = process_controller_host_limit(
self.job_args,
self.helper.control.hosts,
)
self.controller_job = {}

async def __call__(self):
Expand Down
13 changes: 13 additions & 0 deletions ansible_rulebook/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,16 @@ def _builtin_filter_path(name: str) -> Tuple[bool, str]:
dirname = os.path.dirname(os.path.realpath(__file__))
path = os.path.join(dirname, "event_filter", filter_name + ".py")
return os.path.exists(path), path


# TODO(alex): This function should be removed after the
# controller templates are refactored to deduplicate code
def process_controller_host_limit(
job_args: dict,
parent_hosts: list[str],
) -> str:
if "limit" in job_args:
if isinstance(job_args["limit"], list):
return ",".join(job_args["limit"])
return str(job_args["limit"])
return ",".join(parent_hosts)
28 changes: 28 additions & 0 deletions tests/unit/action/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import asyncio

import pytest

from ansible_rulebook.action.control import Control
from ansible_rulebook.action.metadata import Metadata


@pytest.fixture
def base_metadata():
return Metadata(
rule="r1",
rule_set="rs1",
rule_uuid="u1",
rule_set_uuid="u2",
rule_run_at="abc",
)


@pytest.fixture
def base_control():
return Control(
queue=asyncio.Queue(),
inventory="abc",
hosts=["all"],
variables={"a": 1},
project_data_file="",
)
46 changes: 46 additions & 0 deletions tests/unit/action/test_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pytest

from ansible_rulebook.action.run_job_template import RunJobTemplate
from ansible_rulebook.action.run_workflow_template import RunWorkflowTemplate


@pytest.mark.parametrize(
"template_class",
[
pytest.param(RunJobTemplate, id="job_template"),
pytest.param(RunWorkflowTemplate, id="workflow_template"),
],
)
@pytest.mark.parametrize(
"input,expected",
[
pytest.param({"limit": "localhost"}, "localhost", id="single_host"),
pytest.param(
{"limit": "localhost,localhost2"},
"localhost,localhost2",
id="multiple_hosts_str",
),
pytest.param(
{"limit": ["localhost", "localhost2"]},
"localhost,localhost2",
id="multiple_hosts",
),
pytest.param({}, "all", id="default"),
],
)
@pytest.mark.asyncio
async def test_controller_custom_host_limit(
input, expected, template_class, base_metadata, base_control
):
"""Test controller templates process the host limit in job_args."""
action_args = {
"name": "fred",
"organization": "Default",
"retries": 1,
"retry": True,
"delay": 1,
"set_facts": True,
"job_args": input,
}
template = template_class(base_metadata, base_control, **action_args)
assert template.job_args["limit"] == expected