Skip to content

Instantly share code, notes, and snippets.

@kalkin
Created October 12, 2015 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kalkin/f73b447077cd9ad707cd to your computer and use it in GitHub Desktop.
Save kalkin/f73b447077cd9ad707cd to your computer and use it in GitHub Desktop.
#!/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