|
1 | 1 | --- |
| 2 | + |
2 | 3 | - name: Get user list |
3 | 4 | uri: |
4 | 5 | url: "{{ pulp_user_url }}" |
|
10 | 11 | no_log: true |
11 | 12 | register: users_list_result |
12 | 13 |
|
13 | | -- name: Set fact usernames |
| 14 | +- name: Set fact remove_users |
14 | 15 | 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 }}" |
17 | 19 |
|
18 | 20 | - name: Create users |
19 | 21 | 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 }}" |
21 | 23 | uri: |
22 | 24 | url: "{{ pulp_user_url }}" |
23 | 25 | user: "{{ pulp_admin_username }}" |
|
34 | 36 | is_staff: "{{ item.is_staff | default(omit) }}" |
35 | 37 | is_active: "{{ item.is_active | default(omit) }}" |
36 | 38 | body_format: form-urlencoded |
37 | | - loop: "{{ pulp_users_present | default([], true) }}" |
| 39 | + loop: "{{ pulp_users | default([], true) }}" |
38 | 40 | loop_control: |
39 | 41 | label: "{{ item.username }}" |
40 | | - no_log: true |
| 42 | + # no_log: true |
41 | 43 | register: result |
42 | | - when: item.username not in usernames |
| 44 | + when: |
| 45 | + - item.username not in usernames |
| 46 | + - item.state | default('present') != 'absent' |
43 | 47 | changed_when: result.status == 201 |
44 | 48 |
|
45 | 49 | - name: Update existing users |
46 | 50 | 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 }}" |
48 | 52 | url_query: "[?username=='{{ item.username }}'].pulp_href" |
49 | 53 | uri: |
50 | 54 | url: "{{ pulp_url }}{{ users_list_result.json.results | json_query(url_query) | first }}" |
|
61 | 65 | is_staff: "{{ item.is_staff | default(omit) }}" |
62 | 66 | is_active: "{{ item.is_active | default(omit) }}" |
63 | 67 | body_format: form-urlencoded |
64 | | - loop: "{{ pulp_users_present | default([], true) }}" |
| 68 | + loop: "{{ pulp_users | default([], true) }}" |
65 | 69 | loop_control: |
66 | 70 | label: "{{ item.username }}" |
67 | 71 | no_log: true |
68 | 72 | register: result |
69 | | - when: item.username in usernames |
| 73 | + when: |
| 74 | + - item.username in usernames |
| 75 | + - item.state | default('present') != 'absent' |
70 | 76 | changed_when: |
71 | 77 | # The pulp API currently does not report when a change is made, so we must |
72 | 78 | # manually check |
|
75 | 81 |
|
76 | 82 | - name: Add or remove user from group(s) |
77 | 83 | 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' |
80 | 87 | loop_control: |
81 | 88 | loop_var: user |
82 | 89 |
|
|
87 | 94 | - name: Set fact hrefs |
88 | 95 | set_fact: |
89 | 96 | hrefs: "{{ (hrefs | default([])) + [item.pulp_href] }}" |
90 | | - when: item.username in (pulp_users_absent | default([], true)) |
| 97 | + when: item.username in (remove_users | default([])) |
91 | 98 | with_items: "{{ users_list_result.json.results }}" |
92 | 99 |
|
93 | 100 | - name: Delete users |
|
100 | 107 | status_code: 204 |
101 | 108 | body_format: form-urlencoded |
102 | 109 | loop: "{{ hrefs | default([]) }}" |
103 | | - loop_control: |
104 | | - label: "{{ item }}" |
105 | 110 | no_log: true |
106 | 111 | register: result |
107 | 112 | changed_when: result.status == 204 |
0 commit comments