-
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 lookup plugin through add subcommand (#327)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
7f2e436
commit 6f0c674
Showing
10 changed files
with
498 additions
and
11 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.
86 changes: 86 additions & 0 deletions
86
src/ansible_creator/resources/collection_project/plugins/lookup/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,86 @@ | ||
{# lookup_plugin_template.j2 #} | ||
{%- set lookup_name = plugin_name | default("hello_world") -%} | ||
{%- set author = author | default("Your Name") -%} | ||
{%- set description = description | default("A custom lookup plugin for Ansible.") -%} | ||
{%- set license = license | default("GPL-3.0-or-later") -%} | ||
# pylint: disable=E0401 | ||
# {{ lookup_name }}.py - {{ description }} | ||
# Author: {{ author }} | ||
# License: {{ license }} | ||
|
||
from ansible.plugins.lookup import LookupBase # type: ignore | ||
from ansible.errors import AnsibleError # type: ignore | ||
from ansible.utils.display import Display # type: ignore | ||
from typing import Any, Optional, Dict, List | ||
|
||
display = Display() | ||
|
||
DOCUMENTATION = """ | ||
name: {{ lookup_name }} | ||
author: {{ author }} | ||
version_added: "1.0.0" | ||
short_description: {{ description }} | ||
description: | ||
- This is a custom lookup plugin to provide lookup functionality. | ||
options: | ||
_terms: | ||
description: Terms to lookup | ||
required: True | ||
notes: | ||
- This is a scaffold template. Customize the plugin to fit your needs. | ||
""" | ||
|
||
EXAMPLES = """ | ||
- name: Example usage of {{ lookup_name }} | ||
{%- raw %} | ||
ansible.builtin.debug: | ||
msg: "{{ lookup('{%- endraw %}{{ lookup_name }}', 'example_term') }}" | ||
""" | ||
|
||
RETURN = """ | ||
_list: | ||
description: The list of values found by the lookup | ||
type: list | ||
""" | ||
|
||
|
||
class LookupModule(LookupBase): # type: ignore[misc] | ||
""" | ||
Custom Ansible lookup plugin: hello_world | ||
A custom lookup plugin for Ansible. | ||
""" | ||
|
||
def run( | ||
self, | ||
terms: List[str], | ||
variables: Optional[Dict[str, Any]] = None, | ||
**kwargs: Dict[str, Any], | ||
) -> list[str]: | ||
""" | ||
Run the lookup with the specified terms. | ||
|
||
Args: | ||
terms: A list of terms to lookup. | ||
variables: Additional variables. | ||
**kwargs: Additional keyword arguments. | ||
|
||
Returns: | ||
list: A list of processed results. | ||
|
||
Raises: | ||
AnsibleError: If the 'terms' parameter is not a list. | ||
""" | ||
if not isinstance(terms, list): | ||
raise AnsibleError("The 'terms' parameter must be a list.") | ||
|
||
display.vvv(f"Running hello_world lookup plugin with terms: {terms}") | ||
|
||
try: | ||
# Example processing logic - Replace this with actual lookup code | ||
result = [term.upper() for term in terms] | ||
|
||
display.vvv(f"Result from hello_world lookup: {result}") | ||
return result | ||
|
||
except Exception as e: | ||
raise AnsibleError(f"Error in hello_world plugin: {e}") |
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
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.
Oops, something went wrong.