2017-01-06 15:12:24 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
# (c) 2016, James Hogarth <james.hogarth@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2017-03-14 16:07:22 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.0',
|
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
2017-02-02 19:45:22 +00:00
|
|
|
|
2017-01-06 15:12:24 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
author: James Hogarth
|
|
|
|
module: jenkins_script
|
|
|
|
short_description: Executes a groovy script in the jenkins instance
|
|
|
|
version_added: '2.3'
|
|
|
|
description:
|
2017-02-13 14:02:34 +00:00
|
|
|
- The C(jenkins_script) module takes a script plus a dict of values
|
2017-01-06 15:12:24 +00:00
|
|
|
to use within the script and returns the result of the script being run.
|
|
|
|
|
|
|
|
options:
|
|
|
|
script:
|
|
|
|
description:
|
|
|
|
- The groovy script to be executed.
|
|
|
|
This gets passed as a string Template if args is defined.
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
url:
|
|
|
|
description:
|
|
|
|
- The jenkins server to execute the script against. The default is a local
|
|
|
|
jenkins instance that is not being proxied through a webserver.
|
|
|
|
required: false
|
|
|
|
default: http://localhost:8080
|
|
|
|
validate_certs:
|
|
|
|
description:
|
|
|
|
- If set to C(no), the SSL certificates will not be validated.
|
|
|
|
This should only set to C(no) used on personally controlled sites
|
|
|
|
using self-signed certificates as it avoids verifying the source site.
|
|
|
|
required: false
|
|
|
|
default: True
|
|
|
|
user:
|
|
|
|
description:
|
|
|
|
- The username to connect to the jenkins server with.
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
password:
|
|
|
|
description:
|
|
|
|
- The password to connect to the jenkins server with.
|
2017-03-09 16:20:25 +00:00
|
|
|
required: false
|
2017-01-06 15:12:24 +00:00
|
|
|
default: null
|
2017-05-24 10:16:05 +00:00
|
|
|
timeout:
|
|
|
|
description:
|
|
|
|
- The request timeout in seconds
|
|
|
|
required: false
|
|
|
|
default: 10
|
|
|
|
version_added: "2.4"
|
2017-01-06 15:12:24 +00:00
|
|
|
args:
|
|
|
|
description:
|
2017-07-06 20:41:08 +00:00
|
|
|
- A dict of key-value pairs used in formatting the script using string.Template (see https://docs.python.org/2/library/string.html#template-strings).
|
2017-01-06 15:12:24 +00:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
|
|
|
|
notes:
|
|
|
|
- Since the script can do anything this does not report on changes.
|
|
|
|
Knowing the script is being run it's important to set changed_when
|
|
|
|
for the ansible output to be clear on any alterations made.
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Obtaining a list of plugins
|
|
|
|
jenkins_script:
|
|
|
|
script: 'println(Jenkins.instance.pluginManager.plugins)'
|
|
|
|
user: admin
|
|
|
|
password: admin
|
|
|
|
|
|
|
|
- name: Setting master using a variable to hold a more complicate script
|
|
|
|
vars:
|
|
|
|
setmaster_mode: |
|
|
|
|
import jenkins.model.*
|
|
|
|
instance = Jenkins.getInstance()
|
|
|
|
instance.setMode(${jenkins_mode})
|
|
|
|
instance.save()
|
|
|
|
|
|
|
|
- name: use the variable as the script
|
|
|
|
jenkins_script:
|
|
|
|
script: "{{ setmaster_mode }}"
|
|
|
|
args:
|
|
|
|
jenkins_mode: Node.Mode.EXCLUSIVE
|
|
|
|
|
|
|
|
- name: interacting with an untrusted HTTPS connection
|
|
|
|
jenkins_script:
|
|
|
|
script: "println(Jenkins.instance.pluginManager.plugins)"
|
|
|
|
user: admin
|
|
|
|
password: admin
|
|
|
|
url: https://localhost
|
|
|
|
validate_certs: no
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
output:
|
|
|
|
description: Result of script
|
|
|
|
returned: success
|
|
|
|
type: string
|
|
|
|
sample: 'Result: true'
|
|
|
|
'''
|
|
|
|
|
2017-04-06 09:03:57 +00:00
|
|
|
import json
|
|
|
|
|
2017-01-06 15:12:24 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2017-05-19 17:22:16 +00:00
|
|
|
from ansible.module_utils.six.moves.urllib.parse import urlencode
|
2017-01-06 15:12:24 +00:00
|
|
|
from ansible.module_utils.urls import fetch_url
|
|
|
|
|
2017-05-01 15:01:25 +00:00
|
|
|
|
2017-04-06 09:03:57 +00:00
|
|
|
def is_csrf_protection_enabled(module):
|
|
|
|
resp, info = fetch_url(module,
|
|
|
|
module.params['url'] + '/api/json',
|
|
|
|
method='GET')
|
|
|
|
if info["status"] != 200:
|
|
|
|
module.fail_json(msg="HTTP error " + str(info["status"]) + " " + info["msg"])
|
|
|
|
|
|
|
|
content = resp.read()
|
|
|
|
return json.loads(content).get('useCrumbs', False)
|
|
|
|
|
2017-05-01 15:01:25 +00:00
|
|
|
|
2017-04-06 09:03:57 +00:00
|
|
|
def get_crumb(module):
|
|
|
|
resp, info = fetch_url(module,
|
|
|
|
module.params['url'] + '/crumbIssuer/api/json',
|
|
|
|
method='GET')
|
|
|
|
if info["status"] != 200:
|
|
|
|
module.fail_json(msg="HTTP error " + str(info["status"]) + " " + info["msg"])
|
|
|
|
|
|
|
|
content = resp.read()
|
|
|
|
return json.loads(content)
|
|
|
|
|
2017-05-01 15:01:25 +00:00
|
|
|
|
2017-01-06 15:12:24 +00:00
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
2017-05-01 15:01:25 +00:00
|
|
|
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),
|
2017-05-24 10:16:05 +00:00
|
|
|
timeout=dict(required=False, type="int", default=10),
|
2017-05-01 15:01:25 +00:00
|
|
|
args=dict(required=False, type="dict", default=None)
|
2017-01-06 15:12:24 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
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']
|
|
|
|
|
2017-04-06 09:03:57 +00:00
|
|
|
headers = {}
|
|
|
|
if is_csrf_protection_enabled(module):
|
|
|
|
crumb = get_crumb(module)
|
|
|
|
headers = {crumb['crumbRequestField']: crumb['crumb']}
|
2017-01-06 15:12:24 +00:00
|
|
|
|
|
|
|
resp, info = fetch_url(module,
|
|
|
|
module.params['url'] + "/scriptText",
|
|
|
|
data=urlencode({'script': script_contents}),
|
2017-05-01 15:01:25 +00:00
|
|
|
headers=headers,
|
2017-05-24 10:16:05 +00:00
|
|
|
method="POST",
|
|
|
|
timeout=module.params['timeout'])
|
2017-01-06 15:12:24 +00:00
|
|
|
|
|
|
|
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(
|
2017-05-01 15:01:25 +00:00
|
|
|
output=result,
|
2017-01-06 15:12:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|