| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) Ansible Project |
| 4 |
# |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
DOCUMENTATION = r""" |
| 11 |
module: jenkins_job_info |
| 12 |
short_description: Get information about Jenkins jobs |
| 13 |
description: |
| 14 |
- This module can be used to query information about which Jenkins jobs which already exists. |
| 15 |
requirements: |
| 16 |
- "python-jenkins >= 0.4.12" |
| 17 |
extends_documentation_fragment: |
| 18 |
- community.general._attributes |
| 19 |
- community.general._attributes.info_module |
| 20 |
options: |
| 21 |
name: |
| 22 |
type: str |
| 23 |
description: |
| 24 |
- Exact name of the Jenkins job to fetch information about. |
| 25 |
glob: |
| 26 |
type: str |
| 27 |
description: |
| 28 |
- A shell glob of Jenkins job names to fetch information about. |
| 29 |
color: |
| 30 |
type: str |
| 31 |
description: |
| 32 |
- Only fetch jobs with the given status color. |
| 33 |
password: |
| 34 |
type: str |
| 35 |
description: |
| 36 |
- Password to authenticate with the Jenkins server. |
| 37 |
- This is mutually exclusive with O(token). |
| 38 |
token: |
| 39 |
type: str |
| 40 |
description: |
| 41 |
- API token used to authenticate with the Jenkins server. |
| 42 |
- This is mutually exclusive with O(password). |
| 43 |
url: |
| 44 |
type: str |
| 45 |
description: |
| 46 |
- URL where the Jenkins server is accessible. |
| 47 |
default: http://localhost:8080 |
| 48 |
user: |
| 49 |
type: str |
| 50 |
description: |
| 51 |
- User to authenticate with the Jenkins server. |
| 52 |
validate_certs: |
| 53 |
description: |
| 54 |
- If set to V(false), the SSL certificates are not validated. |
| 55 |
- This should only set to V(false) used on personally controlled sites using self-signed certificates. |
| 56 |
default: true |
| 57 |
type: bool |
| 58 |
author: |
| 59 |
- "Chris St. Pierre (@stpierre)" |
| 60 |
""" |
| 61 |
|
| 62 |
EXAMPLES = r""" |
| 63 |
# Get all Jenkins jobs anonymously |
| 64 |
- community.general.jenkins_job_info: |
| 65 |
user: admin |
| 66 |
register: my_jenkins_job_info |
| 67 |
|
| 68 |
# Get all Jenkins jobs using basic auth |
| 69 |
- community.general.jenkins_job_info: |
| 70 |
user: admin |
| 71 |
password: hunter2 |
| 72 |
register: my_jenkins_job_info |
| 73 |
|
| 74 |
# Get all Jenkins jobs using the token |
| 75 |
- community.general.jenkins_job_info: |
| 76 |
user: admin |
| 77 |
token: abcdefghijklmnop |
| 78 |
register: my_jenkins_job_info |
| 79 |
|
| 80 |
# Get info about a single job using basic auth |
| 81 |
- community.general.jenkins_job_info: |
| 82 |
name: some-job-name |
| 83 |
user: admin |
| 84 |
password: hunter2 |
| 85 |
register: my_jenkins_job_info |
| 86 |
|
| 87 |
# Get info about a single job in a folder using basic auth |
| 88 |
- community.general.jenkins_job_info: |
| 89 |
name: some-folder-name/some-job-name |
| 90 |
user: admin |
| 91 |
password: hunter2 |
| 92 |
register: my_jenkins_job_info |
| 93 |
|
| 94 |
# Get info about jobs matching a shell glob using basic auth |
| 95 |
- community.general.jenkins_job_info: |
| 96 |
glob: some-job-* |
| 97 |
user: admin |
| 98 |
password: hunter2 |
| 99 |
register: my_jenkins_job_info |
| 100 |
|
| 101 |
# Get info about all failing jobs using basic auth |
| 102 |
- community.general.jenkins_job_info: |
| 103 |
color: red |
| 104 |
user: admin |
| 105 |
password: hunter2 |
| 106 |
register: my_jenkins_job_info |
| 107 |
|
| 108 |
# Get info about passing jobs matching a shell glob using basic auth |
| 109 |
- community.general.jenkins_job_info: |
| 110 |
name: some-job-* |
| 111 |
color: blue |
| 112 |
user: admin |
| 113 |
password: hunter2 |
| 114 |
register: my_jenkins_job_info |
| 115 |
|
| 116 |
- name: Get the info from custom URL with token and validate_certs=False |
| 117 |
community.general.jenkins_job_info: |
| 118 |
user: admin |
| 119 |
token: 126df5c60d66c66e3b75b11104a16a8a |
| 120 |
url: https://jenkins.example.com |
| 121 |
register: my_jenkins_job_info |
| 122 |
""" |
| 123 |
|
| 124 |
RETURN = r""" |
| 125 |
jobs: |
| 126 |
description: All jobs found matching the specified criteria. |
| 127 |
returned: success |
| 128 |
type: list |
| 129 |
sample: |
| 130 |
[ |
| 131 |
{ |
| 132 |
"name": "test-job", |
| 133 |
"fullname": "test-folder/test-job", |
| 134 |
"url": "http://localhost:8080/job/test-job/", |
| 135 |
"color": "blue" |
| 136 |
} |
| 137 |
] |
| 138 |
""" |
| 139 |
|
| 140 |
import fnmatch |
| 141 |
import ssl |
| 142 |
import traceback |
| 143 |
|
| 144 |
JENKINS_IMP_ERR = None |
| 145 |
try: |
| 146 |
import jenkins |
| 147 |
|
| 148 |
HAS_JENKINS = True |
| 149 |
except ImportError: |
| 150 |
JENKINS_IMP_ERR = traceback.format_exc() |
| 151 |
HAS_JENKINS = False |
| 152 |
|
| 153 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 154 |
|
| 155 |
|
| 156 |
def get_jenkins_connection(module): |
| 157 |
url = module.params["url"] |
| 158 |
username = module.params.get("user") |
| 159 |
password = module.params.get("password") |
| 160 |
token = module.params.get("token") |
| 161 |
|
| 162 |
validate_certs = module.params.get("validate_certs") |
| 163 |
if not validate_certs: |
| 164 |
ssl._create_default_https_context = ssl._create_unverified_context |
| 165 |
|
| 166 |
if username and (password or token): |
| 167 |
return jenkins.Jenkins(url, username, password or token) |
| 168 |
elif username: |
| 169 |
return jenkins.Jenkins(url, username) |
| 170 |
else: |
| 171 |
return jenkins.Jenkins(url) |
| 172 |
|
| 173 |
|
| 174 |
def test_dependencies(module): |
| 175 |
if not HAS_JENKINS: |
| 176 |
module.fail_json( |
| 177 |
msg=missing_required_lib( |
| 178 |
"python-jenkins", url="https://python-jenkins.readthedocs.io/en/latest/install.html" |
| 179 |
), |
| 180 |
exception=JENKINS_IMP_ERR, |
| 181 |
) |
| 182 |
|
| 183 |
|
| 184 |
def get_jobs(module): |
| 185 |
jenkins_conn = get_jenkins_connection(module) |
| 186 |
jobs = [] |
| 187 |
if module.params.get("name"): |
| 188 |
try: |
| 189 |
job_info = jenkins_conn.get_job_info(module.params.get("name")) |
| 190 |
except jenkins.NotFoundException: |
| 191 |
pass |
| 192 |
else: |
| 193 |
jobs.append( |
| 194 |
{ |
| 195 |
"name": job_info["name"], |
| 196 |
"fullname": job_info["fullName"], |
| 197 |
"url": job_info["url"], |
| 198 |
"color": job_info["color"], |
| 199 |
} |
| 200 |
) |
| 201 |
|
| 202 |
else: |
| 203 |
all_jobs = jenkins_conn.get_all_jobs() |
| 204 |
if module.params.get("glob"): |
| 205 |
jobs.extend(j for j in all_jobs if fnmatch.fnmatch(j["fullname"], module.params.get("glob"))) |
| 206 |
else: |
| 207 |
jobs = all_jobs |
| 208 |
# python-jenkins includes the internal Jenkins class used for each job |
| 209 |
# in its return value; we strip that out because the leading underscore |
| 210 |
# (and the fact that it is not documented in the python-jenkins docs) |
| 211 |
# indicates that it is not part of the dependable public interface. |
| 212 |
for job in jobs: |
| 213 |
if "_class" in job: |
| 214 |
del job["_class"] |
| 215 |
|
| 216 |
if module.params.get("color"): |
| 217 |
jobs = [j for j in jobs if j["color"] == module.params.get("color")] |
| 218 |
|
| 219 |
return jobs |
| 220 |
|
| 221 |
|
| 222 |
def main(): |
| 223 |
module = AnsibleModule( |
| 224 |
argument_spec=dict( |
| 225 |
name=dict(type="str"), |
| 226 |
glob=dict(type="str"), |
| 227 |
color=dict(type="str"), |
| 228 |
password=dict(type="str", no_log=True), |
| 229 |
token=dict(type="str", no_log=True), |
| 230 |
url=dict(type="str", default="http://localhost:8080"), |
| 231 |
user=dict(type="str"), |
| 232 |
validate_certs=dict(type="bool", default=True), |
| 233 |
), |
| 234 |
mutually_exclusive=[ |
| 235 |
["password", "token"], |
| 236 |
["name", "glob"], |
| 237 |
], |
| 238 |
supports_check_mode=True, |
| 239 |
) |
| 240 |
|
| 241 |
test_dependencies(module) |
| 242 |
jobs = list() |
| 243 |
|
| 244 |
try: |
| 245 |
jobs = get_jobs(module) |
| 246 |
except jenkins.JenkinsException as err: |
| 247 |
module.fail_json(msg=f"Unable to connect to Jenkins server, {err}", exception=traceback.format_exc()) |
| 248 |
|
| 249 |
module.exit_json(changed=False, jobs=jobs) |
| 250 |
|
| 251 |
|
| 252 |
if __name__ == "__main__": |
| 253 |
main() |