Debian Codegraph @

ansible_collections/community/general/tests/integration/targets/consul/tasks/consul_role.yml

raw
1 ---
2 # Copyright (c) Ansible Project
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 - name: Create a policy with rules
7 consul_policy:
8 name: foo-access-for-role
9 rules: |
10 key "foo" {
11 policy = "read"
12 }
13 key "private/foo" {
14 policy = "deny"
15 }
16 register: policy_result
17
18 - name: Create another policy with rules
19 consul_policy:
20 name: bar-access-for-role
21 rules: |
22 key "bar" {
23 policy = "read"
24 }
25 key "private/bar" {
26 policy = "deny"
27 }
28 register: policy_result
29
30 - name: Create a role with policy
31 consul_role:
32 name: foo-role-with-policy
33 policies:
34 - name: "foo-access-for-role"
35 register: result
36
37 - assert:
38 that:
39 - result is changed
40 - result.role.Name == 'foo-role-with-policy'
41 - result.operation == 'create'
42
43 - name: Update policy description, in check mode
44 consul_role:
45 name: foo-role-with-policy
46 description: "Testing updating description"
47 check_mode: true
48 register: result
49
50 - assert:
51 that:
52 - result is changed
53 - result.role.Description == "Testing updating description"
54 - result.role.Policies.0.Name == 'foo-access-for-role'
55 - result.operation == 'update'
56
57 - name: Update policy to add the description
58 consul_role:
59 name: foo-role-with-policy
60 description: "Role for testing policies"
61 register: result
62
63 - assert:
64 that:
65 - result is changed
66 - result.role.Description == "Role for testing policies"
67 - result.role.Policies.0.Name == 'foo-access-for-role'
68 - result.operation == 'update'
69
70 - name: Update the role with another policy, also testing leaving description blank
71 consul_role:
72 name: foo-role-with-policy
73 policies:
74 - name: "foo-access-for-role"
75 - name: "bar-access-for-role"
76 register: result
77
78 - assert:
79 that:
80 - result is changed
81 - result.role.Policies.0.Name == 'foo-access-for-role'
82 - result.role.Policies.1.Name == 'bar-access-for-role'
83 - result.role.Description == "Role for testing policies"
84 - result.operation == 'update'
85
86 - name: Create a role with service identity
87 consul_role:
88 name: role-with-service-identity
89 service_identities:
90 - name: web
91 datacenters:
92 - dc1
93 register: result
94
95 - assert:
96 that:
97 - result is changed
98 - result.role.ServiceIdentities.0.ServiceName == "web"
99 - result.role.ServiceIdentities.0.Datacenters.0 == "dc1"
100
101 - name: Update the role with service identity in check mode
102 consul_role:
103 name: role-with-service-identity
104 service_identities:
105 - name: web
106 datacenters:
107 - dc2
108 register: result
109 check_mode: true
110
111 - assert:
112 that:
113 - result is changed
114 - result.role.ServiceIdentities.0.ServiceName == "web"
115 - result.role.ServiceIdentities.0.Datacenters.0 == "dc2"
116
117 - name: Update the role with service identity to add a policy, leaving the service id unchanged
118 consul_role:
119 name: role-with-service-identity
120 policies:
121 - name: "foo-access-for-role"
122 register: result
123
124 - assert:
125 that:
126 - result is changed
127 - result.role.ServiceIdentities.0.ServiceName == "web"
128 - result.role.ServiceIdentities.0.Datacenters.0 == "dc1"
129 - result.role.Policies.0.Name == 'foo-access-for-role'
130
131 - name: Update the role with service identity to remove the policies
132 consul_role:
133 name: role-with-service-identity
134 policies: []
135 register: result
136
137 - assert:
138 that:
139 - result is changed
140 - result.role.ServiceIdentities.0.ServiceName == "web"
141 - result.role.ServiceIdentities.0.Datacenters.0 == "dc1"
142 - result.role.Policies is not defined
143
144 - name: Update the role with service identity to remove the node identities, in check mode
145 consul_role:
146 name: role-with-service-identity
147 node_identities: []
148 register: result
149 check_mode: true
150
151 - assert:
152 that:
153 - result is changed
154 - result.role.ServiceIdentities.0.ServiceName == "web"
155 - result.role.ServiceIdentities.0.Datacenters.0 == "dc1"
156 - result.role.Policies is not defined
157 - result.role.NodeIdentities == [] # in check mode the cleared field is returned as an empty array
158
159 - name: Update the role with service identity to remove the service identities
160 consul_role:
161 name: role-with-service-identity
162 service_identities: []
163 register: result
164
165 - assert:
166 that:
167 - result is changed
168 - result.role.ServiceIdentities is not defined # in normal mode the dictionary is removed from the result
169 - result.role.Policies is not defined
170
171 - name: Create a role with node identity
172 consul_role:
173 name: role-with-node-identity
174 node_identities:
175 - name: node-1
176 datacenter: dc2
177 register: result
178
179 - assert:
180 that:
181 - result is changed
182 - result.role.NodeIdentities.0.NodeName == "node-1"
183 - result.role.NodeIdentities.0.Datacenter == "dc2"
184
185 - name: Remove the last role
186 consul_role:
187 name: role-with-node-identity
188 state: absent
189 register: result
190
191 - assert:
192 that:
193 - result is changed
194 - result.operation == 'remove'