feat(role): Initial commit

This commit is contained in:
hygienic-books 2025-04-02 02:39:25 +02:00
parent 523022da40
commit d72ad298b2
14 changed files with 177 additions and 9 deletions

9
.gitignore vendored
View File

@ -1,7 +1,2 @@
# ---> JetBrainsWorkspace
# Additional coverage for JetBrains IDEs workspace files
.idea/deployment.xml
.idea/misc.xml
.idea/remote-mappings.xml
.idea/*.iml
# SPDX-License-Identifier: MIT
.idea/

View File

@ -1,3 +1,36 @@
# role-common-bash_convenience
[//]: # (SPDX-License-Identifier: MIT)
# Role Name
An Ansible role that does basic bashrc setup for Linux machines
Does basic `.bashrc` setup for Linux machines.
Requirements
------------
Your target machines must be Linux.
Role Variables
--------------
Per [defaults/main.yml](defaults/main.yml) this role will operate on the target machine's local `root` account by default. Change list elements in `local_accounts_that_need_bash_settings` as needed. Better yet, override this with host vars.
Dependencies
------------
None.
Example Playbook
----------------
In your `playbook.yml` call it like so:
```
- name: 'Awesome playbook'
hosts: all
roles:
- 'role_common_local-os-password'
```
License
-------
MIT

3
defaults/main.yml Normal file
View File

@ -0,0 +1,3 @@
# SPDX-License-Identifier: MIT
local_accounts_that_need_bash_settings:
- 'root'

View File

@ -0,0 +1,2 @@
# SPDX-License-Identifier: MIT
alias paru='sudo --user build paru'

View File

@ -0,0 +1,2 @@
# SPDX-License-Identifier: MIT
eval "$(uv generate-shell-completion bash)"

View File

@ -0,0 +1,2 @@
# SPDX-License-Identifier: MIT
alias grep='grep --color=auto'

View File

@ -0,0 +1,2 @@
# SPDX-License-Identifier: MIT
PS1='[$([[ "${?}" -eq '"'"'0'"'"' ]] && echo -ne '"'"'\[\e[32m\]✔'"'"' || echo -ne '"'"'\[\e[91m\]✘'"'"'; echo -n '"'"'\[\e[0m\] '"'"')\[\e[38;5;244m\]\A\[\e[0m\] \[\e[38;5;209;1m\]\u\[\e[38;5;51m\]@\[\e[38;5;154m\]\H\[\e[0m\] \W]\$ '

View File

@ -0,0 +1,4 @@
# SPDX-License-Identifier: MIT
HISTCONTROL=ignoredups
HISTFILESIZE=1000000
HISTSIZE=1000000

View File

@ -0,0 +1,2 @@
# SPDX-License-Identifier: MIT
shopt -s histappend

9
meta/main.yml Normal file
View File

@ -0,0 +1,9 @@
# SPDX-License-Identifier: MIT
galaxy_info:
author: 'hygienic-books'
description: 'Does basic .bashrc setup for Linux machines'
license: MIT
min_ansible_version: 2.18.1
galaxy_tags:
- 'bash'
dependencies: []

View File

@ -0,0 +1,20 @@
# SPDX-License-Identifier: MIT
- name: 'Copy non-templated bash include files'
loop_control:
label: 'Non-templated file ''{{ item | basename }}'''
loop: '{{ lookup(''ansible.builtin.fileglob'', ''root/.config/bash/bashrc-includes.d/system_'' + ansible_facts[''system''] | lower + ''_*'', ''root/.config/bash/bashrc-includes.d/os_family_'' + ansible_facts[''os_family''] | lower + ''_*'', wantlist=True) }}'
ansible.builtin.copy:
src: '{{ item }}'
dest: '{% if local_account != ''root'' %}/home{% endif %}/{{ local_account }}/.config/bash/bashrc-includes.d/'
owner: '{{ local_account }}'
group: '{{ local_account }}'
- name: 'Copy templated bash include files'
loop_control:
label: 'Templated file ''{{ item | basename }}'' --> ''{{ item | basename | regex_search(''.+?(?=\.j2)'') }}'''
loop: '{{ lookup(''ansible.builtin.fileglob'', ''../templates/root/.config/bash/bashrc-includes.d/system_'' + ansible_facts[''system''] | lower + ''_*.j2'', ''../templates/root/.config/bash/bashrc-includes.d/os_family_'' + ansible_facts[''os_family''] | lower + ''_*.j2'', wantlist=True) }}'
ansible.builtin.template:
src: '{{ item }}'
dest: '{% if local_account != ''root'' %}/home{% endif %}/{{ local_account }}/.config/bash/bashrc-includes.d/{{ item | basename | regex_search(''.+?(?=\.j2)'') }}'
owner: '{{ local_account }}'
group: '{{ local_account }}'

View File

@ -0,0 +1,15 @@
# SPDX-License-Identifier: MIT
- name: 'For local account ''{{ local_account }}'' get list of bash include files'
changed_when: false
register: 'bashcon_canonical_bashrc_includes_actual'
ansible.builtin.shell: |
ls -1 {% if local_account != 'root' %}/home{% endif %}/{{ local_account }}/.config/bash/bashrc-includes.d
- name: 'For local account ''{{ local_account }}'' delete unneeded bash include files'
when: 'item not in bashcon_canonical_bashrc_includes_desired'
loop_control:
label: 'If unneeded delete bashrc include file ''{{ item }}'''
loop: '{{ bashcon_canonical_bashrc_includes_actual.stdout_lines }}'
ansible.builtin.file:
path: '{% if local_account != ''root'' %}/home{% endif %}/{{ local_account }}/.config/bash/bashrc-includes.d/{{ item }}'
state: 'absent'

77
tasks/main.yml Normal file
View File

@ -0,0 +1,77 @@
# SPDX-License-Identifier: MIT
- name: 'Create dir to store bash include files'
loop_control:
loop_var: 'local_account'
label: 'For local account ''{{ local_account }}'' create ~/''.config/bash/bashrc-includes.d'' dir'
loop: '{{ local_accounts_that_need_bash_settings }}'
ansible.builtin.file:
path: '{% if local_account != ''root'' %}/home{% endif %}/{{ local_account }}/.config/bash/bashrc-includes.d'
state: 'directory'
owner: '{{ local_account }}'
group: '{{ local_account }}'
- name: 'If OS is Arch Linux flavor install rsync'
when: 'ansible_facts[''system''] | lower == ''linux'' and ansible_facts[''os_family''] | lower == ''archlinux'''
ansible.builtin.package:
name:
- 'rsync'
state: 'present'
- include_tasks: 'add_bash_include_files.yml'
loop: '{{ local_accounts_that_need_bash_settings }}'
loop_control:
loop_var: 'local_account'
- name: 'Store canonical list of bashrc includes in a variable'
ansible.builtin.set_fact:
bashcon_canonical_bashrc_includes_desired: '{{ lookup(''ansible.builtin.fileglob'', ''root/.config/bash/bashrc-includes.d/system_'' + ansible_facts[''system''] | lower + ''_*'', ''root/.config/bash/bashrc-includes.d/os_family_'' + ansible_facts[''os_family''] | lower + ''_*'', ''../templates/root/.config/bash/bashrc-includes.d/system_'' + ansible_facts[''system''] | lower + ''_*.j2'', ''../templates/root/.config/bash/bashrc-includes.d/os_family_'' + ansible_facts[''os_family''] | lower + ''_*.j2'', wantlist=True) | map(''regex_replace'', ''(.*/)([^/]+?)(\.j2)?$'', ''\2'') | list }}'
- include_tasks: 'delete_unneeded_bash_include_files.yml'
loop: '{{ local_accounts_that_need_bash_settings }}'
loop_control:
loop_var: 'local_account'
- name: 'Ensure that ''.bashrc'' exists'
loop: '{{ local_accounts_that_need_bash_settings }}'
loop_control:
loop_var: 'local_account'
label: 'For local account ''{{ local_account }}'' ensure that ''.bashrc'' exists'
ansible.builtin.file:
path: '{% if local_account != ''root'' %}/home{% endif %}/{{ local_account }}/.bashrc'
state: 'touch'
access_time: 'preserve'
modification_time: 'preserve'
owner: '{{ local_account }}'
group: '{{ local_account }}'
- name: 'Source ''.bashrc'' file from within ''.bash_profile'' file'
loop: '{{ local_accounts_that_need_bash_settings }}'
loop_control:
loop_var: 'local_account'
label: 'For local account ''{{ local_account }}'' source ''.bashrc'' file'
ansible.builtin.blockinfile:
path: '{% if local_account != ''root'' %}/home{% endif %}/{{ local_account }}/.bash_profile'
create: true
append_newline: true
prepend_newline: true
block: |
[[ -f ~/.bashrc ]] && . ~/.bashrc
- name: 'Source bash include files'
loop: '{{ local_accounts_that_need_bash_settings }}'
loop_control:
loop_var: 'local_account'
label: 'For local account ''{{ local_account }}'' source ''.bashrc'' file'
ansible.builtin.blockinfile:
path: '{% if local_account != ''root'' %}/home{% endif %}/{{ local_account }}/.bashrc'
append_newline: true
prepend_newline: true
block: |
for bashrc_include_file in ~/.config/bash/bashrc-includes.d/*; do
source "${bashrc_include_file}"
done
- name: 'Install exa for sane directory listings'
ansible.builtin.package:
name: 'exa'
state: 'present'

View File

@ -0,0 +1,2 @@
# SPDX-License-Identifier: MIT
alias l='e{% if ansible_facts['os_family'] | lower == 'archlinux' %}z{% else %}x{% endif %}a --all --long --binary --group{% if ansible_facts['os_family'] | lower == 'archlinux' %} --smart-group{% endif %} --links{% if ansible_facts['os_family'] | lower == 'archlinux' %} --octal-permissions{% endif %}'