From 8895c3ee9ec687aa66b1fd2dea21023ff3d7a42e Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Mon, 17 Oct 2022 18:51:54 +0200 Subject: [PATCH] machinectl: add enabled --- ansible/library/machinectl.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/ansible/library/machinectl.py b/ansible/library/machinectl.py index 4bcea61..f1a437f 100644 --- a/ansible/library/machinectl.py +++ b/ansible/library/machinectl.py @@ -4,6 +4,7 @@ from time import sleep from os import fdopen from dbus import SystemBus, Array from dbus.exceptions import DBusException +from subprocess import run from ansible.module_utils.basic import AnsibleModule @@ -141,6 +142,13 @@ def start(bus, machines): errors.append(f'{host}: ' + '\n'.join(ret)) return changed, errors + +def enable(machines): + cmd = ['/usr/bin/machinectl', 'enable'] + machines + run(cmd) + return True + + def run_module(): # define available arguments/parameters a user can pass to the module module_args = dict( @@ -172,21 +180,23 @@ def run_module(): if module.check_mode: module.exit_json(**result) - bus = SystemBus() - # manipulate or modify the state as needed (this is going to be the # part where your module will do what it needs to do) machines = module.params['machines'] if module.params['state'] == 'stopped': + bus = SystemBus() result['changed'], errors = stop(bus, machines) if errors: errors = '\n\n'.join(errors) module.fail_json(msg=f'Some machines are not stopping correctly {errors}', **result) elif module.params['state'] == 'started': + bus = SystemBus() result['changed'], errors = start(bus, machines) if errors: errors = '\n\n'.join(errors) module.fail_json(msg=f'Some machines are not running correctly {errors}', **result) + elif module.params['state'] == 'enabled': + result['changed'] = enable(machines) else: module.fail_json(msg=f"Unknown state: {module.params['state']}")