Last active
April 21, 2025 13:30
-
-
Save millerthegorilla/acedbc8c5a4eeea4beee9de42c0f29ea to your computer and use it in GitHub Desktop.
ansible action plugin - community.general.rpm_ostree_pkg.py
This file contains hidden or 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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# Copyright: Contributors to the Ansible project | |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | |
from __future__ import annotations | |
from __future__ import (absolute_import, division, print_function) | |
__metaclass__ = type | |
from ansible.plugins.action import ActionBase | |
from ansible.plugins.loader import action_loader | |
from ansible.playbook.task import Task | |
from ansible.module_utils.common.text.converters import to_native | |
from ansible.module_utils.errors import UnsupportedError | |
from ansible.errors import AnsibleError, AnsibleOptionsError, AnsibleActionFail | |
class ActionModule(ActionBase): | |
def run(self, tmp=None, task_vars=None): | |
super(ActionModule,self).run(tmp, task_vars) | |
reboot_args = None | |
# make a copy of the task args to play with | |
pkg_args = self._task.args.copy() | |
# bool indicator for later | |
will_reboot = False | |
# separate out arguments to be passed to the reboot module | |
if 'reboot' in pkg_args: | |
try: | |
if ((not isinstance(pkg_args['reboot'],dict)) | |
and pkg_args['reboot']): | |
reboot_args = {} | |
else: | |
reboot_args = pkg_args.get('reboot', {}) | |
del pkg_args['reboot'] | |
will_reboot = True | |
except TypeError as e: | |
raise AnsibleError('Incompatible type: %s' % to_native(e)) | |
elif 'ostree_reboot' in task_vars: | |
try: | |
if ((not isinstance(task_vars['ostree_reboot'],dict)) | |
and task_vars['ostree_reboot']): | |
reboot_args = {} | |
else: | |
reboot_args = task_vars.get('ostree_reboot', {}) | |
will_reboot = True | |
except TypeError as e: | |
raise AnsibleError('Incompatible type: %s' % to_native(e)) | |
# run the rpm_ostree_pkg module | |
print(reboot_args) | |
pkg_return = self._execute_module(module_name='community.general.rpm_ostree_pkg', | |
module_args=pkg_args, | |
task_vars=task_vars, tmp=tmp) | |
# check to see if a pending deployment exists | |
pending_result = 'pending' in self._low_level_execute_command('rpm-ostree db diff')['stdout'] | |
reboot_result = {} | |
# if reboot is requested, reboot if a pending deployment | |
# exists. always_reboot indicates that a reboot is to | |
# be performed if there is a pending deployment | |
if (('needs_reboot' in pkg_return and pkg_return['needs_reboot']) | |
or pending_result and will_reboot | |
or ('always_reboot' in reboot_args | |
and reboot_args['always_reboot'])): | |
# not sure of the correct way to handle stdout during execution | |
# but print works ok | |
print("...Rebooting...") | |
if 'always_reboot' in reboot_args: | |
del reboot_args['always_reboot'] | |
# capture bad options and inform the user | |
# that they are bad options to the reboot | |
# module as opposed to rpm_ostree_pkg | |
try: | |
self._task.args = reboot_args | |
# prepare the reboot task | |
# reboot is an action hence the need to | |
# use action_loader | |
lookup = action_loader.get('ansible.builtin.reboot', loader=self._loader, | |
templar=self._templar, connection=self._connection, | |
task=self._task, play_context=self._play_context, | |
shared_loader_obj=self._shared_loader_obj) | |
reboot_result = lookup.run(task_vars=task_vars, tmp=tmp) | |
except (AnsibleActionFail, UnsupportedError) as e: | |
# let the user know a bad option has been passed | |
raise AnsibleOptionsError('Error with reboot: %s' % to_native(e)) | |
if reboot_result: | |
pkg_return.update({ 'reboot_result': reboot_result }) | |
return pkg_return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment