Zack Williams | a276311 | 2017-01-03 11:38:38 -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 | |
| 13 | # args styled after the uri module |
| 14 | module = AnsibleModule( |
| 15 | argument_spec = dict( |
| 16 | url = dict(required=True, type='str'), |
| 17 | recipe = dict(required=True, type='str'), |
| 18 | user = dict(required=True, type='str'), |
| 19 | password = dict(required=True, type='str'), |
| 20 | ) |
| 21 | ) |
| 22 | |
| 23 | xos_auth=(module.params['user'], module.params['password']) |
| 24 | |
| 25 | r = requests.post(module.params['url'], data={"recipe": module.params['recipe']}, auth=xos_auth) |
| 26 | if (r.status_code != 200): |
| 27 | try: |
| 28 | error_text=r.json()["error_text"] |
| 29 | except: |
| 30 | error_text="error while formatting the error: " + traceback.format_exc() |
| 31 | module.fail_json(msg=error_text, rc=r.status_code) |
| 32 | |
| 33 | result = r.json() |
| 34 | if "log_msgs" in result: |
| 35 | module.exit_json(changed=True, msg="\n".join(result["log_msgs"])+"\n") |
| 36 | else: |
| 37 | module.exit_json(changed=True, msg="success") |
| 38 | |
| 39 | if __name__ == '__main__': |
| 40 | main() |