Last active
August 1, 2022 22:01
-
-
Save Varriount/dd7cdc355c787824bd057ac4a22a94ce to your computer and use it in GitHub Desktop.
Ansible Action Plugin Template (modified from https://gist.github.com/ju2wheels/408e2d34c788e417832c756320d05fb5)
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
from __future__ import (absolute_import, division, print_function) | |
__metaclass__ = type | |
from ansible.plugins.action import ActionBase | |
# Load the display handler to send logging to CLI or relevant display mechanism | |
try: | |
from __main__ import display | |
except ImportError: | |
from ansible.utils.display import Display | |
display = Display() | |
# Create a plugin based off of `ActionBase` from `ansible.plugins.action`. | |
# The plugin class must be named "ActionModule" | |
# | |
# At a minimum, an ActionModule must have a defined `run` method. | |
# | |
# An action plugin must always have an associated Ansible module of the same name, even if the | |
# module will not be doing any work. It is common practice for the module to at least contain a | |
# documentation fragment, even if it doesn't actually do anything. If a module has an associated | |
# action plugin, the action plugin will always override the module and be run instead of the module. | |
# If both the module and action must run, the action plugin must explicitly execute the module. | |
# | |
# Useful methods inherited from ActionBase: | |
# - `_execute_module`: Instance method used to find and execute another Ansible module. | |
# - `_remote_expand_user`: Expand a tilde in a file PATH. | |
# | |
# Useful instance properties inherited from ActionBase: | |
# - `self._connection`: | |
# An `ansible.plugins.connection.ConnectionBase` subclass instance. | |
# Useful methods are `exec_command`, `fetch_file`, and `put_file`. | |
# - `self._loader`: | |
# An `ansible.parsing.dataloader.DataLoader` instance used to parse JSON/YAML files or | |
# strings. | |
# Supports Ansible Vault encryption. | |
# - `self._play_context`: | |
# An `ansible.playbook.play_context.PlayContext` instance that encapsulates a connection | |
# instance and play details. | |
# - `self._shared_loader_obj`: | |
# An `ansible.plugins.loader.PluginLoader` subclass instance which loads plugins from | |
# configured plugin directories. | |
# The `ansible.plugins.loader` module contains instances of `PluginLoader` specialized in | |
# finding different types of plugins. | |
# - `self._task`: | |
# An `ansible.playbook.task.Task` instance for the current task. | |
# Contains `action`, `args`, `parent`, `role` and other task properties. | |
# | |
class ActionModule(ActionBase): | |
# Some plugins may use class constants to control behavior. | |
# In the case of `TRANSFERS_FILES`, it is used by ActionBase to determine at which point in | |
# execution temporary directories need to be available if the Action Plugin is using modules | |
# to transfer files. | |
TRANSFERS_FILES = False | |
# The `run` method is the main Action Plugin driver. All work is done from within this method. | |
# | |
# tmp: | |
# Temporary directory. | |
# Sometimes an action plugin sets up a temporary directory and then calls another module. | |
# This parameter allows reusing the same directory for both. | |
# | |
# task_vars: | |
# The variables (host vars, group vars, config vars, etc) associated with this task. | |
# Note that while this will contain Ansible facts from the host, they should be used | |
# with caution as a user running Ansible can disable their collection. If you want to make | |
# sure that your Action Plugin always has access to the ones it needs, you may want to | |
# consider running the setup module directly in the run the method and getting Ansible facts | |
# that way. | |
# The strategy plugin which manages running tasks on instances uses an | |
# `ansible.vars.manager.VariableManager` instance to retrieve this context specific | |
# dictionary of variables. | |
# | |
def run(self, tmp=None, task_vars=None): | |
# Initialize the parent. The returned result is normally an empty dict, unless the plugin | |
# inherits from another ActionBase subclass that also performs tasks in its `run` method. | |
# Otherwise, all that the `run` call will do is parameter validation. | |
# | |
# For a list of common properties included in a result, | |
# see `ansible/utils/module_docs_fragments/return_common.py`. | |
# | |
result = super(ActionModule, self).run(tmp, task_vars) | |
# Initialize the result object with some `return_common` values. | |
result.update( | |
dict( | |
changed=False, | |
failed=False, | |
msg='', | |
skipped=False | |
) | |
) | |
# Define support for check mode and the `async` parameter. | |
self._supports_check_mode = True | |
self._supports_async = False | |
# Execute another module - the "setup" module - to collect facts. | |
# | |
# Parameters: | |
# - `module_name`: | |
# The name of the Ansible module to run. | |
# - `module_args`: | |
# A dict of arguments to provide to the Ansible module. | |
# - `persist_files`: | |
# A boolean that determines whether to keep temporary files. | |
# - `task_vars`: | |
# The task variables for the current play context. | |
# - `tmp`: | |
# The path to the temporary directory. | |
# - `delete_remote_tmp`: | |
# A boolean that determines whether the remote tmp directory and files are deleted. | |
# - `wrap_async`: | |
# A boolean that controls whether the task is run asynchronously. | |
# | |
setup_result = self._execute_module( | |
module_name='setup', | |
module_args=dict( | |
gather_subset='all', | |
gather_timeout=10 | |
), | |
task_vars=task_vars, | |
persist_files=False, | |
tmp=tmp, | |
delete_remote_tmp=True, | |
wrap_async=getattr(self._task, 'async') | |
) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment