machinectl: add enabled

This commit is contained in:
Emmanuel Garette 2022-10-17 18:51:54 +02:00
parent de48994d76
commit 8895c3ee9e

View file

@ -4,6 +4,7 @@ from time import sleep
from os import fdopen from os import fdopen
from dbus import SystemBus, Array from dbus import SystemBus, Array
from dbus.exceptions import DBusException from dbus.exceptions import DBusException
from subprocess import run
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
@ -141,6 +142,13 @@ def start(bus, machines):
errors.append(f'{host}: ' + '\n'.join(ret)) errors.append(f'{host}: ' + '\n'.join(ret))
return changed, errors return changed, errors
def enable(machines):
cmd = ['/usr/bin/machinectl', 'enable'] + machines
run(cmd)
return True
def run_module(): def run_module():
# define available arguments/parameters a user can pass to the module # define available arguments/parameters a user can pass to the module
module_args = dict( module_args = dict(
@ -172,21 +180,23 @@ def run_module():
if module.check_mode: if module.check_mode:
module.exit_json(**result) module.exit_json(**result)
bus = SystemBus()
# manipulate or modify the state as needed (this is going to be the # manipulate or modify the state as needed (this is going to be the
# part where your module will do what it needs to do) # part where your module will do what it needs to do)
machines = module.params['machines'] machines = module.params['machines']
if module.params['state'] == 'stopped': if module.params['state'] == 'stopped':
bus = SystemBus()
result['changed'], errors = stop(bus, machines) result['changed'], errors = stop(bus, machines)
if errors: if errors:
errors = '\n\n'.join(errors) errors = '\n\n'.join(errors)
module.fail_json(msg=f'Some machines are not stopping correctly {errors}', **result) module.fail_json(msg=f'Some machines are not stopping correctly {errors}', **result)
elif module.params['state'] == 'started': elif module.params['state'] == 'started':
bus = SystemBus()
result['changed'], errors = start(bus, machines) result['changed'], errors = start(bus, machines)
if errors: if errors:
errors = '\n\n'.join(errors) errors = '\n\n'.join(errors)
module.fail_json(msg=f'Some machines are not running correctly {errors}', **result) module.fail_json(msg=f'Some machines are not running correctly {errors}', **result)
elif module.params['state'] == 'enabled':
result['changed'] = enable(machines)
else: else:
module.fail_json(msg=f"Unknown state: {module.params['state']}") module.fail_json(msg=f"Unknown state: {module.params['state']}")