무척 간결한 앤서블의 jenkins_script 모듈이다.
혹시 젠킨스에서 암호 없이 접근한데 4xx 에러가 난다면. 이는 분명 proxy 이슈이다.
https://github.com/ansible/ansible/pull/19640/files
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.urls import fetch_url
try:
# python2
from urllib import urlencode
except ImportError:
# python3
from urllib.parse import urlencode
def main():
module = AnsibleModule(
argument_spec = dict(
script = dict(required=True, type="str"),
url = dict(required=False, type="str", default="http://localhost:8080"),
validate_certs = dict(required=False, type="bool", default=True),
user = dict(required=False, no_log=True, type="str",default=None),
password = dict(required=False, no_log=True, type="str",default=None),
args = dict(required=False, type="dict", default=None)
)
)
if module.params['user'] is not None:
if module.params['password'] is None:
module.fail_json(msg="password required when user provided")
module.params['url_username'] = module.params['user']
module.params['url_password'] = module.params['password']
module.params['force_basic_auth'] = True
if module.params['args'] is not None:
from string import Template
script_contents = Template(module.params['script']).substitute(module.params['args'])
else:
script_contents = module.params['script']
resp, info = fetch_url(module,
module.params['url'] + "/scriptText",
data=urlencode({'script': script_contents}),
method="POST")
if info["status"] != 200:
module.fail_json(msg="HTTP error " + str(info["status"]) + " " + info["msg"])
result = resp.read()
if 'Exception:' in result and 'at java.lang.Thread' in result:
module.fail_json(msg="script failed with stacktrace:\n " + result)
module.exit_json(
output = result,
)
if __name__ == '__main__':
main()