Skip to content

Commit 1bba733

Browse files
committed
state as attribute & updating readme
1 parent bc48ac6 commit 1bba733

File tree

7 files changed

+86
-57
lines changed

7 files changed

+86
-57
lines changed

roles/pulp_content_guard_rbac/README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ Role variables
99
* `pulp_url`: URL of Pulp server. Default is `https://localhost:8080`
1010
* `pulp_admin_username`: Username used to access Pulp server. Default is `admin`
1111
* `pulp_admin_password`: Password used to access Pulp server. Default is unset
12-
* `pulp_content_guards_rbac_present`: List of groups to be present. Default is an empty list.
13-
* `pulp_content_guards_rbac_absent`: List of groups to be absent. Default is an empty list.
12+
* `pulp_content_guards_rbac`: List of groups to create/update/delete. Default is an empty list. Each item is a dict containing:
13+
* `name` (Required)
14+
* `state` (default is `present`. Setting this value to `absent` will delete the content guard if it exists)
15+
* `download_groups` (list of groups to to be added to this content guard with the download role)
1416

15-
Note: The groups associated with specified content guards are evauluated against the user's current list of content guards, and their respective groups, returned from the Pulp server API. Removing a group from the list of groups defined in `pulp_content_guards_rbac_present[*].download_groups` will result in the group being removed from that content guard, and adding a group will result in the group being added to that content guard. Adding an empty `download_groups:` for a content guard will result in all groups being removed for that content guard.
17+
18+
19+
Note: The groups associated with specified content guards are evauluated against the user's current list of content guards, and their respective groups, returned from the Pulp server API. Removing a group from the list of groups defined in `pulp_content_guards_rbac[*].download_groups` will result in the group being removed from that content guard, and adding a group will result in the group being added to that content guard. Adding an empty `download_groups:` for a content guard will result in all groups being removed for that content guard.
1620

1721
Example playbook
1822
----------------
@@ -32,10 +36,13 @@ Example playbook
3236
download_groups:
3337
- existing-group-1
3438
- existing-group-2
39+
state: present
3540
- name: content-guard-2
3641
download_groups:
3742
- existing-group-3
38-
pulp_content_guards_rbac_absent:
39-
- content-guard-3
40-
- content-guard-4
43+
state: present
44+
- name: content-guard-3
45+
state: present
46+
- name: content-guard-4
47+
state: absent
4148
```

roles/pulp_content_guard_rbac/tasks/main.yml

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

14-
- name: Set fact cgnames
14+
- name: Set fact remove_rbac_cg
1515
set_fact:
16-
cgnames: "{{ (cgnames | default([])) + [item.name] }}"
17-
with_items: "{{ rbac_cg_list_result.json.results }}"
16+
remove_rbac_cg: "{{ (remove_rbac_cg | default([])) + [item.name] }}"
17+
when: item.state | default('present') == 'absent'
18+
with_items: "{{ pulp_content_guards_rbac }}"
1819

1920
- name: Create RBAC content guards
21+
vars:
22+
cgnames: "{{ rbac_cg_list_result.json.results | map(attribute='name') | list }}"
2023
uri:
2124
url: "{{ pulp_rbac_cg_url }}"
2225
user: "{{ pulp_admin_username }}"
@@ -27,19 +30,22 @@
2730
body:
2831
name: "{{ item.name }}"
2932
body_format: form-urlencoded
30-
loop: "{{ pulp_content_guards_rbac_present | default([], true) }}"
33+
loop: "{{ pulp_content_guards_rbac | default([], true) }}"
3134
loop_control:
3235
label: "{{ item.name }}"
33-
no_log: true
36+
# no_log: true
3437
register: result
35-
when: item.name not in cgnames
38+
when:
39+
- item.name not in cgnames
40+
- item.state | default('present') != 'absent'
3641
changed_when: result.status == 201
3742

3843
- name: Add or remove group(s) from content guard
3944
include_tasks: rbac_group/add_or_remove_groups.yml
40-
loop: "{{ pulp_content_guards_rbac_present | default([], true) }}"
45+
loop: "{{ pulp_content_guards_rbac | default([], true) }}"
4146
loop_control:
4247
loop_var: content_guard
48+
when: content_guard.state | default('present') != 'absent'
4349

4450
- name: Initialise hrefs
4551
set_fact:
@@ -48,7 +54,7 @@
4854
- name: Set fact hrefs
4955
set_fact:
5056
hrefs: "{{ (hrefs | default([])) + [item.pulp_href] }}"
51-
when: item.name in (pulp_content_guards_rbac_absent | default([], true))
57+
when: item.name in (remove_rbac_cg | default([]))
5258
with_items: "{{ rbac_cg_list_result.json.results }}"
5359

5460
- name: Delete RBAC content guards
@@ -61,8 +67,6 @@
6167
status_code: 204
6268
body_format: form-urlencoded
6369
loop: "{{ hrefs | default([]) }}"
64-
loop_control:
65-
label: "{{ item }}"
6670
no_log: true
6771
register: result
6872
changed_when: result.status == 204

roles/pulp_group/README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ Role variables
1111
* `pulp_url`: URL of Pulp server. Default is `https://localhost:8080`
1212
* `pulp_admin_username`: Username used to access Pulp server. Default is `admin`
1313
* `pulp_admin_password`: Password used to access Pulp server. Default is unset
14-
* `pulp_groups_present`: List of groups to be present. Default is an empty list.
15-
* `pulp_groups_absent`: List of groups to be absent. Default is an empty list.
14+
* `pulp_groups`: List of groups to be created/updated/deleted. Default is an empty list. Each item is a dict containing:
15+
* `name` (Required)
16+
* `state` (default is `present`. Setting this value to `absent` will delete the use if it exists)
1617

1718

1819

@@ -30,8 +31,10 @@ Example playbook
3031
pulp_admin_username: admin
3132
pulp_admin_password: "{{ secrets_pulp_admin_password }}"
3233
pulp_groups_present:
33-
- example-group-1
34-
- example-group-2
35-
pulp_groups_absent:
36-
- example-group-3
34+
- name: example-group-1
35+
state: present
36+
- name: example-group-2
37+
state: present
38+
- name: example-group-3
39+
state: absent
3740
```

roles/pulp_group/tasks/main.yml

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111
no_log: true
1212
register: groups_list_result
1313

14-
- name: Set fact groupnames
14+
- name: Set fact remove_groups
1515
set_fact:
16-
groupnames: "{{ (groupnames | default([])) + [item.name] }}"
17-
with_items: "{{ groups_list_result.json.results }}"
16+
remove_groups: "{{ (remove_groups | default([])) + [item.name] }}"
17+
when: item.state | default('present') == 'absent'
18+
with_items: "{{ pulp_groups }}"
1819

1920
- name: Create groups
21+
vars:
22+
groupnames: "{{ groups_list_result.json.results | map(attribute='name') | list }}"
2023
uri:
2124
url: "{{ pulp_group_url }}"
2225
user: "{{ pulp_admin_username }}"
@@ -25,13 +28,14 @@
2528
method: POST
2629
status_code: 201
2730
body:
28-
name: "{{ item }}"
31+
name: "{{ item.name }}"
2932
body_format: form-urlencoded
30-
loop: "{{ pulp_groups_present | default([], true) }}"
31-
no_log: true
33+
loop: "{{ pulp_groups | default([], true) }}"
34+
# no_log: true
3235
register: result
3336
when:
34-
- item not in groupnames
37+
- item.name not in groupnames
38+
- item.state | default('present') != 'absent'
3539
changed_when: result.status == 201
3640

3741
- name: Initialise hrefs
@@ -40,8 +44,8 @@
4044

4145
- name: Set fact hrefs
4246
set_fact:
43-
hrefs: "{{ (hrefs|default([])) + [item.pulp_href] }}"
44-
when: item.name in (pulp_groups_absent | default([], true))
47+
hrefs: "{{ (hrefs | default([])) + [item.pulp_href] }}"
48+
when: item.name in (remove_groups | default([]))
4549
with_items: "{{ groups_list_result.json.results }}"
4650

4751
- name: Remove groups
@@ -54,8 +58,6 @@
5458
status_code: 204
5559
body_format: form-urlencoded
5660
loop: "{{ hrefs | default([]) }}"
57-
loop_control:
58-
label: "{{ item }}"
5961
no_log: true
6062
register: result
6163
changed_when: result.status == 204

roles/pulp_user/README.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ Role variables
99
* `pulp_url`: URL of Pulp server. Default is `https://localhost:8080`
1010
* `pulp_admin_username`: Username used to access Pulp server. Default is `admin`
1111
* `pulp_admin_password`: Password used to access Pulp server. Default is unset
12-
* `pulp_users_present`: List of users to be present. Default is an empty list.
13-
* `pulp_users_absent`: List of users to be absent. Default is an empty list.
12+
* `pulp_users`: List of users to create/update/delete. Default is an empty list. Each item is a dict containing:
13+
* `username` (Required)
14+
* `password`
15+
* `first_name`
16+
* `last_name`
17+
* `email`
18+
* `is_staff`
19+
* `is_active`
20+
* `state` (default is `present`. Setting this value to `absent` will delete the use if it exists)
21+
* `groups` (list of groups to add the user to)
1422

15-
Note: User groups are evaluated against the user's current list of groups returned from the Pulp server API. Removing a group from the list of groups defined in `pulp_users_present[*].groups` will result in the user being removed from that group, and adding a group will result in the user being added to that group. Adding an empty `groups:` for a user will result in that user being removed from all groups.
23+
Note: User groups are evaluated against the user's current list of groups returned from the Pulp server API. Removing a group from the list of groups defined in `pulp_users[*].groups` will result in the user being removed from that group, and adding a group will result in the user being added to that group. Adding an empty `groups:` for a user will result in that user being removed from all groups.
1624

1725
Example playbook
1826
----------------
@@ -27,16 +35,17 @@ Example playbook
2735
pulp_url: https://pulp.example.com
2836
pulp_admin_username: admin
2937
pulp_admin_password: "{{ secrets_pulp_admin_password }}"
30-
pulp_users_present:
38+
pulp_users:
3139
- username: example-user-1
3240
password: correct horse battery staple
3341
groups:
3442
- existing.container.namespace.consumers.one
3543
- existing.container.namespace.consumers.two
44+
state: present
3645
- username: example-user-2
37-
password: germany ansible rain farmer
3846
groups:
3947
- existing.container.namespace.consumers.one
40-
pulp_users_absent:
41-
- example-user-3
48+
state: present
49+
- username: example-user-3
50+
state: absent
4251
```

roles/pulp_user/defaults/main.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ pulp_admin_username: admin
44
pulp_admin_password:
55
pulp_validate_certs: true
66

7-
pulp_users_present: []
8-
pulp_users_absent: []
7+
pulp_users: []

roles/pulp_user/tasks/main.yml

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
2+
23
- name: Get user list
34
uri:
45
url: "{{ pulp_user_url }}"
@@ -10,14 +11,15 @@
1011
no_log: true
1112
register: users_list_result
1213

13-
- name: Set fact usernames
14+
- name: Set fact remove_users
1415
set_fact:
15-
usernames: "{{ (usernames | default([])) + [item.username] }}"
16-
with_items: "{{ users_list_result.json.results }}"
16+
remove_users: "{{ (remove_users | default([])) + [item.username] }}"
17+
when: item.state | default('present') == 'absent'
18+
with_items: "{{ pulp_users }}"
1719

1820
- name: Create users
1921
vars:
20-
pulp_users_present_nn: "{{ pulp_users_present if pulp_users_present is iterable else [] }}"
22+
usernames: "{{ users_list_result.json.results | map(attribute='username') | list }}"
2123
uri:
2224
url: "{{ pulp_user_url }}"
2325
user: "{{ pulp_admin_username }}"
@@ -34,17 +36,19 @@
3436
is_staff: "{{ item.is_staff | default(omit) }}"
3537
is_active: "{{ item.is_active | default(omit) }}"
3638
body_format: form-urlencoded
37-
loop: "{{ pulp_users_present | default([], true) }}"
39+
loop: "{{ pulp_users | default([], true) }}"
3840
loop_control:
3941
label: "{{ item.username }}"
40-
no_log: true
42+
# no_log: true
4143
register: result
42-
when: item.username not in usernames
44+
when:
45+
- item.username not in usernames
46+
- item.state | default('present') != 'absent'
4347
changed_when: result.status == 201
4448

4549
- name: Update existing users
4650
vars:
47-
pulp_users_present_nn: "{{ pulp_users_present if pulp_users_present is iterable else [] }}"
51+
usernames: "{{ users_list_result.json.results | map(attribute='username') | list }}"
4852
url_query: "[?username=='{{ item.username }}'].pulp_href"
4953
uri:
5054
url: "{{ pulp_url }}{{ users_list_result.json.results | json_query(url_query) | first }}"
@@ -61,12 +65,14 @@
6165
is_staff: "{{ item.is_staff | default(omit) }}"
6266
is_active: "{{ item.is_active | default(omit) }}"
6367
body_format: form-urlencoded
64-
loop: "{{ pulp_users_present | default([], true) }}"
68+
loop: "{{ pulp_users | default([], true) }}"
6569
loop_control:
6670
label: "{{ item.username }}"
6771
no_log: true
6872
register: result
69-
when: item.username in usernames
73+
when:
74+
- item.username in usernames
75+
- item.state | default('present') != 'absent'
7076
changed_when:
7177
# The pulp API currently does not report when a change is made, so we must
7278
# manually check
@@ -75,8 +81,9 @@
7581

7682
- name: Add or remove user from group(s)
7783
include_tasks: user_groups/add_or_remove_users.yml
78-
# Noop if pulp_users_present is defined but empty
79-
loop: "{{ pulp_users_present | default([], true) }}"
84+
# Noop if pulp_users is defined but empty
85+
loop: "{{ pulp_users | default([], true) }}"
86+
when: (user.state | default('present')) != 'absent'
8087
loop_control:
8188
loop_var: user
8289

@@ -87,7 +94,7 @@
8794
- name: Set fact hrefs
8895
set_fact:
8996
hrefs: "{{ (hrefs | default([])) + [item.pulp_href] }}"
90-
when: item.username in (pulp_users_absent | default([], true))
97+
when: item.username in (remove_users | default([]))
9198
with_items: "{{ users_list_result.json.results }}"
9299

93100
- name: Delete users
@@ -100,8 +107,6 @@
100107
status_code: 204
101108
body_format: form-urlencoded
102109
loop: "{{ hrefs | default([]) }}"
103-
loop_control:
104-
label: "{{ item }}"
105110
no_log: true
106111
register: result
107112
changed_when: result.status == 204

0 commit comments

Comments
 (0)