Andy Bavier | ddb7cfc | 2017-08-15 06:28:17 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Matteo Scandolo | 3896c47 | 2017-08-01 13:31:42 -0700 | [diff] [blame] | 2 | |
| 3 | # Copyright 2017-present Open Networking Foundation |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Zack Williams | a276311 | 2017-01-03 11:38:38 -0700 | [diff] [blame] | 17 | import json |
| 18 | import os |
| 19 | import requests |
| 20 | import sys |
| 21 | import traceback |
| 22 | |
| 23 | from ansible.module_utils.basic import AnsibleModule |
| 24 | |
| 25 | def main(): |
| 26 | |
| 27 | # args styled after the uri module |
| 28 | module = AnsibleModule( |
| 29 | argument_spec = dict( |
| 30 | url = dict(required=True, type='str'), |
| 31 | recipe = dict(required=True, type='str'), |
| 32 | user = dict(required=True, type='str'), |
| 33 | password = dict(required=True, type='str'), |
| 34 | ) |
| 35 | ) |
| 36 | |
| 37 | xos_auth=(module.params['user'], module.params['password']) |
| 38 | |
| 39 | r = requests.post(module.params['url'], data={"recipe": module.params['recipe']}, auth=xos_auth) |
| 40 | if (r.status_code != 200): |
| 41 | try: |
| 42 | error_text=r.json()["error_text"] |
| 43 | except: |
| 44 | error_text="error while formatting the error: " + traceback.format_exc() |
| 45 | module.fail_json(msg=error_text, rc=r.status_code) |
| 46 | |
| 47 | result = r.json() |
| 48 | if "log_msgs" in result: |
| 49 | module.exit_json(changed=True, msg="\n".join(result["log_msgs"])+"\n") |
| 50 | else: |
| 51 | module.exit_json(changed=True, msg="success") |
| 52 | |
| 53 | if __name__ == '__main__': |
| 54 | main() |