2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
2021-08-08 10:40:22 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2022-08-08 14:24:58 +02:00
|
|
|
# Copyright (c) 2016, James Hogarth <james.hogarth@gmail.com>
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
author: James Hogarth (@hogarthj)
|
|
|
|
module: jenkins_script
|
|
|
|
short_description: Executes a groovy script in the jenkins instance
|
|
|
|
description:
|
|
|
|
- The C(jenkins_script) module takes a script plus a dict of values
|
|
|
|
to use within the script and returns the result of the script being run.
|
|
|
|
|
Add attributes to consul, datadog, jenkins, pagerduty, rundeck, and sensu modules (#5957)
* Add attributes to consul, datadog, jenkins, pagerduty, rundeck, and sensu modules.
* Fix error.
2023-02-24 09:23:56 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: none
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
script:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The groovy script to be executed.
|
|
|
|
This gets passed as a string Template if args is defined.
|
|
|
|
required: true
|
|
|
|
url:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The jenkins server to execute the script against. The default is a local
|
|
|
|
jenkins instance that is not being proxied through a webserver.
|
|
|
|
default: http://localhost:8080
|
|
|
|
validate_certs:
|
|
|
|
description:
|
2023-06-20 08:14:02 +02:00
|
|
|
- If set to V(false), the SSL certificates will not be validated.
|
|
|
|
This should only set to V(false) used on personally controlled sites
|
2020-03-09 10:11:07 +01:00
|
|
|
using self-signed certificates as it avoids verifying the source site.
|
|
|
|
type: bool
|
2022-08-24 20:00:11 +02:00
|
|
|
default: true
|
2020-03-09 10:11:07 +01:00
|
|
|
user:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The username to connect to the jenkins server with.
|
|
|
|
password:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The password to connect to the jenkins server with.
|
|
|
|
timeout:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: int
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The request timeout in seconds
|
|
|
|
default: 10
|
|
|
|
args:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: dict
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- 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).
|
|
|
|
|
|
|
|
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
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.jenkins_script:
|
2020-03-09 10:11:07 +01:00
|
|
|
script: 'println(Jenkins.instance.pluginManager.plugins)'
|
|
|
|
user: admin
|
|
|
|
password: admin
|
|
|
|
|
|
|
|
- name: Setting master using a variable to hold a more complicate script
|
2020-07-16 13:42:12 +02:00
|
|
|
ansible.builtin.set_fact:
|
2020-03-09 10:11:07 +01:00
|
|
|
setmaster_mode: |
|
|
|
|
import jenkins.model.*
|
|
|
|
instance = Jenkins.getInstance()
|
|
|
|
instance.setMode(${jenkins_mode})
|
|
|
|
instance.save()
|
|
|
|
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Use the variable as the script
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.jenkins_script:
|
2020-03-09 10:11:07 +01:00
|
|
|
script: "{{ setmaster_mode }}"
|
|
|
|
args:
|
|
|
|
jenkins_mode: Node.Mode.EXCLUSIVE
|
|
|
|
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Interacting with an untrusted HTTPS connection
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.jenkins_script:
|
2020-03-09 10:11:07 +01:00
|
|
|
script: "println(Jenkins.instance.pluginManager.plugins)"
|
|
|
|
user: admin
|
|
|
|
password: admin
|
|
|
|
url: https://localhost
|
2023-11-26 21:54:53 +01:00
|
|
|
validate_certs: false # only do this when you trust the network!
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
output:
|
|
|
|
description: Result of script
|
|
|
|
returned: success
|
|
|
|
type: str
|
|
|
|
sample: 'Result: true'
|
|
|
|
'''
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.six.moves import http_cookiejar as cookiejar
|
|
|
|
from ansible.module_utils.six.moves.urllib.parse import urlencode
|
|
|
|
from ansible.module_utils.urls import fetch_url
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def is_csrf_protection_enabled(module):
|
|
|
|
resp, info = fetch_url(module,
|
|
|
|
module.params['url'] + '/api/json',
|
|
|
|
timeout=module.params['timeout'],
|
|
|
|
method='GET')
|
|
|
|
if info["status"] != 200:
|
|
|
|
module.fail_json(msg="HTTP error " + str(info["status"]) + " " + info["msg"], output='')
|
|
|
|
|
|
|
|
content = to_native(resp.read())
|
|
|
|
return json.loads(content).get('useCrumbs', False)
|
|
|
|
|
|
|
|
|
|
|
|
def get_crumb(module, cookies):
|
|
|
|
resp, info = fetch_url(module,
|
|
|
|
module.params['url'] + '/crumbIssuer/api/json',
|
|
|
|
method='GET',
|
|
|
|
timeout=module.params['timeout'],
|
|
|
|
cookies=cookies)
|
|
|
|
if info["status"] != 200:
|
|
|
|
module.fail_json(msg="HTTP error " + str(info["status"]) + " " + info["msg"], output='')
|
|
|
|
|
|
|
|
content = to_native(resp.read())
|
|
|
|
return json.loads(content)
|
|
|
|
|
|
|
|
|
|
|
|
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, type="str", default=None),
|
|
|
|
password=dict(required=False, no_log=True, type="str", default=None),
|
|
|
|
timeout=dict(required=False, type="int", default=10),
|
|
|
|
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", output='')
|
|
|
|
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
|
|
|
|
try:
|
|
|
|
script_contents = Template(module.params['script']).substitute(module.params['args'])
|
|
|
|
except KeyError as err:
|
|
|
|
module.fail_json(msg="Error with templating variable: %s" % err, output='')
|
|
|
|
else:
|
|
|
|
script_contents = module.params['script']
|
|
|
|
|
|
|
|
headers = {}
|
|
|
|
cookies = None
|
|
|
|
if is_csrf_protection_enabled(module):
|
|
|
|
cookies = cookiejar.LWPCookieJar()
|
|
|
|
crumb = get_crumb(module, cookies)
|
|
|
|
headers = {crumb['crumbRequestField']: crumb['crumb']}
|
|
|
|
|
|
|
|
resp, info = fetch_url(module,
|
|
|
|
module.params['url'] + "/scriptText",
|
|
|
|
data=urlencode({'script': script_contents}),
|
|
|
|
headers=headers,
|
|
|
|
method="POST",
|
|
|
|
timeout=module.params['timeout'],
|
|
|
|
cookies=cookies)
|
|
|
|
|
|
|
|
if info["status"] != 200:
|
|
|
|
module.fail_json(msg="HTTP error " + str(info["status"]) + " " + info["msg"], output='')
|
|
|
|
|
|
|
|
result = to_native(resp.read())
|
|
|
|
|
|
|
|
if 'Exception:' in result and 'at java.lang.Thread' in result:
|
|
|
|
module.fail_json(msg="script failed with stacktrace:\n " + result, output='')
|
|
|
|
|
|
|
|
module.exit_json(
|
|
|
|
output=result,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|