mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-09-02 00:20:35 +02:00
Huge role refactoring/cleanup. Other commits will propably follow. Because some bugs will exist. Still important for longrun and also for auto docs/help/slideshow generation
This commit is contained in:
67
roles/web-app-mailu/tasks/create-mailu-token.yml
Normal file
67
roles/web-app-mailu/tasks/create-mailu-token.yml
Normal file
@@ -0,0 +1,67 @@
|
||||
|
||||
- name: "Fetch existing API tokens via curl inside admin container"
|
||||
command: >-
|
||||
docker compose exec -T admin \
|
||||
curl -s -X GET {{ mailu_api_base_url }}/token \
|
||||
-H "Authorization: Bearer {{ mailu_global_api_token }}"
|
||||
args:
|
||||
chdir: "{{ mailu_compose_dir }}"
|
||||
register: mailu_tokens_cli
|
||||
changed_when: false
|
||||
|
||||
- name: "Extract existing token info for '{{ mailu_user_key }};{{ mailu_user_name }}'"
|
||||
set_fact:
|
||||
mailu_user_existing_token: >-
|
||||
{{ (
|
||||
mailu_tokens_cli.stdout
|
||||
| default('[]')
|
||||
| from_json
|
||||
| selectattr('comment','equalto', mailu_user_key ~ " - ansible.cymais")
|
||||
| list
|
||||
).0 | default(None) }}
|
||||
|
||||
- name: "Delete existing API token for '{{ mailu_user_key }};{{ mailu_user_name }}' if local token missing but remote exists"
|
||||
command: >-
|
||||
docker compose exec -T admin \
|
||||
curl -s -X DELETE {{ mailu_api_base_url }}/token/{{ mailu_user_existing_token.id }} \
|
||||
-H "Authorization: Bearer {{ mailu_global_api_token }}"
|
||||
args:
|
||||
chdir: "{{ mailu_compose_dir }}"
|
||||
when:
|
||||
- users[mailu_user_key].mailu_token is not defined
|
||||
- mailu_user_existing_token is not none
|
||||
- mailu_user_existing_token.id is defined
|
||||
register: mailu_token_delete
|
||||
changed_when: mailu_token_delete.rc == 0
|
||||
|
||||
- name: "Create API token for '{{ mailu_user_key }};{{ mailu_user_name }}' if no local token defined"
|
||||
command: >-
|
||||
docker compose exec -T admin \
|
||||
curl -s -X POST {{ mailu_api_base_url }}/token \
|
||||
-H "Authorization: Bearer {{ mailu_global_api_token }}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{{ {
|
||||
"comment": mailu_user_key ~ " - ansible.cymais",
|
||||
"email": users[mailu_user_key].email,
|
||||
"ip": mailu_token_ip
|
||||
} | to_json }}'
|
||||
args:
|
||||
chdir: "{{ mailu_compose_dir }}"
|
||||
when: users[mailu_user_key].mailu_token is not defined
|
||||
register: mailu_token_creation
|
||||
changed_when: mailu_token_creation.rc == 0
|
||||
|
||||
- name: "Set mailu_token for '{{ mailu_user_key }};{{ mailu_user_name }}' in users dict if newly created"
|
||||
set_fact:
|
||||
users: >-
|
||||
{{ users
|
||||
| combine({
|
||||
mailu_user_key: (
|
||||
users[mailu_user_key]
|
||||
| combine({
|
||||
'mailu_token': (mailu_token_creation.stdout | from_json).token
|
||||
})
|
||||
)
|
||||
}, recursive=True)
|
||||
}}
|
||||
when: users[mailu_user_key].mailu_token is not defined
|
Reference in New Issue
Block a user