blob: 4724395712f260b05bed6416542edecee4de6a3b [file] [log] [blame]
Andy Bavierddb7cfc2017-08-15 06:28:17 -07001#!/usr/bin/python
Matteo Scandolo3896c472017-08-01 13:31:42 -07002
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 Williamsa2763112017-01-03 11:38:38 -070017import json
18import os
19import requests
20import sys
21import traceback
22
23from ansible.module_utils.basic import AnsibleModule
24
25def 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
53if __name__ == '__main__':
54 main()