Skip to content

Commit c92ae90

Browse files
committed
Adding support for arbitrary roles
1 parent 707bd1e commit c92ae90

File tree

19 files changed

+209
-169
lines changed

19 files changed

+209
-169
lines changed

roles/pulp_content_guard/README.md

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,20 @@ Role variables
1515
* `name` (Required)
1616
* `description`
1717
* `ca_certificate`
18-
* `state` (default is `present`. Setting this value to `absent` will delete the content guard if it exists)
18+
* `state` (Default is `present`. Setting this value to `absent` will delete the content guard if it exists)
1919
* `pulp_content_guard_rbac`: List of groups to create/update/delete. Default is an empty list. Each item is a dict containing:
2020
* `name` (Required)
21-
* `download_groups` (list of groups to to be added to this content guard with the download role)
21+
* `roles` List of dict containing:
22+
* `role` (role name)
23+
* `groups` List of groups to be assigned the role
2224
* `state` (default is `present`. Setting this value to `absent` will delete the content guard if it exists)
2325

26+
Note: groups assigned roles are evaluated against the content guard's current list of roles returned from the Pulp server API. Removing a group from the list of groups defined under any role in `pulp_content_guard_rbac[*].roles` will result in the group being removed, and adding a group will result in it being added. Adding an empty `groups:` for a role will result in all groups being removed from that role.
2427

2528
Example playbook
2629
----------------
2730

28-
```
31+
```yaml
2932
---
3033
- name: Create Pulp content guards
3134
any_errors_fatal: True
@@ -49,19 +52,20 @@ Example playbook
4952
pulp_username: admin
5053
pulp_password: "{{ secrets_pulp_admin_password }}"
5154
pulp_content_guard_rbac:
52-
- name: alex-test-rbac_cg-1
53-
description: test-description-edited
54-
download_groups:
55-
- alex-test-group-1
56-
- alex-test-group-2
55+
- name: test_rbac_cg_1
56+
description: test content guard number 1
57+
roles:
58+
- role: core.rbaccontentguard_downloader
59+
groups:
60+
- role: core.rbaccontentguard_viewer
5761
state: present
58-
- name: alex-test-rbac_cg-2
59-
description: test-description2-edited
60-
download_groups:
61-
- alex-test-group-2
62-
- name: alex-test-rbac_cg-3
63-
description: test-description3-edited
64-
download_groups:
65-
- alex-test-group-1
62+
- name: test_rbac_cg_2
6663
state: absent
64+
- name: test_rbac_cg_3
65+
description: test content guard number 3
66+
roles:
67+
- role: core.rbaccontentguard_viewer
68+
groups:
69+
- test_group_1
70+
- test_group_2
6771
```

roles/pulp_content_guard/tasks/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
label: "{{ item.name }}"
1515

1616
- name: Ensure RBAC cert guards exist
17-
include_tasks: rbac/rbac.yml
17+
import_tasks: rbac/rbac.yml
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
3+
- name: Initialise facts
4+
set_fact:
5+
new_roles: []
6+
current_roles: []
7+
8+
- name: Get RBAC content guard list
9+
uri:
10+
url: "{{ pulp_rbac_cg_url }}"
11+
user: "{{ pulp_username }}"
12+
password: "{{ pulp_password }}"
13+
method: GET
14+
status_code: 200
15+
force_basic_auth: true
16+
no_log: true
17+
register: rbac_cg_list_result
18+
19+
- name: Get current roles associated with content guard
20+
vars:
21+
url_query: "[?name=='{{ content_guard.name }}'].pulp_href"
22+
set_fact:
23+
given_cg_href: "{{ rbac_cg_list_result.json.results | json_query(url_query) | first | default([]) }}"
24+
25+
- name: Get role list
26+
uri:
27+
url: "{{ pulp_url }}{{ given_cg_href }}list_roles/"
28+
user: "{{ pulp_username }}"
29+
password: "{{ pulp_password }}"
30+
method: GET
31+
status_code: 200
32+
force_basic_auth: true
33+
no_log: true
34+
register: role_list_result
35+
36+
- name: Remove unused roles
37+
vars:
38+
rolenames: "{{ content_guard.roles | default([]) | map(attribute='role') | list }}"
39+
url_query: "[?name=='{{ content_guard.name }}'].pulp_href"
40+
uri:
41+
url: "{{ pulp_url }}{{ rbac_cg_list_result.json.results | json_query(url_query) | first }}remove_role/"
42+
user: "{{ pulp_username }}"
43+
password: "{{ pulp_password }}"
44+
force_basic_auth: true
45+
method: POST
46+
status_code: 201
47+
body:
48+
role: "{{ item.role }}"
49+
groups: "{{ item.groups }}"
50+
body_format: form-urlencoded
51+
# debug:
52+
# msg: "{{ item.role }}"
53+
loop: "{{ role_list_result.json.roles }}"
54+
# no_log: true
55+
register: result
56+
when:
57+
- item.role not in rolenames
58+
- item.users == []
59+
changed_when: result.status == 201
60+
61+
- name: Loop on new roles
62+
include_tasks: add_or_remove_groups_from_role.yml
63+
loop: "{{ content_guard.roles | default([]) }}"
64+
loop_control:
65+
loop_var: rbac_cg_new_role

roles/pulp_content_guard/tasks/rbac/add_or_remove_groups.yml

Lines changed: 0 additions & 77 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
3+
- name: Set fact current groups
4+
vars:
5+
role_query: "[?role=='{{ rbac_cg_new_role.role }}'].groups"
6+
set_fact:
7+
current_groups: "{{ role_list_result.json.roles | json_query(role_query) | first | default([]) }}"
8+
9+
- name: Add new group to role
10+
vars:
11+
url_query: "[?name=='{{ content_guard.name }}'].pulp_href"
12+
uri:
13+
url: "{{ pulp_url }}{{ rbac_cg_list_result.json.results | json_query(url_query) | first }}add_role/"
14+
user: "{{ pulp_username }}"
15+
password: "{{ pulp_password }}"
16+
force_basic_auth: true
17+
method: POST
18+
status_code: 201
19+
body:
20+
role: "{{ rbac_cg_new_role.role }}"
21+
groups: "{{ item }}"
22+
body_format: form-urlencoded
23+
# no_log: true
24+
register: result
25+
loop: "{{ rbac_cg_new_role.groups | default([], true) }}"
26+
when: item not in current_groups
27+
changed_when: result.status == 201
28+
29+
- name: Remove old group from role
30+
vars:
31+
url_query: "[?name=='{{ content_guard.name }}'].pulp_href"
32+
uri:
33+
url: "{{ pulp_url }}{{ rbac_cg_list_result.json.results | json_query(url_query) | first }}remove_role/"
34+
user: "{{ pulp_username }}"
35+
password: "{{ pulp_password }}"
36+
force_basic_auth: true
37+
method: POST
38+
status_code: 201
39+
body:
40+
role: "{{ rbac_cg_new_role.role }}"
41+
groups: "{{ item }}"
42+
body_format: form-urlencoded
43+
# no_log: true
44+
register: result
45+
loop: "{{ current_groups }}"
46+
when: item not in (rbac_cg_new_role.groups | default([]))
47+
changed_when: result.status == 201

roles/pulp_content_guard/tasks/rbac/rbac.yml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
no_log: true
1212
register: rbac_cg_list_result
1313

14+
- name: Initialise remove_rbac_cg
15+
set_fact:
16+
remove_rbac_cg: []
17+
1418
- name: Set fact remove_rbac_cg
1519
set_fact:
1620
remove_rbac_cg: "{{ (remove_rbac_cg | default([])) + [item.name] }}"
@@ -32,11 +36,9 @@
3236
description: "{{ item.description | default(omit) }}"
3337
body_format: form-urlencoded
3438
loop: "{{ pulp_content_guard_rbac | default([], true) }}"
35-
loop_control:
36-
label: "{{ item.name }}"
3739
# no_log: true
3840
register: result
39-
when:
41+
when:
4042
- item.name not in rbaccgnames
4143
- item.state | default('present') != 'absent'
4244
changed_when: result.status == 201
@@ -58,17 +60,17 @@
5860
loop: "{{ pulp_content_guard_rbac | default([], true) }}"
5961
# no_log: true
6062
register: result
61-
when:
63+
when:
6264
- item.name in rbaccgnames
6365
- item.state | default('present') != 'absent'
64-
changed_when:
66+
changed_when:
6567
# The pulp API currently does not report when a change is made, so we must
6668
# manually check
67-
- result.json not in rbac_cg_list_result.json.results
68-
- result.status == 200
69+
- result.json not in rbac_cg_list_result.json.results
70+
- result.status == 200
6971

70-
- name: Add or remove group(s) from content guard
71-
include_tasks: add_or_remove_groups.yml
72+
- name: Add or remove group roles from content guard
73+
include_tasks: add_or_remove_group_roles.yml
7274
loop: "{{ pulp_content_guard_rbac | default([], true) }}"
7375
loop_control:
7476
loop_var: content_guard

roles/pulp_distribution/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Role variables
2525
Example playbook
2626
----------------
2727

28-
```
28+
```yaml
2929
---
3030
- name: Manage Pulp distributions
3131
any_errors_fatal: True

roles/pulp_django_user/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Note: User groups are evauluated against the user's current list of groups retur
2222
Example playbook
2323
----------------
2424

25-
```
25+
```yaml
2626
---
2727
- name: Create Pulp Django users
2828
gather_facts: True

roles/pulp_group/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ pulp_group
33

44
This role creates and deletes Pulp groups using the Pulp API.
55

6-
To add users to groups or add groups to content guards, use the pulp_user and pulp_content_guard_rbac roles respectively.
6+
To add users to groups or add groups to content guards, use the pulp_user and pulp_content_guard roles respectively.
77

88
Role variables
99
--------------
1010

1111
* `pulp_url`: URL of Pulp server. Default is `https://localhost:8080`
12-
* `pulp_admin_username`: Username used to access Pulp server. Default is `admin`
13-
* `pulp_admin_password`: Password used to access Pulp server. Default is unset
12+
* `pulp_username`: Username used to access Pulp server. Default is `admin`
13+
* `pulp_password`: Password used to access Pulp server. Default is unset
1414
* `pulp_groups`: List of groups to be created/updated/deleted. Default is an empty list. Each item is a dict containing:
1515
* `name` (Required)
1616
* `state` (default is `present`. Setting this value to `absent` will delete the use if it exists)
@@ -20,17 +20,17 @@ Role variables
2020
Example playbook
2121
----------------
2222

23-
```
23+
```yaml
2424
---
2525
- name: Create and delete groups
2626
gather_facts: True
2727
hosts: localhost
2828
roles:
2929
- role: stackhpc.pulp.pulp_group
3030
pulp_url: https://pulp.example.com
31-
pulp_admin_username: admin
32-
pulp_admin_password: "{{ secrets_pulp_admin_password }}"
33-
pulp_groups_present:
31+
pulp_username: admin
32+
pulp_password: "{{ secrets_pulp_admin_password }}"
33+
pulp_groups:
3434
- name: example-group-1
3535
state: present
3636
- name: example-group-2

roles/pulp_group/defaults/main.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
---
22
pulp_url: https://localhost:8080
3-
pulp_admin_username: admin
4-
pulp_admin_password:
3+
pulp_username: admin
4+
pulp_password:
55
pulp_validate_certs: true
66

7-
pulp_groups_present: []
8-
pulp_groups_absent: []
7+
pulp_groups: []

0 commit comments

Comments
 (0)