-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scaffold a generic module plugin in an existing ansible collection us…
…ing add subcommand. (#365) * Scaffold generic module through add subcommand * Removed add_resource_module function * chore: auto fixes from pre-commit.com hooks * Changed Module dir to Sample_Module AND Updated test_add.py * chore: auto fixes from pre-commit.com hooks * updated test_add.py * Added sample_module dir under test/fixtures * updated hello_world.py under tests/fixtures/plugins/sample_module * updated test_add.py * Updated test_add.py and arg_parser.py * Updated docs * changed launch.json * chore: auto fixes from pre-commit.com hooks * Reverted launch.json changes * Updated docs * Fixed docs * Fixed docs, args_parser.py and add.py * chore: auto fixes from pre-commit.com hooks * Updated add.py * Update add.py * Reverted changes in add.py --------- Co-authored-by: Shashank Venkat <shvenkat> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
edd5012
commit f8b098d
Showing
8 changed files
with
256 additions
and
0 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
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
Empty file.
65 changes: 65 additions & 0 deletions
65
src/ansible_creator/resources/collection_project/plugins/sample_module/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,65 @@ | ||
{# module_plugin_template.j2 #} | ||
{%- set module_name = plugin_name | default("hello_world") -%} | ||
{%- set author = author | default("Your Name (@username)") -%} | ||
{%- set description = description | default("A custom module plugin for Ansible.") -%} | ||
{%- set license = license | default("GPL-3.0-or-later") -%} | ||
# {{ module_name }}.py - {{ description }} | ||
# Author: {{ author }} | ||
# License: {{ license }} | ||
|
||
from __future__ import absolute_import, annotations, division, print_function | ||
|
||
|
||
__metaclass__ = type # pylint: disable=C0103 | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
|
||
if TYPE_CHECKING: | ||
from typing import Callable | ||
|
||
|
||
DOCUMENTATION = """ | ||
name: {{ module_name }} | ||
author: {{ author }} | ||
version_added: "1.0.0" | ||
short_description: {{ description }} | ||
description: | ||
- This is a demo module plugin designed to return Hello message. | ||
options: | ||
name: | ||
description: Value specified here is appended to the Hello message. | ||
type: str | ||
""" | ||
|
||
EXAMPLES = """ | ||
# {{ module_name }} module example | ||
{% raw %} | ||
- name: Display a hello message | ||
ansible.builtin.debug: | ||
msg: "{{ 'ansible-creator' {%- endraw %} | {{ module_name }} }}" | ||
""" | ||
|
||
|
||
def _hello_world(name: str) -> str: | ||
"""Returns Hello message. | ||
|
||
Args: | ||
name: The name to greet. | ||
|
||
Returns: | ||
str: The greeting message. | ||
""" | ||
return "Hello, " + name | ||
|
||
|
||
class SampleModule: | ||
"""module plugin.""" | ||
|
||
def modules(self) -> dict[str, Callable[[str], str]]: | ||
"""Map module plugin names to their functions. | ||
|
||
Returns: | ||
dict: The module plugin functions. | ||
""" | ||
return {"{{ module_name }}": _hello_world} |
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
Empty file.
60 changes: 60 additions & 0 deletions
60
tests/fixtures/collection/testorg/testcol/plugins/sample_module/hello_world.py
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,60 @@ | ||
# hello_world.py - A custom module plugin for Ansible. | ||
# Author: Your Name (@username) | ||
# License: GPL-3.0-or-later | ||
|
||
from __future__ import absolute_import, annotations, division, print_function | ||
|
||
|
||
__metaclass__ = type # pylint: disable=C0103 | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
|
||
if TYPE_CHECKING: | ||
from typing import Callable | ||
|
||
|
||
DOCUMENTATION = """ | ||
name: hello_world | ||
author: Your Name (@username) | ||
version_added: "1.0.0" | ||
short_description: A custom module plugin for Ansible. | ||
description: | ||
- This is a demo module plugin designed to return Hello message. | ||
options: | ||
name: | ||
description: Value specified here is appended to the Hello message. | ||
type: str | ||
""" | ||
|
||
EXAMPLES = """ | ||
# hello_world module example | ||
- name: Display a hello message | ||
ansible.builtin.debug: | ||
msg: "{{ 'ansible-creator' | hello_world }}" | ||
""" | ||
|
||
|
||
def _hello_world(name: str) -> str: | ||
"""Returns Hello message. | ||
Args: | ||
name: The name to greet. | ||
Returns: | ||
str: The greeting message. | ||
""" | ||
return "Hello, " + name | ||
|
||
|
||
class SampleModule: | ||
"""module plugin.""" | ||
|
||
def modules(self) -> dict[str, Callable[[str], str]]: | ||
"""Map module plugin names to their functions. | ||
Returns: | ||
dict: The module plugin functions. | ||
""" | ||
return {"hello_world": _hello_world} |
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