Skip to content

Commit 414d576

Browse files
committed
Scaffold action plugin through add subcommand
1 parent bafa0dc commit 414d576

File tree

4 files changed

+176
-5
lines changed

4 files changed

+176
-5
lines changed

src/ansible_creator/arg_parser.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@
3434

3535
MIN_COLLECTION_NAME_LEN = 2
3636

37-
COMING_SOON = (
38-
"add resource role",
39-
"add plugin action",
40-
)
37+
COMING_SOON = ("add resource role",)
4138

4239

4340
class Parser:
@@ -369,6 +366,7 @@ def _add_plugin_action(self, subparser: SubParser[ArgumentParser]) -> None:
369366
formatter_class=CustomHelpFormatter,
370367
)
371368
self._add_args_common(parser)
369+
self._add_overwrite(parser)
372370
self._add_args_plugin_common(parser)
373371

374372
def _add_plugin_filter(self, subparser: SubParser[ArgumentParser]) -> None:
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
{# action_plugin_template.j2 #}
2+
{%- set action_name = plugin_name | default("hello_world") -%}
3+
{%- set author = author | default("Your Name") -%}
4+
{%- set description = description | default("A custom action plugin for Ansible.") -%}
5+
{%- set license = license | default("GPL-3.0-or-later") -%}
6+
# {{ action_name }}.py - {{ description }}
7+
# Author: {{ author }}
8+
# License: {{ license }}
9+
# pylint: disable=E0401
10+
11+
from __future__ import absolute_import, annotations, division, print_function
12+
13+
__metaclass__ = type # pylint: disable=C0103
14+
15+
from typing import TYPE_CHECKING
16+
from ansible.plugins.action import ActionBase # type: ignore
17+
18+
19+
if TYPE_CHECKING:
20+
from typing import Optional, Dict, Any
21+
22+
23+
DOCUMENTATION = """
24+
name: {{ action_name }}
25+
author: {{ author }}
26+
version_added: "1.0.0"
27+
short_description: {{ description }}
28+
description:
29+
- This is a custom action plugin to provide action functionality.
30+
notes:
31+
- This is a scaffold template. Customize the plugin to fit your needs.
32+
"""
33+
34+
EXAMPLES = """
35+
- name: Example Action Plugin
36+
hosts: localhost
37+
tasks:
38+
- name: Example {{ action_name }} plugin
39+
with_prefix:
40+
prefix: "Hello, World"
41+
msg: "Ansible!"
42+
"""
43+
44+
45+
class ActionModule(ActionBase): # type: ignore[misc]
46+
"""
47+
Custom Ansible action plugin: {{ action_name }}
48+
A custom action plugin for Ansible.
49+
"""
50+
51+
def run(
52+
self,
53+
tmp: Optional[str] = None,
54+
task_vars: Optional[Dict[str, Any]] = None,
55+
) -> Dict[str, Any]:
56+
"""
57+
Executes the action plugin.
58+
59+
Args:
60+
tmp: Temporary path provided by Ansible for the module execution. Defaults to None.
61+
task_vars: Dictionary of task variables available to the plugin. Defaults to None.
62+
63+
Returns:
64+
dict: Result of the action plugin execution.
65+
"""
66+
# Get the task arguments
67+
if task_vars is None:
68+
task_vars = {}
69+
result = {}
70+
warnings: list[str] = []
71+
72+
# Example processing logic - Replace this with actual action code
73+
result = super(ActionModule, self).run(tmp, task_vars)
74+
module_args = self._task.args.copy()
75+
result.update(
76+
self._execute_module(
77+
module_name="debug",
78+
module_args=module_args,
79+
task_vars=task_vars,
80+
tmp=tmp,
81+
),
82+
)
83+
84+
if warnings:
85+
if "warnings" in result:
86+
result["warnings"].extend(warnings)
87+
else:
88+
result["warnings"] = warnings
89+
return result

src/ansible_creator/subcommands/add.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def _plugin_scaffold(self, plugin_path: Path) -> None:
174174
self.output.debug(f"Started copying {self._project} plugin to destination")
175175

176176
# Call the appropriate scaffolding function based on the plugin type
177-
if self._plugin_type in ("lookup", "filter"):
177+
if self._plugin_type in ("action", "filter", "lookup"):
178178
template_data = self._get_plugin_template_data()
179179

180180
else:
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# hello_world.py - A custom action plugin for Ansible.
2+
# Author: Your Name
3+
# License: GPL-3.0-or-later
4+
# pylint: disable=E0401
5+
6+
from __future__ import absolute_import, annotations, division, print_function
7+
8+
__metaclass__ = type # pylint: disable=C0103
9+
10+
from typing import TYPE_CHECKING
11+
from ansible.plugins.action import ActionBase # type: ignore
12+
13+
14+
if TYPE_CHECKING:
15+
from typing import Optional, Dict, Any
16+
17+
18+
DOCUMENTATION = """
19+
name: hello_world
20+
author: Your Name
21+
version_added: "1.0.0"
22+
short_description: A custom action plugin for Ansible.
23+
description:
24+
- This is a custom action plugin to provide action functionality.
25+
notes:
26+
- This is a scaffold template. Customize the plugin to fit your needs.
27+
"""
28+
29+
EXAMPLES = """
30+
- name: Example Action Plugin
31+
hosts: localhost
32+
tasks:
33+
- name: Example hello_world plugin
34+
with_prefix:
35+
prefix: "Hello, World"
36+
msg: "Ansible!"
37+
"""
38+
39+
40+
class ActionModule(ActionBase): # type: ignore[misc]
41+
"""
42+
Custom Ansible action plugin: hello_world
43+
A custom action plugin for Ansible.
44+
"""
45+
46+
def run(
47+
self,
48+
tmp: Optional[str] = None,
49+
task_vars: Optional[Dict[str, Any]] = None,
50+
) -> Dict[str, Any]:
51+
"""
52+
Executes the action plugin.
53+
54+
Args:
55+
tmp: Temporary path provided by Ansible for the module execution. Defaults to None.
56+
task_vars: Dictionary of task variables available to the plugin. Defaults to None.
57+
58+
Returns:
59+
dict: Result of the action plugin execution.
60+
"""
61+
# Get the task arguments
62+
if task_vars is None:
63+
task_vars = {}
64+
result = {}
65+
warnings: list[str] = []
66+
67+
# Example processing logic - Replace this with actual action code
68+
result = super(ActionModule, self).run(tmp, task_vars)
69+
module_args = self._task.args.copy()
70+
result.update(
71+
self._execute_module(
72+
module_name="debug",
73+
module_args=module_args,
74+
task_vars=task_vars,
75+
tmp=tmp,
76+
),
77+
)
78+
79+
if warnings:
80+
if "warnings" in result:
81+
result["warnings"].extend(warnings)
82+
else:
83+
result["warnings"] = warnings
84+
return result

0 commit comments

Comments
 (0)