From cd250f1472ab025982d0b969c032931d182a3c6c Mon Sep 17 00:00:00 2001 From: L3D Date: Tue, 26 Mar 2024 15:58:56 +0100 Subject: [PATCH] Create bashrc config - global and pre user --- README.md | 1 + roles/dotfiles/README.md | 1 + roles/dotfiles/defaults/main.yml | 27 ++++++++++ roles/dotfiles/tasks/bashrc.yml | 19 ++++++- roles/dotfiles/templates/bash.bashrc.j2 | 19 +++---- roles/dotfiles/templates/dotbashrc.j2 | 66 +++++++++++++++++++++++++ 6 files changed, 118 insertions(+), 15 deletions(-) create mode 100644 roles/dotfiles/templates/dotbashrc.j2 diff --git a/README.md b/README.md index 5defc50..baa9e29 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,7 @@ The Option of these directory-variables are the following. | ``pubkeys`` | string or lookup | - | see examples | | ``exklusive_pubkeys`` | ``true`` | - | delete all undefined ssh keys | | ``password`` | password hash | - | See [official FAQ](https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#how-do-i-generate-encrypted-passwords-for-the-user-module) | +| ``bashrc`` | list | - | adding additional content to l3d.users.dotfiles to .bashrc | | ``groups`` | list | - | Additional groups for your user | | ``remove`` | ``false`` | - | completly remove user if ``state: absent`` | diff --git a/roles/dotfiles/README.md b/roles/dotfiles/README.md index 01fb07f..3fc6b91 100644 --- a/roles/dotfiles/README.md +++ b/roles/dotfiles/README.md @@ -27,6 +27,7 @@ The Option of these directory-variables are the following. | ``pubkeys`` | string or lookup | - | see examples | | ``exklusive_pubkeys`` | ``true`` | - | delete all undefined ssh keys | | ``password`` | password hash | - | See [official FAQ](https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#how-do-i-generate-encrypted-passwords-for-the-user-module) | +| ``bashrc`` | list | - | adding additional content to l3d.users.dotfiles to .bashrc | | ``groups`` | list | - | Additional groups for your user | | ``remove`` | ``false`` | - | completly remove user if ``state: absent`` | diff --git a/roles/dotfiles/defaults/main.yml b/roles/dotfiles/defaults/main.yml index 21fe338..984e286 100644 --- a/roles/dotfiles/defaults/main.yml +++ b/roles/dotfiles/defaults/main.yml @@ -15,6 +15,8 @@ l3d_users__default_users: [] # admin_commands: 'ALL' # admin_nopassword: false # admin_ansible_login: true +# bashrc: +# - '[[ $- == *i* ]] && eval $(keychain --eval --quiet id_ed25519)' # - name: 'bob' # state: 'present' # shell: '/bin/zsh' @@ -29,6 +31,31 @@ l3d_users__local_users: [] # pubkeys: "{{ lookup('url', 'https://github.com/do1jlr.keys', split_lines=False) }}" l3d_users__bashrc: true +l3d_users__dotfiles__bash_completion_enabled: true +l3d_users__dotfiles__aliases: + - {alias: "ls", command: "ls ", color: true} + - {alias: "grep", command: "grep", color: true} + - {alias: "ll", command: "ls -alF", color: false} + - {alias: "la", command: "ls -A", color: false} + - {alias: "l", command: "ls -CF", color: false} + - {alias: "lz", command: "ls -Z", color: false} + - {alias: "EDITOR", command: "/usr/bin/vim", color: false} + - {alias: "VISUAL", command: "/usr/bin/gedit", color: false} + - {alias: "gitsubpull", command: "git submodule foreach '(git checkout \\$(git symbolic-ref --short refs/remotes/origin/HEAD | sed 's@^origin/@@'); git pull)'", color: false} + - {alias: "pwgen", command: "/usr/bin/pwgen --num-passwords=3000 --numerals --capitalize --secure --no-vowels --ambiguous 95 1", color: false} + - {alias: "gdiff", command: "git diff --submodule=diff", color: false} +dotfiles__additional_user_bashrc_lines: [] + +l3d_users__bashrc_path: "$HOME/.local/bin:$HOME/bin:$HOME/.cargo/env:$PATH" + +# PS1 Prompt +l3d_users__dotfiles__user_prompt: '\e]0; \u@\H <\A> \w\a\[\033[01;33m\] $(printf "\xE2\x9D\xA4") \[\033[01;32m\]\u\[\033[01;36m\]@\[\033[01;32m\]\H\[\033[01;34m\] <\A> \[\033[01;35m\] \j \[\033[01;36m\] \w \[\033[01;33m\]\n\[\033[01;33m\] $(git branch 2>/dev/null | sed -n "s/* \(.*\)/\1 /p")$\[\033[01;00m\]' +l3d_users__dotfiles__root_prompt: '\e]0; \u@\H <\A> \w\a\[\033[01;31m\] $(printf "\xE2\x9D\xA4") \[\033[01;32m\]\u\[\033[01;36m\]@\[\033[01;32m\]\H\[\033[01;34m\] <\A> \[\033[01;35m\] \j \[\033[01;36m\] \w \[\033[01;33m\]\n\[\033[01;33m\] $(git branch 2>/dev/null | sed -n "s/* \(.*\)/\1 /p")$\[\033[01;00m\]' + +# bash_history +l3d_users__dotfiles__history_control: 'ignoreboth' +l3d_users__dotfiles__history_size: '-1' +l3d_users__dotfiles__history_file_size: '-1' # run simple versionscheck submodules_versioncheck: false diff --git a/roles/dotfiles/tasks/bashrc.yml b/roles/dotfiles/tasks/bashrc.yml index ac91fae..a87c014 100644 --- a/roles/dotfiles/tasks/bashrc.yml +++ b/roles/dotfiles/tasks/bashrc.yml @@ -4,6 +4,21 @@ ansible.builtin.template: src: 'templates/bash.bashrc.j2' dest: '/etc/bash.bashrc' - owner: root - group: root + owner: 'root' + group: 'root' mode: 'u=rw,g=r,o=r' + +- name: Create global bashrc configuration + become: true + ansible.builtin.template: + src: 'templates/dotbashrc.j2' + dest: "{{ user.home | default('/home/' + user.name) }}/.bashrc" + owner: "{{ user.name }}" + group: "{{ user.name }}" + mode: '0640' + loop: "{{ _l3d_users__merged_users }}" + loop_control: + label: "user={{ user.name }}" + loop_var: user + when: user.state | default ('present') == 'present' + diff --git a/roles/dotfiles/templates/bash.bashrc.j2 b/roles/dotfiles/templates/bash.bashrc.j2 index 6524b14..03d237c 100644 --- a/roles/dotfiles/templates/bash.bashrc.j2 +++ b/roles/dotfiles/templates/bash.bashrc.j2 @@ -1,11 +1,9 @@ # /etc/bashrc +{{ ansible_managed | comment }} # System wide functions and aliases # Environment stuff goes in /etc/profile -# This file may be overwritten by ansible. Please think about commiting -# changes you want to keep persistent. - # enable color support of ls and also add handy aliases if [ -x /usr/bin/dircolors ]; then test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)" @@ -16,13 +14,13 @@ if [ -x /usr/bin/dircolors ]; then {% endfor %} fi -{% for alias in dotfiles__aliases %} +{% for alias in l3d_users__dotfiles__aliases %} {% if not alias.color %} alias {{ alias.alias }}="{{ alias.command }}" {% endif%} {% endfor %} -{%if dotfiles__bash_completion_enabled %} +{%if l3d_users__dotfiles__bash_completion_enabled %} if ! shopt -oq posix; then if [ -f /usr/share/bash-completion/bash_completion ]; then . /usr/share/bash-completion/bash_completion @@ -35,13 +33,13 @@ fi # fancy Prompt if [ $(id -u) -eq 0 ]; then - export PS1='{{ dotfiles__root_prompt }} ' + export PS1='{{ l3d_users__dotfiles__root_prompt }} ' else - export PS1='{{ dotfiles__user_prompt }} ' + export PS1='{{ l3d_users__dotfiles__user_prompt }} ' fi -HISTCONTROL={{ dotfiles__history_control }} +HISTCONTROL={{ l3d_users__dotfiles__history_control }} shopt -s histappend {% if ansible_os_family == 'Redhat' %} # HISTSIZE={{ dotfiles__history_size }} @@ -53,9 +51,4 @@ shopt -s histappend HISTSIZE={{ dotfiles__history_size }} HISTFILESIZE={{ dotfiles__history_file_size }} {% endif %} - - -{% for _row in dotfiles__additional_bashrc_lines | default() %} -{{ _row }} -{% endfor %} # vim:ts=4:sw=4 diff --git a/roles/dotfiles/templates/dotbashrc.j2 b/roles/dotfiles/templates/dotbashrc.j2 new file mode 100644 index 0000000..600c40a --- /dev/null +++ b/roles/dotfiles/templates/dotbashrc.j2 @@ -0,0 +1,66 @@ +# {{ user.home | default('/home/' + user.name) }}/.bashrc" +# for user {{ user.name }} +{{ ansible_managed | comment }} + +# Source global definitions +if [ -f /etc/bash.bashrc ]; then + . /etc/bash.bashrc +fi + +# User specific environment +PATH="{{ l3d_users__bashrc_path }}" +export PATH + +# enable color support of ls and also add handy aliases +if [ -x /usr/bin/dircolors ]; then + test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)" +{% for alias in dotfiles__aliases %} +{% if alias.color %} + alias {{ alias.alias }}="{{ alias.command }} --color=auto" +{% endif%} +{% endfor %} +fi + +{% for alias in l3d_users__dotfiles__aliases %} +{% if not alias.color %} + alias {{ alias.alias }}="{{ alias.command }}" +{% endif%} +{% endfor %} + +{%if l3d_users__dotfiles__bash_completion_enabled %} +if ! shopt -oq posix; then + if [ -f /usr/share/bash-completion/bash_completion ]; then + . /usr/share/bash-completion/bash_completion + elif [ -f /etc/bash_completion ]; then + . /etc/bash_completion + fi +fi + +# fancy Prompt +export PS1='{{ l3d_users__dotfiles__user_prompt }} ' + +# baqsh_history +HISTCONTROL={{ l3d_users__dotfiles__history_control }} +shopt -s histappend +{% if ansible_os_family == 'Redhat' %} +# HISTSIZE={{ dotfiles__history_size }} +# HISTFILESIZE={{ dotfiles__history_file_size }} +{% elif ansible_distribution == "CentOS" %} +# HISTSIZE={{ dotfiles__history_size }} +# HISTFILESIZE={{ dotfiles__history_file_size }} +{% else %} +HISTSIZE={{ dotfiles__history_size }} +HISTFILESIZE={{ dotfiles__history_file_size }} +{% endif %} + +# additional general rules +{% for _row in dotfiles__additional_user_bashrc_lines | default() %} +{{ _row }} +{% endfor %} + +# additional user rules +{% for custom in {{ user.bashrc | default([]) }} %} +{{ custom }} +{% endfor %} + +# vim:ts=4:sw=4