Added user root

This commit is contained in:
Kevin Veen-Birkenbach 2024-01-02 09:11:53 +01:00
parent d3628d90b9
commit ab11095ec8
3 changed files with 56 additions and 0 deletions

View File

@ -4,3 +4,4 @@ dependencies:
- cleanup-backups-timer
- cleanup-failed-docker-backups
- system-maintenance-lock
- user-root

29
roles/user-root/README.md Normal file
View File

@ -0,0 +1,29 @@
Certainly! Below is a README file in English for an Ansible role that includes the tasks you've provided:
# Root User
## Overview
This Ansible role is designed to manage the generation and handling of an SSH key for the root user on a target system. It ensures that an SSH key is generated if it does not already exist and displays the public key. This role is particularly useful for setting up secure SSH access for root users in automated environments.
## Role Variables
- `run_once_user_root`: A variable to ensure that certain tasks are only run once. This is used for idempotency purposes.
## Tasks
1. **Check if the SSH key for root already exists**: Verifies the existence of an SSH public key for the root user.
2. **Generate a SSH key for root if it does not exist**: Generates a new SSH key pair (RSA 4096 bits) for the root user if it is not already present.
3. **Display the public SSH key**: Outputs the content of the generated public SSH key.
4. **Output the public SSH key**: Debug task to display the SSH public key in the Ansible output.
5. **Run the user_root tasks once**: Sets a fact to ensure that the tasks for generating and displaying the key are executed only once.
## Usage
To use this role, include it in your playbook and set any necessary variables in your playbook's `vars` section. Ensure you have the necessary permissions to execute tasks as the root user.
```yaml
- hosts: servers
become: yes
roles:
- ssh_key_generator_root
```
## Important Notes
- Running this role will affect the root user's SSH configuration on the target system. Ensure you understand the implications of modifying root SSH keys.
- Always test the role in a controlled environment before deploying to production.

View File

@ -0,0 +1,26 @@
- name: Check if the SSH key for root already exists
ansible.builtin.stat:
path: "/root/.ssh/id_rsa.pub"
register: ssh_key
- name: Generate a SSH key for root if it does not exist
ansible.builtin.openssh_keypair:
path: "/root/.ssh/id_rsa"
type: rsa
size: 4096
when: not ssh_key.stat.exists and run_once_user_administrator is not defined
- name: Display the public SSH key
command: cat /root/.ssh/id_rsa.pub
register: public_key
when: not ssh_key.stat.exists and run_once_user_administrator is not defined
- name: Output the public SSH key
debug:
msg: "{{ public_key.stdout }}"
when: not ssh_key.stat.exists and run_once_user_administrator is not defined
- name: run the user_administrator tasks once
set_fact:
run_once_user_administrator: true
when: run_once_user_administrator is not defined