Scott Baker | f93a06c | 2016-07-11 17:04:49 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import json |
| 4 | import os |
| 5 | import requests |
| 6 | import sys |
| 7 | import traceback |
| 8 | |
| 9 | from ansible.module_utils.basic import AnsibleModule |
| 10 | |
| 11 | def main(): |
| 12 | module = AnsibleModule( |
| 13 | argument_spec = dict( |
| 14 | recipe = dict(required=True, type='str'), |
| 15 | username = dict(required=True, type='str'), |
| 16 | password = dict(required=True, type='str'), |
| 17 | hostname = dict(default="127.0.0.1", type="str"), |
| 18 | port = dict(default=80, type="int") |
| 19 | ) |
| 20 | ) |
| 21 | |
| 22 | xos_auth=(module.params['username'], module.params['password']) |
| 23 | |
| 24 | url = "http://%s:%d/api/utility/tosca/run/" % (module.params['hostname'], module.params['port']) |
| 25 | |
| 26 | r = requests.post(url, data={"recipe": module.params['recipe']}, auth=xos_auth) |
| 27 | if (r.status_code != 200): |
| 28 | try: |
| 29 | error_text=r.json()["error_text"] |
| 30 | except: |
| 31 | error_text="error while formatting the error: " + traceback.format_exc() |
| 32 | module.fail_json(msg=error_text, rc=r.status_code) |
| 33 | |
| 34 | result = r.json() |
| 35 | if "log_msgs" in result: |
| 36 | module.exit_json(changed=True, msg="\n".join(result["log_msgs"])+"\n") |
| 37 | else: |
| 38 | module.exit_json(changed=True, msg="success") |
| 39 | |
| 40 | if __name__ == '__main__': |
| 41 | main() |