| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2022, Håkon Lerring |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: consul_policy |
| 11 |
short_description: Manipulate Consul policies |
| 12 |
version_added: 7.2.0 |
| 13 |
description: |
| 14 |
- Allows the addition, modification and deletion of policies in a Consul cluster using the agent. For more details on using |
| 15 |
and configuring ACLs, see U(https://www.consul.io/docs/guides/acl.html). |
| 16 |
author: |
| 17 |
- Håkon Lerring (@Hakon) |
| 18 |
extends_documentation_fragment: |
| 19 |
- community.general._consul |
| 20 |
- community.general._consul.actiongroup_consul |
| 21 |
- community.general._consul.token |
| 22 |
- community.general._attributes |
| 23 |
attributes: |
| 24 |
check_mode: |
| 25 |
support: full |
| 26 |
version_added: 8.3.0 |
| 27 |
diff_mode: |
| 28 |
support: partial |
| 29 |
version_added: 8.3.0 |
| 30 |
details: |
| 31 |
- In check mode the diff misses operational attributes. |
| 32 |
action_group: |
| 33 |
version_added: 8.3.0 |
| 34 |
options: |
| 35 |
state: |
| 36 |
description: |
| 37 |
- Whether the policy should be present or absent. |
| 38 |
choices: ['present', 'absent'] |
| 39 |
default: present |
| 40 |
type: str |
| 41 |
valid_datacenters: |
| 42 |
description: |
| 43 |
- Valid datacenters for the policy. All if list is empty. |
| 44 |
type: list |
| 45 |
elements: str |
| 46 |
name: |
| 47 |
description: |
| 48 |
- The name that should be associated with the policy, this is opaque to Consul. |
| 49 |
required: true |
| 50 |
type: str |
| 51 |
description: |
| 52 |
description: |
| 53 |
- Description of the policy. |
| 54 |
type: str |
| 55 |
rules: |
| 56 |
type: str |
| 57 |
description: |
| 58 |
- Rule document that should be associated with the current policy. |
| 59 |
""" |
| 60 |
|
| 61 |
EXAMPLES = r""" |
| 62 |
- name: Create a policy with rules |
| 63 |
community.general.consul_policy: |
| 64 |
host: consul1.example.com |
| 65 |
token: some_management_acl |
| 66 |
name: foo-access |
| 67 |
rules: | |
| 68 |
key "foo" { |
| 69 |
policy = "read" |
| 70 |
} |
| 71 |
key "private/foo" { |
| 72 |
policy = "deny" |
| 73 |
} |
| 74 |
|
| 75 |
- name: Update the rules associated to a policy |
| 76 |
community.general.consul_policy: |
| 77 |
host: consul1.example.com |
| 78 |
token: some_management_acl |
| 79 |
name: foo-access |
| 80 |
rules: | |
| 81 |
key "foo" { |
| 82 |
policy = "read" |
| 83 |
} |
| 84 |
key "private/foo" { |
| 85 |
policy = "deny" |
| 86 |
} |
| 87 |
event "bbq" { |
| 88 |
policy = "write" |
| 89 |
} |
| 90 |
|
| 91 |
- name: Remove a policy |
| 92 |
community.general.consul_policy: |
| 93 |
host: consul1.example.com |
| 94 |
token: some_management_acl |
| 95 |
name: foo-access |
| 96 |
state: absent |
| 97 |
""" |
| 98 |
|
| 99 |
RETURN = r""" |
| 100 |
policy: |
| 101 |
description: The policy as returned by the Consul HTTP API. |
| 102 |
returned: always |
| 103 |
type: dict |
| 104 |
sample: |
| 105 |
CreateIndex: 632 |
| 106 |
Description: Testing |
| 107 |
Hash: rj5PeDHddHslkpW7Ij4OD6N4bbSXiecXFmiw2SYXg2A= |
| 108 |
Name: foo-access |
| 109 |
Rules: |- |
| 110 |
key "foo" { |
| 111 |
policy = "read" |
| 112 |
} |
| 113 |
key "private/foo" { |
| 114 |
policy = "deny" |
| 115 |
} |
| 116 |
operation: |
| 117 |
description: The operation performed. |
| 118 |
returned: changed |
| 119 |
type: str |
| 120 |
sample: update |
| 121 |
""" |
| 122 |
|
| 123 |
from ansible.module_utils.basic import AnsibleModule |
| 124 |
|
| 125 |
from ansible_collections.community.general.plugins.module_utils._consul import ( |
| 126 |
AUTH_ARGUMENTS_SPEC, |
| 127 |
OPERATION_READ, |
| 128 |
_ConsulModule, |
| 129 |
) |
| 130 |
|
| 131 |
_ARGUMENT_SPEC = { |
| 132 |
"name": dict(required=True), |
| 133 |
"description": dict(type="str"), |
| 134 |
"rules": dict(type="str"), |
| 135 |
"valid_datacenters": dict(type="list", elements="str"), |
| 136 |
"state": dict(default="present", choices=["present", "absent"]), |
| 137 |
} |
| 138 |
_ARGUMENT_SPEC.update(AUTH_ARGUMENTS_SPEC) |
| 139 |
|
| 140 |
|
| 141 |
class ConsulPolicyModule(_ConsulModule): |
| 142 |
api_endpoint = "acl/policy" |
| 143 |
result_key = "policy" |
| 144 |
unique_identifiers = ["id"] |
| 145 |
|
| 146 |
def endpoint_url(self, operation, identifier=None): |
| 147 |
if operation == OPERATION_READ: |
| 148 |
return [self.api_endpoint, "name", self.params["name"]] |
| 149 |
return super().endpoint_url(operation, identifier) |
| 150 |
|
| 151 |
|
| 152 |
def main(): |
| 153 |
module = AnsibleModule( |
| 154 |
_ARGUMENT_SPEC, |
| 155 |
supports_check_mode=True, |
| 156 |
) |
| 157 |
consul_module = ConsulPolicyModule(module) |
| 158 |
consul_module.execute() |
| 159 |
|
| 160 |
|
| 161 |
if __name__ == "__main__": |
| 162 |
main() |