forked from stove/risotto
machinectl: add enabled
This commit is contained in:
parent
de48994d76
commit
8895c3ee9e
1 changed files with 12 additions and 2 deletions
|
@ -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']}")
|
||||
|
||||
|
|
Loading…
Reference in a new issue