Skip to content

Instantly share code, notes, and snippets.

@tstrul1
Created September 30, 2020 15:40
Show Gist options
  • Select an option

  • Save tstrul1/bada21f89438d54675211311d99c99ba to your computer and use it in GitHub Desktop.

Select an option

Save tstrul1/bada21f89438d54675211311d99c99ba to your computer and use it in GitHub Desktop.
custom ansible module
#!/usr/bin/python
# -*- coding: utf-8 -*-
from ansible.module_utils.basic import AnsibleModule
from zeep import Client as Client, Transport
import urllib3
import time
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
DOCUMENTATION = '''
---
module: ks_kill_and_wait
short_description: Kill active processes and wait until KS is dry
requirements: zeep == 3.4.0
'''
EXAMPLES = '''
- name: Dry KS
ks_kill_and_wait:
ks_fqdn: "fqdn"
api_user: "user"
api_password: "password"
timeout: 600 (seconds)
interval: 5 (seconds)
'''
def configure_connection(url):
transport = Transport()
transport.session.verify = False
return Client(wsdl=url, transport=transport)
def abort_stoppable_processes(client, api_user, api_password):
return client.service.abortStoppableProcesses(api_user, api_password, '')
def check_if_ks_is_idle(client, api_user, api_password, allowed_processes):
return client.service.checkIfInIdleState(api_user, api_password, '0', 'true', '420000', '')
def main():
module_args = {
"ks_fqdn": {"required": True, "type": "str"},
"api_user": {"required": True, "type": "str"},
"api_password": {"required": True, "type": "str", "no_log": True},
"interval": {"required": True, "type": "int"},
"timeout": {"required": True, "type": "int"},
}
module = AnsibleModule(argument_spec=module_args)
url = "https://{}/<?wsdl url>".format(module.params['ks_fqdn'])
api_user = module.params['api_user']
api_password = module.params['api_password']
interval = module.params['interval']
timeout = time.time() + module.params['timeout']
client = configure_connection(url)
abort_stoppable_processes(client, api_user, api_password)
start_time = time.time()
while not check_if_ks_is_idle(client, api_user, api_password, allowed_processes):
if time.time() > timeout:
result = "TIMEOUT REACHED: KS is not dry after {} seconds".format(timeout)
module.fail_json(msg=result)
time.sleep(interval)
timer = round(time.time() - start_time, 2)
result = {"dry_time": timer }
module.exit_json(changed=True, meta=result)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment