| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Ansible module to manage rundeck projects |
| 4 |
# Copyright (c) 2017, Loic Blot <[email protected]> |
| 5 |
# Sponsored by Infopro Digital. http://www.infopro-digital.com/ |
| 6 |
# Sponsored by E.T.A.I. http://www.etai.fr/ |
| 7 |
# |
| 8 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 9 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 10 |
|
| 11 |
from __future__ import annotations |
| 12 |
|
| 13 |
DOCUMENTATION = r""" |
| 14 |
module: rundeck_project |
| 15 |
|
| 16 |
short_description: Manage Rundeck projects |
| 17 |
description: |
| 18 |
- Create and remove Rundeck projects through HTTP API. |
| 19 |
author: "Loic Blot (@nerzhul)" |
| 20 |
attributes: |
| 21 |
check_mode: |
| 22 |
support: full |
| 23 |
diff_mode: |
| 24 |
support: none |
| 25 |
options: |
| 26 |
state: |
| 27 |
type: str |
| 28 |
description: |
| 29 |
- Create or remove Rundeck project. |
| 30 |
choices: ['present', 'absent'] |
| 31 |
default: 'present' |
| 32 |
name: |
| 33 |
type: str |
| 34 |
description: |
| 35 |
- Sets the project name. |
| 36 |
required: true |
| 37 |
api_token: |
| 38 |
description: |
| 39 |
- Sets the token to authenticate against Rundeck API. |
| 40 |
aliases: ["token"] |
| 41 |
client_cert: |
| 42 |
version_added: '0.2.0' |
| 43 |
client_key: |
| 44 |
version_added: '0.2.0' |
| 45 |
force: |
| 46 |
version_added: '0.2.0' |
| 47 |
force_basic_auth: |
| 48 |
version_added: '0.2.0' |
| 49 |
http_agent: |
| 50 |
version_added: '0.2.0' |
| 51 |
url_password: |
| 52 |
version_added: '0.2.0' |
| 53 |
url_username: |
| 54 |
version_added: '0.2.0' |
| 55 |
use_proxy: |
| 56 |
version_added: '0.2.0' |
| 57 |
validate_certs: |
| 58 |
version_added: '0.2.0' |
| 59 |
extends_documentation_fragment: |
| 60 |
- ansible.builtin.url |
| 61 |
- community.general._attributes |
| 62 |
- community.general._rundeck |
| 63 |
""" |
| 64 |
|
| 65 |
EXAMPLES = r""" |
| 66 |
- name: Create a rundeck project |
| 67 |
community.general.rundeck_project: |
| 68 |
name: "Project_01" |
| 69 |
label: "Project 01" |
| 70 |
description: "My Project 01" |
| 71 |
url: "https://rundeck.example.org" |
| 72 |
api_version: 39 |
| 73 |
api_token: "mytoken" |
| 74 |
state: present |
| 75 |
|
| 76 |
- name: Remove a rundeck project |
| 77 |
community.general.rundeck_project: |
| 78 |
name: "Project_01" |
| 79 |
url: "https://rundeck.example.org" |
| 80 |
api_token: "mytoken" |
| 81 |
state: absent |
| 82 |
""" |
| 83 |
|
| 84 |
RETURN = r""" |
| 85 |
rundeck_response: |
| 86 |
description: Rundeck response when a failure occurs. |
| 87 |
returned: failed |
| 88 |
type: str |
| 89 |
before: |
| 90 |
description: Dictionary containing project information before modification. |
| 91 |
returned: success |
| 92 |
type: dict |
| 93 |
after: |
| 94 |
description: Dictionary containing project information after modification. |
| 95 |
returned: success |
| 96 |
type: dict |
| 97 |
""" |
| 98 |
|
| 99 |
# import module snippets |
| 100 |
from ansible.module_utils.basic import AnsibleModule |
| 101 |
|
| 102 |
from ansible_collections.community.general.plugins.module_utils._rundeck import ( |
| 103 |
api_argument_spec, |
| 104 |
api_request, |
| 105 |
) |
| 106 |
|
| 107 |
|
| 108 |
class RundeckProjectManager: |
| 109 |
def __init__(self, module): |
| 110 |
self.module = module |
| 111 |
|
| 112 |
def get_project_facts(self): |
| 113 |
resp, info = api_request( |
| 114 |
module=self.module, |
| 115 |
endpoint=f"project/{self.module.params['name']}", |
| 116 |
) |
| 117 |
|
| 118 |
return resp |
| 119 |
|
| 120 |
def create_or_update_project(self): |
| 121 |
facts = self.get_project_facts() |
| 122 |
|
| 123 |
if facts is None: |
| 124 |
# If in check mode don't create project, simulate a fake project creation |
| 125 |
if self.module.check_mode: |
| 126 |
self.module.exit_json( |
| 127 |
changed=True, |
| 128 |
before={}, |
| 129 |
after={"name": self.module.params["name"]}, |
| 130 |
) |
| 131 |
|
| 132 |
resp, info = api_request( |
| 133 |
module=self.module, |
| 134 |
endpoint="projects", |
| 135 |
method="POST", |
| 136 |
data={ |
| 137 |
"name": self.module.params["name"], |
| 138 |
"config": {}, |
| 139 |
}, |
| 140 |
) |
| 141 |
|
| 142 |
if info["status"] == 201: |
| 143 |
self.module.exit_json(changed=True, before={}, after=self.get_project_facts()) |
| 144 |
else: |
| 145 |
self.module.fail_json( |
| 146 |
msg=f"Unhandled HTTP status {info['status']}, please report the bug", |
| 147 |
before={}, |
| 148 |
after=self.get_project_facts(), |
| 149 |
) |
| 150 |
else: |
| 151 |
self.module.exit_json(changed=False, before=facts, after=facts) |
| 152 |
|
| 153 |
def remove_project(self): |
| 154 |
facts = self.get_project_facts() |
| 155 |
if facts is None: |
| 156 |
self.module.exit_json(changed=False, before={}, after={}) |
| 157 |
else: |
| 158 |
# If not in check mode, remove the project |
| 159 |
if not self.module.check_mode: |
| 160 |
api_request( |
| 161 |
module=self.module, |
| 162 |
endpoint=f"project/{self.module.params['name']}", |
| 163 |
method="DELETE", |
| 164 |
) |
| 165 |
|
| 166 |
self.module.exit_json(changed=True, before=facts, after={}) |
| 167 |
|
| 168 |
|
| 169 |
def main(): |
| 170 |
# Also allow the user to set values for fetch_url |
| 171 |
argument_spec = api_argument_spec() |
| 172 |
argument_spec.update( |
| 173 |
dict( |
| 174 |
state=dict(type="str", choices=["present", "absent"], default="present"), |
| 175 |
name=dict(required=True, type="str"), |
| 176 |
) |
| 177 |
) |
| 178 |
|
| 179 |
argument_spec["api_token"]["aliases"] = ["token"] |
| 180 |
|
| 181 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 182 |
|
| 183 |
if module.params["api_version"] < 14: |
| 184 |
module.fail_json(msg="API version should be at least 14") |
| 185 |
|
| 186 |
rundeck = RundeckProjectManager(module) |
| 187 |
if module.params["state"] == "present": |
| 188 |
rundeck.create_or_update_project() |
| 189 |
elif module.params["state"] == "absent": |
| 190 |
rundeck.remove_project() |
| 191 |
|
| 192 |
|
| 193 |
if __name__ == "__main__": |
| 194 |
main() |