1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-06-05 15:57:05 +00:00
community.general/tests/integration/targets/gitlab_user/tasks/sshkey.yml
patchback[bot] b94ac3fa21
[PR #11996/2cb4a5d4 backport][stable-12] gitlab_user: update SSH keys when key material changes (#12038)
gitlab_user: update SSH keys when key material changes (#11996)

* gitlab_user: update SSH keys when key material changes

Compare SSH keys by key type and key material so comment-only differences remain idempotent while changed keys are replaced. Add unit and integration coverage for SSH key updates.

Fixes #6516

* gitlab_user: add SSH key update modes

Restore backward-compatible same-name SSH key handling by default and
add explicit update and deduplicate modes for controlled replacement
behavior.

Refs: #6516

* Apply suggestions from code review



---------


(cherry picked from commit 2cb4a5d4e7)

Co-authored-by: Fulvius <31437530+LCerebo@users.noreply.github.com>
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
2026-05-13 08:07:21 +02:00

180 lines
5.5 KiB
YAML

---
####################################################################
# WARNING: These are designed specifically for Ansible tests #
# and should not be used as examples of how to write Ansible roles #
####################################################################
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
- name: Create gitlab user with sshkey credentials
gitlab_user:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
email: "{{ gitlab_user_email }}"
name: "{{ gitlab_user }}"
username: "{{ gitlab_user }}"
password: "{{ gitlab_user_pass }}"
validate_certs: false
sshkey_name: "{{ gitlab_sshkey_name }}"
sshkey_file: "{{ gitlab_sshkey_file }}"
state: present
register: gitlab_user_sshkey
- name: Check user has been created correctly
assert:
that:
- gitlab_user_sshkey is changed
- name: Create gitlab user again
gitlab_user:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
email: "{{ gitlab_user_email }}"
name: "{{ gitlab_user }}"
username: "{{ gitlab_user }}"
password: "{{ gitlab_user_pass }}"
validate_certs: false
sshkey_name: "{{ gitlab_sshkey_name }}"
sshkey_file: "{{ gitlab_sshkey_file }}"
state: present
register: gitlab_user_sshkey_again
- name: Check state is not changed
assert:
that:
- gitlab_user_sshkey_again is not changed
- name: Update gitlab user ssh key when key material changes
gitlab_user:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
email: "{{ gitlab_user_email }}"
name: "{{ gitlab_user }}"
username: "{{ gitlab_user }}"
password: "{{ gitlab_user_pass }}"
validate_certs: false
sshkey_name: "{{ gitlab_sshkey_name }}"
sshkey_file: "{{ gitlab_sshkey_file_updated }}"
sshkey_update_mode: update
state: present
register: gitlab_user_sshkey_updated
- name: Check ssh key has been updated
assert:
that:
- gitlab_user_sshkey_updated is changed
- name: Update gitlab user ssh key again
gitlab_user:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
email: "{{ gitlab_user_email }}"
name: "{{ gitlab_user }}"
username: "{{ gitlab_user }}"
password: "{{ gitlab_user_pass }}"
validate_certs: false
sshkey_name: "{{ gitlab_sshkey_name }}"
sshkey_file: "{{ gitlab_sshkey_file_updated }}"
sshkey_update_mode: update
state: present
register: gitlab_user_sshkey_updated_again
- name: Check updated ssh key is idempotent
assert:
that:
- gitlab_user_sshkey_updated_again is not changed
- name: Add expires_at to an already created gitlab user with ssh key
gitlab_user:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
email: "{{ gitlab_user_email }}"
name: "{{ gitlab_user }}"
username: "{{ gitlab_user }}"
password: "{{ gitlab_user_pass }}"
validate_certs: false
sshkey_name: "{{ gitlab_sshkey_name }}"
sshkey_file: "{{ gitlab_sshkey_file_updated }}"
sshkey_expires_at: "{{ gitlab_sshkey_expires_at }}"
sshkey_update_mode: update
state: present
register: gitlab_user_created_user_sshkey_expires_at
- name: Check expires_at will not be added to a present ssh key
assert:
that:
- gitlab_user_created_user_sshkey_expires_at is not changed
- name: Remove created gitlab user
gitlab_user:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
email: "{{ gitlab_user_email }}"
name: "{{ gitlab_user }}"
username: "{{ gitlab_user }}"
validate_certs: false
state: absent
register: gitlab_user_sshkey_remove
- name: Check user has been removed correctly
assert:
that:
- gitlab_user_sshkey_remove is changed
- name: Create gitlab user with sshkey and expires_at
gitlab_user:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
email: "{{ gitlab_user_email }}"
name: "{{ gitlab_user }}"
username: "{{ gitlab_user }}"
password: "{{ gitlab_user_pass }}"
validate_certs: false
sshkey_name: "{{ gitlab_sshkey_name }}"
sshkey_file: "{{ gitlab_sshkey_file }}"
sshkey_expires_at: "{{ gitlab_sshkey_expires_at }}"
state: present
register: gitlab_user_sshkey_expires_at
- name: Check user has been created correctly
assert:
that:
- gitlab_user_sshkey_expires_at is changed
- name: Create gitlab user with sshkey and expires_at again
gitlab_user:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
email: "{{ gitlab_user_email }}"
name: "{{ gitlab_user }}"
username: "{{ gitlab_user }}"
password: "{{ gitlab_user_pass }}"
validate_certs: false
sshkey_name: "{{ gitlab_sshkey_name }}"
sshkey_file: "{{ gitlab_sshkey_file }}"
sshkey_expires_at: "{{ gitlab_sshkey_expires_at }}"
state: present
register: gitlab_user_sshkey_expires_at_again
- name: Check state is not changed
assert:
that:
- gitlab_user_sshkey_expires_at_again is not changed
- name: Remove created gitlab user
gitlab_user:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
email: "{{ gitlab_user_email }}"
name: "{{ gitlab_user }}"
username: "{{ gitlab_user }}"
validate_certs: false
state: absent
register: gitlab_user_sshkey_expires_at_remove
- name: Check user has been removed correctly
assert:
that:
- gitlab_user_sshkey_expires_at_remove is changed