diff --git a/ansible_rulebook/builtin.py b/ansible_rulebook/builtin.py index e25fc706..27cbf47e 100644 --- a/ansible_rulebook/builtin.py +++ b/ansible_rulebook/builtin.py @@ -733,6 +733,7 @@ async def run_job_template( ruleset: str, name: str, organization: str, + workflow: Optional[bool] = False, job_args: Optional[dict] = None, set_facts: Optional[bool] = None, post_events: Optional[bool] = None, @@ -795,6 +796,7 @@ async def run_job_template( controller_job = await job_template_runner.run_job_template( name, organization, + workflow, job_args, ) if controller_job["status"] != "failed": diff --git a/ansible_rulebook/job_template_runner.py b/ansible_rulebook/job_template_runner.py index a0ecdca0..f64c3349 100644 --- a/ansible_rulebook/job_template_runner.py +++ b/ansible_rulebook/job_template_runner.py @@ -33,16 +33,17 @@ class JobTemplateRunner: - JOB_TEMPLATE_SLUG = "/api/v2/job_templates" CONFIG_SLUG = "/api/v2/config" JOB_COMPLETION_STATUSES = ["successful", "failed", "error", "canceled"] def __init__( - self, host: str = "", token: str = "", verify_ssl: str = "yes" + self, host: str = "", token: str = "", verify_ssl: str = "yes", + workflow: bool = False ): self.token = token self.host = host self.verify_ssl = verify_ssl + self.workflow = workflow self.refresh_delay = int( os.environ.get("EDA_JOB_TEMPLATE_REFRESH_DELAY", 10) ) @@ -89,9 +90,11 @@ def _sslcontext(self) -> Union[bool, ssl.SSLContext]: return ssl.create_default_context(cafile=self.verify_ssl) return False - async def _get_job_template_id(self, name: str, organization: str) -> int: - slug = f"{self.JOB_TEMPLATE_SLUG}/" + async def _get_job_template_id(self, name: str, workflow: bool, + organization: str) -> int: params = {"name": name} + slug = ("/api/v2/workflow_job_templates" if workflow else + "/api/v2/job_templates") while True: json_body = await self._get_page(slug, params) @@ -118,10 +121,11 @@ async def _get_job_template_id(self, name: str, organization: str) -> int: async def run_job_template( self, name: str, + workflow: bool, organization: str, job_params: dict, ) -> dict: - job = await self.launch(name, organization, job_params) + job = await self.launch(name, workflow, organization, job_params) url = job["url"] params = {} @@ -136,10 +140,12 @@ async def run_job_template( await asyncio.sleep(self.refresh_delay) async def launch( - self, name: str, organization: str, job_params: dict + self, name: str, organization: str, workflow: bool, job_params: dict ) -> dict: - jt_id = await self._get_job_template_id(name, organization) - url = urljoin(self.host, f"{self.JOB_TEMPLATE_SLUG}/{jt_id}/launch/") + jt_id = await self._get_job_template_id(name, workflow, organization) + slug = ("/api/v2/workflow_job_templates" if workflow else + "/api/v2/job_templates") + url = urljoin(self.host, f"{slug}/{jt_id}/launch/") try: async with self._session.post( diff --git a/ansible_rulebook/schema/ruleset_schema.json b/ansible_rulebook/schema/ruleset_schema.json index 6c441cd3..bf06d988 100644 --- a/ansible_rulebook/schema/ruleset_schema.json +++ b/ansible_rulebook/schema/ruleset_schema.json @@ -405,6 +405,9 @@ "organization": { "type": "string" }, + "workflow": { + "type": "boolean" + }, "job_args": { "type": "object" }, diff --git a/docs/actions.rst b/docs/actions.rst index 2e2342e7..c0bddbc8 100644 --- a/docs/actions.rst +++ b/docs/actions.rst @@ -117,6 +117,9 @@ Run a job template. * - organization - The name of the organization - Yes + * - workflow + - Launch a workflow job template instead of a normal job template. + - No * - set_facts - The artifacts from the playbook execution are inserted back into the rule set as facts - No