2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-08 14:24:58 +02:00
|
|
|
# Copyright (c) 2017, Loic Blot <loic.blot@unix-experience.fr>
|
2020-03-09 10:11:07 +01:00
|
|
|
# Sponsored by Infopro Digital. http://www.infopro-digital.com/
|
|
|
|
# Sponsored by E.T.A.I. http://www.etai.fr/
|
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 = '''
|
|
|
|
---
|
|
|
|
module: rundeck_acl_policy
|
|
|
|
|
2022-11-09 21:18:39 +01:00
|
|
|
short_description: Manage Rundeck ACL policies
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Create, update and remove Rundeck ACL policies through HTTP API.
|
|
|
|
author: "Loic Blot (@nerzhul)"
|
[PR #5957/1f1ae558 backport][stable-6] Add attributes to consul, datadog, jenkins, pagerduty, rundeck, and sensu modules (#6057)
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.
(cherry picked from commit 1f1ae558f9cee4b2984660d70987e1afb3fb8a8d)
Co-authored-by: Felix Fontein <felix@fontein.de>
2023-02-24 11:04:27 +01:00
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
state:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Create or remove Rundeck project.
|
|
|
|
choices: ['present', 'absent']
|
|
|
|
default: 'present'
|
|
|
|
name:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Sets the project name.
|
2022-09-06 20:42:17 +02:00
|
|
|
required: true
|
2023-04-16 13:47:47 +02:00
|
|
|
api_token:
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Sets the token to authenticate against Rundeck API.
|
2023-04-16 13:47:47 +02:00
|
|
|
aliases: ["token"]
|
2020-03-09 10:11:07 +01:00
|
|
|
project:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Sets the project which receive the ACL policy.
|
|
|
|
- If unset, it's a system ACL policy.
|
|
|
|
policy:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Sets the ACL policy content.
|
|
|
|
- ACL policy content is a YAML object as described in http://rundeck.org/docs/man5/aclpolicy.html.
|
|
|
|
- It can be a YAML string or a pure Ansible inventory YAML object.
|
2020-06-13 15:01:19 +02:00
|
|
|
client_cert:
|
|
|
|
version_added: '0.2.0'
|
|
|
|
client_key:
|
|
|
|
version_added: '0.2.0'
|
|
|
|
force:
|
|
|
|
version_added: '0.2.0'
|
|
|
|
force_basic_auth:
|
|
|
|
version_added: '0.2.0'
|
|
|
|
http_agent:
|
|
|
|
version_added: '0.2.0'
|
|
|
|
url_password:
|
|
|
|
version_added: '0.2.0'
|
|
|
|
url_username:
|
|
|
|
version_added: '0.2.0'
|
|
|
|
use_proxy:
|
|
|
|
version_added: '0.2.0'
|
|
|
|
validate_certs:
|
|
|
|
version_added: '0.2.0'
|
[PR #5957/1f1ae558 backport][stable-6] Add attributes to consul, datadog, jenkins, pagerduty, rundeck, and sensu modules (#6057)
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.
(cherry picked from commit 1f1ae558f9cee4b2984660d70987e1afb3fb8a8d)
Co-authored-by: Felix Fontein <felix@fontein.de>
2023-02-24 11:04:27 +01:00
|
|
|
extends_documentation_fragment:
|
2023-04-16 13:47:47 +02:00
|
|
|
- ansible.builtin.url
|
|
|
|
- community.general.attributes
|
|
|
|
- community.general.rundeck
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Create or update a rundeck ACL policy in project Ansible
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.rundeck_acl_policy:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: "Project_01"
|
|
|
|
api_version: 18
|
|
|
|
url: "https://rundeck.example.org"
|
|
|
|
token: "mytoken"
|
|
|
|
state: present
|
|
|
|
project: "Ansible"
|
|
|
|
policy:
|
|
|
|
description: "my policy"
|
|
|
|
context:
|
|
|
|
application: rundeck
|
|
|
|
for:
|
|
|
|
project:
|
|
|
|
- allow: read
|
|
|
|
by:
|
|
|
|
group: "build"
|
|
|
|
|
|
|
|
- name: Remove a rundeck system policy
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.rundeck_acl_policy:
|
2023-04-16 13:47:47 +02:00
|
|
|
name: "Project_01"
|
2020-03-09 10:11:07 +01:00
|
|
|
url: "https://rundeck.example.org"
|
|
|
|
token: "mytoken"
|
|
|
|
state: absent
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
rundeck_response:
|
|
|
|
description: Rundeck response when a failure occurs.
|
|
|
|
returned: failed
|
|
|
|
type: str
|
|
|
|
before:
|
|
|
|
description: Dictionary containing ACL policy informations before modification.
|
|
|
|
returned: success
|
|
|
|
type: dict
|
|
|
|
after:
|
|
|
|
description: Dictionary containing ACL policy informations after modification.
|
|
|
|
returned: success
|
|
|
|
type: dict
|
|
|
|
'''
|
|
|
|
|
|
|
|
# import module snippets
|
2020-07-16 23:17:18 +02:00
|
|
|
import re
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2023-04-16 13:47:47 +02:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible_collections.community.general.plugins.module_utils.rundeck import (
|
|
|
|
api_argument_spec,
|
|
|
|
api_request,
|
|
|
|
)
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
class RundeckACLManager:
|
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
|
|
|
|
|
|
|
def get_acl(self):
|
2023-04-16 13:47:47 +02:00
|
|
|
resp, info = api_request(
|
|
|
|
module=self.module,
|
|
|
|
endpoint="system/acl/%s.aclpolicy" % self.module.params["name"],
|
|
|
|
)
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
return resp
|
|
|
|
|
|
|
|
def create_or_update_acl(self):
|
|
|
|
facts = self.get_acl()
|
|
|
|
if facts is None:
|
|
|
|
# If in check mode don't create project, simulate a fake project creation
|
|
|
|
if self.module.check_mode:
|
|
|
|
self.module.exit_json(changed=True, before={}, after=self.module.params["policy"])
|
|
|
|
|
2023-04-16 13:47:47 +02:00
|
|
|
resp, info = api_request(
|
|
|
|
module=self.module,
|
|
|
|
endpoint="system/acl/%s.aclpolicy" % self.module.params["name"],
|
|
|
|
method="POST",
|
|
|
|
data={"contents": self.module.params["policy"]},
|
|
|
|
)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if info["status"] == 201:
|
|
|
|
self.module.exit_json(changed=True, before={}, after=self.get_acl())
|
|
|
|
elif info["status"] == 400:
|
|
|
|
self.module.fail_json(msg="Unable to validate acl %s. Please ensure it's a valid ACL" %
|
|
|
|
self.module.params["name"])
|
|
|
|
elif info["status"] == 409:
|
|
|
|
self.module.fail_json(msg="ACL %s already exists" % self.module.params["name"])
|
|
|
|
else:
|
|
|
|
self.module.fail_json(msg="Unhandled HTTP status %d, please report the bug" % info["status"],
|
|
|
|
before={}, after=self.get_acl())
|
|
|
|
else:
|
|
|
|
if facts["contents"] == self.module.params["policy"]:
|
|
|
|
self.module.exit_json(changed=False, before=facts, after=facts)
|
|
|
|
|
|
|
|
if self.module.check_mode:
|
|
|
|
self.module.exit_json(changed=True, before=facts, after=facts)
|
|
|
|
|
2023-04-16 13:47:47 +02:00
|
|
|
resp, info = api_request(
|
|
|
|
module=self.module,
|
|
|
|
endpoint="system/acl/%s.aclpolicy" % self.module.params["name"],
|
|
|
|
method="PUT",
|
|
|
|
data={"contents": self.module.params["policy"]},
|
|
|
|
)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if info["status"] == 200:
|
|
|
|
self.module.exit_json(changed=True, before=facts, after=self.get_acl())
|
|
|
|
elif info["status"] == 400:
|
|
|
|
self.module.fail_json(msg="Unable to validate acl %s. Please ensure it's a valid ACL" %
|
|
|
|
self.module.params["name"])
|
|
|
|
elif info["status"] == 404:
|
|
|
|
self.module.fail_json(msg="ACL %s doesn't exists. Cannot update." % self.module.params["name"])
|
|
|
|
|
|
|
|
def remove_acl(self):
|
|
|
|
facts = self.get_acl()
|
2023-04-16 13:47:47 +02:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
if facts is None:
|
|
|
|
self.module.exit_json(changed=False, before={}, after={})
|
|
|
|
else:
|
|
|
|
# If not in check mode, remove the project
|
|
|
|
if not self.module.check_mode:
|
2023-04-16 13:47:47 +02:00
|
|
|
api_request(
|
|
|
|
module=self.module,
|
|
|
|
endpoint="system/acl/%s.aclpolicy" % self.module.params["name"],
|
|
|
|
method="DELETE",
|
|
|
|
)
|
|
|
|
|
|
|
|
self.module.exit_json(changed=True, before=facts, after={})
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
# Also allow the user to set values for fetch_url
|
2023-04-16 13:47:47 +02:00
|
|
|
argument_spec = api_argument_spec()
|
2020-03-09 10:11:07 +01:00
|
|
|
argument_spec.update(dict(
|
|
|
|
state=dict(type='str', choices=['present', 'absent'], default='present'),
|
|
|
|
name=dict(required=True, type='str'),
|
|
|
|
policy=dict(type='str'),
|
|
|
|
project=dict(type='str'),
|
|
|
|
))
|
|
|
|
|
2023-04-16 13:47:47 +02:00
|
|
|
argument_spec['api_token']['aliases'] = ['token']
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=argument_spec,
|
|
|
|
required_if=[
|
|
|
|
['state', 'present', ['policy']],
|
|
|
|
],
|
2023-04-16 13:47:47 +02:00
|
|
|
supports_check_mode=True,
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
|
2020-07-16 23:17:18 +02:00
|
|
|
if not bool(re.match("[a-zA-Z0-9,.+_-]+", module.params["name"])):
|
|
|
|
module.fail_json(msg="Name contains forbidden characters. The policy can contain the characters: a-zA-Z0-9,.+_-")
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
if module.params["api_version"] < 14:
|
|
|
|
module.fail_json(msg="API version should be at least 14")
|
|
|
|
|
|
|
|
rundeck = RundeckACLManager(module)
|
|
|
|
if module.params['state'] == 'present':
|
|
|
|
rundeck.create_or_update_acl()
|
|
|
|
elif module.params['state'] == 'absent':
|
|
|
|
rundeck.remove_acl()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|