Added bashrc routines

This commit is contained in:
Kevin Veen-Birkenbach 2025-02-20 14:22:08 +01:00
parent 439072c6b1
commit 5b40fe1740
7 changed files with 86 additions and 11 deletions

View File

@ -4,3 +4,6 @@ This user needs to type in his password before executing sudo.
For security reasons it's recommended to use this user instead of the standard root user.
This user should not be used to login to other systems. It's just there to let administration tasks run.
For this reason no ssh-keys are generated.
## Author
This role was created by [Kevin Veen-Birkenbach](https://www.veen.world/)

View File

@ -46,6 +46,13 @@
notify: sshd restart
when: run_once_user_administrator is not defined
- name: "embed user routines for {{ role_path | basename }}"
include_role:
name: user
vars:
user_name: "administrator"
when: run_once_user_administrator is not defined
- name: run the user_administrator tasks once
set_fact:
run_once_user_administrator: true

View File

@ -1,4 +1,3 @@
Certainly! Below is a README file in English for an Ansible role that includes the tasks you've provided:
# Root User
## Overview
@ -14,16 +13,9 @@ This Ansible role is designed to manage the generation and handling of an SSH ke
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.
## Author
This role was created by [Kevin Veen-Birkenbach](https://www.veen.world/)

View File

@ -20,6 +20,13 @@
msg: "{{ public_key.stdout }}"
when: not ssh_key.stat.exists and run_once_user_root is not defined
- name: "embed user routines for {{ role_path | basename }}"
include_role:
name: user
vars:
user_name: "root"
when: run_once_user_root is not defined
- name: run the user_root tasks once
set_fact:
run_once_user_root: true

1
roles/user/README.md Normal file
View File

@ -0,0 +1 @@
This role executes tasks which are relevant for all users

View File

@ -0,0 +1,6 @@
- name: create .bashrc
template:
src: "bashrc.j2"
dest: "/home/{{user_name}}/.bashrc"
owner: {{user_name}}
group: {{user_name}}

View File

@ -0,0 +1,59 @@
GNU nano 8.3 .bashrc
#!/bin/bash
# If not running interactively, don't do anything
[[ $- != *i* ]] && return
# Set color variables based on the current user
if [ "$USER" = "root" ]; then
HEADER_COLOR="\033[1;31m" # Bold red for root
elif [ "$USER" = "administrator" ]; then
HEADER_COLOR="\033[1;38;5;208m" # Bold orange for administrator
else
HEADER_COLOR="\033[1;33m" # Bold yellow for other users
fi
RESET_COLOR="\033[0m"
# Welcome message
echo -e "${HEADER_COLOR}Welcome, $USER on $HOSTNAME!${RESET_COLOR}"
echo -e "${HEADER_COLOR}Today is $(date +"%A, %d.%m.%Y %T")${RESET_COLOR}"
echo ""
# System Load (shows load averages and uptime)
echo -e "${HEADER_COLOR}System Load:${RESET_COLOR}"
uptime
echo ""
# Memory Usage (RAM and swap)
echo -e "${HEADER_COLOR}Memory Usage:${RESET_COLOR}"
free -h
echo ""
# Disk Usage
echo -e "${HEADER_COLOR}Disk Usage:${RESET_COLOR}"
df -h
echo ""
# CPU Information (e.g., model name)
echo -e "${HEADER_COLOR}CPU Information:${RESET_COLOR}"
lscpu | grep "Model name"
echo ""
# Top 5 Processes by CPU Usage
echo -e "${HEADER_COLOR}Top 5 Processes by CPU Usage:${RESET_COLOR}"
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head -n 6
echo ""
alias ls='ls --color=auto'
alias grep='grep --color=auto'
PS1="\$(if [ \"\$USER\" = \"administrator\" ]; then \
echo \"\[\033[4;38;5;208m\]$USER\"; \
elif [ \"\$USER\" = \"root\" ]; then \
echo \"\[\033[4;5;1;31m\]$USER\"; \
else \
echo \"\[\033[4;33m\]\$USER\"; \
fi)@\$(if [ \"\$USER\" = \"root\" ]; then \
echo \"\[\033[1;4;5;32m\]\h\"; \
else \
echo \"\[\033[1;4;32m\]\h\"; \
fi) \[\033[90m\]\$(date +%H:%M:%S)\[\033[0m\]:\[\033[38;5;13m\]\w \[\033[0m\]\$ "