-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scaffold action plugin through add subcommand (#348)
* Scaffold action plugin through add subcommand * Make seperate functions to collect and store resources based on plugin_type * Changes for adding module path * Fix logic for module scaffolding as part of adding action plugin * Chages in the module template to fix the error: DOCUMENTATION.module- not a valid value for dictionary value quit exit * fix the arg spec validation related errors in plugin template * add a function to update galaxy dependency for action plugin * logic cleanup * dependency key update and add checks for it * move the update_galaxy_dependency func back to add.py and initial tests for action plugin * test update_galaxy_dependency function * correct the debug message * author name change in the module documentation
- Loading branch information
1 parent
c38f225
commit 7ca0789
Showing
9 changed files
with
486 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ addopts | |
antsibull | ||
argcomplete | ||
argnames | ||
argspec | ||
argvalues | ||
capsys | ||
chakarborty | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/ansible_creator/resources/collection_project/plugins/action/hello_world.py.j2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
{# action_plugin_template.j2 #} | ||
{%- set action_name = plugin_name | default("hello_world") -%} | ||
{%- set author = author | default("Your Name") -%} | ||
{%- set description = description | default("A custom action plugin for Ansible.") -%} | ||
{%- set license = license | default("GPL-3.0-or-later") -%} | ||
# {{ action_name }}.py - {{ description }} | ||
# Author: {{ author }} | ||
# License: {{ license }} | ||
# pylint: disable=E0401 | ||
|
||
from __future__ import absolute_import, annotations, division, print_function | ||
|
||
__metaclass__ = type # pylint: disable=C0103 | ||
|
||
from typing import TYPE_CHECKING | ||
from ansible_collections.ansible.utils.plugins.module_utils.common.argspec_validate import ( # type: ignore | ||
AnsibleArgSpecValidator, | ||
) | ||
from ansible_collections.ansible.utils.plugins.modules.fact_diff import DOCUMENTATION # type: ignore | ||
from ansible.plugins.action import ActionBase # type: ignore | ||
|
||
|
||
if TYPE_CHECKING: | ||
from typing import Optional, Dict, Any | ||
|
||
|
||
class ActionModule(ActionBase): # type: ignore[misc] | ||
""" | ||
Custom Ansible action plugin: {{ action_name }} | ||
A custom action plugin for Ansible. | ||
""" | ||
|
||
def _check_argspec(self, result: dict[str, Any]) -> None: | ||
aav = AnsibleArgSpecValidator( | ||
data=self._task.args, | ||
schema=DOCUMENTATION, | ||
schema_format="doc", | ||
name=self._task.action, | ||
) | ||
valid, errors, self._task.args = aav.validate() | ||
if not valid: | ||
result["failed"] = True | ||
result["msg"] = errors | ||
|
||
def run( | ||
self, | ||
tmp: Optional[str] = None, | ||
task_vars: Optional[Dict[str, Any]] = None, | ||
) -> Dict[str, Any]: | ||
""" | ||
Executes the action plugin. | ||
|
||
Args: | ||
tmp: Temporary path provided by Ansible for the module execution. Defaults to None. | ||
task_vars: Dictionary of task variables available to the plugin. Defaults to None. | ||
|
||
Returns: | ||
dict: Result of the action plugin execution. | ||
""" | ||
# Get the task arguments | ||
if task_vars is None: | ||
task_vars = {} | ||
result = {} | ||
warnings: list[str] = [] | ||
|
||
# Example processing logic - Replace this with actual action code | ||
result = super(ActionModule, self).run(tmp, task_vars) | ||
self._check_argspec(result) | ||
|
||
# Copy the task arguments | ||
module_args = self._task.args.copy() | ||
|
||
prefix = module_args.get("prefix", "DefaultPrefix") | ||
message = module_args.get("msg", "No message provided") | ||
module_args["msg"] = f"{prefix}: {message}" | ||
|
||
result.update( | ||
self._execute_module( | ||
module_name="debug", | ||
module_args=module_args, | ||
task_vars=task_vars, | ||
tmp=tmp, | ||
), | ||
) | ||
|
||
if warnings: | ||
if "warnings" in result: | ||
result["warnings"].extend(warnings) | ||
else: | ||
result["warnings"] = warnings | ||
return result |
37 changes: 37 additions & 0 deletions
37
src/ansible_creator/resources/collection_project/plugins/modules/hello_world.py.j2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{%- set module_name = plugin_name | default("hello_world") -%} | ||
{%- set author = author | default("Your Name (@username)") -%} | ||
# {{ module_name }}.py | ||
# GNU General Public License v3.0+ | ||
|
||
DOCUMENTATION = """ | ||
module: {{ module_name }} | ||
author: {{ author }} | ||
version_added: "1.0.0" | ||
short_description: A custom action plugin for Ansible. | ||
description: | ||
- This is a custom action plugin to provide action functionality. | ||
options: | ||
prefix: | ||
description: | ||
- A string that is added as a prefix to the message passed to the module. | ||
type: str | ||
msg: | ||
description: The message to display in the output. | ||
type: str | ||
with_prefix: | ||
description: | ||
- A boolean flag indicating whether to include the prefix in the message. | ||
type: bool | ||
notes: | ||
- This is a scaffold template. Customize the plugin to fit your needs. | ||
""" | ||
|
||
EXAMPLES = """ | ||
- name: Example Action Plugin | ||
hosts: localhost | ||
tasks: | ||
- name: Example {{ module_name }} plugin | ||
with_prefix: | ||
prefix: "Hello, World" | ||
msg: "Ansible!" | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.