Created
October 12, 2015 11:42
-
-
Save kalkin/f73b447077cd9ad707cd to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python | |
DOCUMENTATION = ''' | |
--- | |
module: qvm | |
short_description: check state presence/absence/running/halted of a qubes vm | |
description: | |
- check presence/absence/running/halted of a qubes vm. If a vm is not | |
running it will be started. If vm is not halted it will be stopped. | |
options: | |
name: | |
description: | |
- The name of the vm | |
required: true | |
state | |
description: | |
- present, absent or running | |
required: false | |
default: present | |
author: Bahtiar `kalkin-` Gadimov` | |
''' | |
from qubes.qubes import QubesVmCollection | |
from ansible.module_utils.basic import * | |
def main(): | |
module = AnsibleModule( | |
argument_spec = dict( | |
state = dict(default='present', choices=['present', 'absent', | |
'running', 'halted']), | |
name = dict(required=True) | |
), | |
) | |
col = QubesVmCollection() | |
col.lock_db_for_reading() | |
col.load() | |
col.unlock_db() | |
vm = col.get_vm_by_name(module.params.get('name')) | |
state = module.params.get('state') | |
if not vm: | |
if state == 'absent': | |
module.exit_json(changed=False) | |
else: | |
module.fail_json(msg="VM does not exist") | |
elif state == 'running': | |
if vm.is_running(): | |
module.exit_json(changed=False) | |
else: | |
vm.start() | |
while not vm.is_running(): | |
time.sleep(0.25) | |
module.exit_json(changed=True) | |
elif state == 'halted': | |
if not vm.is_running(): | |
module.exit_json(changed=False) | |
else: | |
vm.shutdown() | |
while vm.is_running(): | |
time.sleep(0.25) | |
module.exit_json(changed=True) | |
elif state == 'present': | |
module.exit_json(changed=False) | |
else: | |
module.fail_json(msg="WTF??? This should not happen") | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment