From d72ad298b284b7071d0b5eb50176b821e49ec7e7 Mon Sep 17 00:00:00 2001 From: hygienic-books Date: Wed, 2 Apr 2025 02:39:25 +0200 Subject: [PATCH] feat(role): Initial commit --- .gitignore | 9 +-- README.md | 37 ++++++++- defaults/main.yml | 3 + .../os_family_archlinux_alias_paru | 2 + .../os_family_archlinux_uv_bash_completion | 2 + .../bashrc-includes.d/system_linux_alias_grep | 2 + .../bashrc-includes.d/system_linux_var_PS1 | 2 + .../system_linux_var_history | 4 + .../bashrc-includes.d/system_linux_var_shopt | 2 + meta/main.yml | 9 +++ tasks/add_bash_include_files.yml | 20 +++++ tasks/delete_unneeded_bash_include_files.yml | 15 ++++ tasks/main.yml | 77 +++++++++++++++++++ .../bashrc-includes.d/system_linux_alias_l.j2 | 2 + 14 files changed, 177 insertions(+), 9 deletions(-) create mode 100644 defaults/main.yml create mode 100644 files/root/.config/bash/bashrc-includes.d/os_family_archlinux_alias_paru create mode 100644 files/root/.config/bash/bashrc-includes.d/os_family_archlinux_uv_bash_completion create mode 100644 files/root/.config/bash/bashrc-includes.d/system_linux_alias_grep create mode 100644 files/root/.config/bash/bashrc-includes.d/system_linux_var_PS1 create mode 100644 files/root/.config/bash/bashrc-includes.d/system_linux_var_history create mode 100644 files/root/.config/bash/bashrc-includes.d/system_linux_var_shopt create mode 100644 meta/main.yml create mode 100644 tasks/add_bash_include_files.yml create mode 100644 tasks/delete_unneeded_bash_include_files.yml create mode 100644 tasks/main.yml create mode 100644 templates/root/.config/bash/bashrc-includes.d/system_linux_alias_l.j2 diff --git a/.gitignore b/.gitignore index 489fe27..831f4b2 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ diff --git a/README.md b/README.md index 09828c7..c32f855 100644 --- a/README.md +++ b/README.md @@ -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 \ No newline at end of file +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 diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..dac58df --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: MIT +local_accounts_that_need_bash_settings: + - 'root' diff --git a/files/root/.config/bash/bashrc-includes.d/os_family_archlinux_alias_paru b/files/root/.config/bash/bashrc-includes.d/os_family_archlinux_alias_paru new file mode 100644 index 0000000..cc25e69 --- /dev/null +++ b/files/root/.config/bash/bashrc-includes.d/os_family_archlinux_alias_paru @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: MIT +alias paru='sudo --user build paru' diff --git a/files/root/.config/bash/bashrc-includes.d/os_family_archlinux_uv_bash_completion b/files/root/.config/bash/bashrc-includes.d/os_family_archlinux_uv_bash_completion new file mode 100644 index 0000000..6fad0aa --- /dev/null +++ b/files/root/.config/bash/bashrc-includes.d/os_family_archlinux_uv_bash_completion @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: MIT +eval "$(uv generate-shell-completion bash)" diff --git a/files/root/.config/bash/bashrc-includes.d/system_linux_alias_grep b/files/root/.config/bash/bashrc-includes.d/system_linux_alias_grep new file mode 100644 index 0000000..82c06af --- /dev/null +++ b/files/root/.config/bash/bashrc-includes.d/system_linux_alias_grep @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: MIT +alias grep='grep --color=auto' diff --git a/files/root/.config/bash/bashrc-includes.d/system_linux_var_PS1 b/files/root/.config/bash/bashrc-includes.d/system_linux_var_PS1 new file mode 100644 index 0000000..1eff7eb --- /dev/null +++ b/files/root/.config/bash/bashrc-includes.d/system_linux_var_PS1 @@ -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]\$ ' diff --git a/files/root/.config/bash/bashrc-includes.d/system_linux_var_history b/files/root/.config/bash/bashrc-includes.d/system_linux_var_history new file mode 100644 index 0000000..a6006ad --- /dev/null +++ b/files/root/.config/bash/bashrc-includes.d/system_linux_var_history @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: MIT +HISTCONTROL=ignoredups +HISTFILESIZE=1000000 +HISTSIZE=1000000 diff --git a/files/root/.config/bash/bashrc-includes.d/system_linux_var_shopt b/files/root/.config/bash/bashrc-includes.d/system_linux_var_shopt new file mode 100644 index 0000000..24546ee --- /dev/null +++ b/files/root/.config/bash/bashrc-includes.d/system_linux_var_shopt @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: MIT +shopt -s histappend diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..8a6605f --- /dev/null +++ b/meta/main.yml @@ -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: [] diff --git a/tasks/add_bash_include_files.yml b/tasks/add_bash_include_files.yml new file mode 100644 index 0000000..129c680 --- /dev/null +++ b/tasks/add_bash_include_files.yml @@ -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 }}' diff --git a/tasks/delete_unneeded_bash_include_files.yml b/tasks/delete_unneeded_bash_include_files.yml new file mode 100644 index 0000000..07f09bb --- /dev/null +++ b/tasks/delete_unneeded_bash_include_files.yml @@ -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' diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..64f3d17 --- /dev/null +++ b/tasks/main.yml @@ -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' diff --git a/templates/root/.config/bash/bashrc-includes.d/system_linux_alias_l.j2 b/templates/root/.config/bash/bashrc-includes.d/system_linux_alias_l.j2 new file mode 100644 index 0000000..501cf85 --- /dev/null +++ b/templates/root/.config/bash/bashrc-includes.d/system_linux_alias_l.j2 @@ -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 %}'