From e9f2898efbbc8e2dd224e6305d27bd72a0c62797 Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Wed, 3 Apr 2024 11:58:29 +0200 Subject: [PATCH] Attempt at customising the bashrc file. --- defaults/main.yml | 12 ++++ files/root_bashrc.sh | 2 + files/root_bashrc_history.sh | 3 + files/skel_bashrc.sh | 7 +++ tasks/custom_bashrc.yml | 113 +++++++++++++++++++++++++++++++++++ tasks/main.yml | 2 + 6 files changed, 139 insertions(+) create mode 100644 files/root_bashrc.sh create mode 100644 files/root_bashrc_history.sh create mode 100644 files/skel_bashrc.sh create mode 100644 tasks/custom_bashrc.yml diff --git a/defaults/main.yml b/defaults/main.yml index 6bd4e31..42085e3 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -31,6 +31,18 @@ ubuntu_configure_additional_ints_list: [] disable_ipv6: false ipv6_sysctl_value: 1 ipv6_sysctl_file: /etc/sysctl.d/10-ipv6-disable.conf + +# Bash prompt and shell history settings +# +bash_customize_skel_bashrc: false +bash_etc_skel_file: /etc/skel/.bashrc +bash_custom_skel_bashrc_file: files/skel_bashrc.sh +bash_customize_root_bashrc: false +bash_custom_root_bashrc_file: files/root_bashrc.sh +bash_root_bashrc_file: /etc/.bashrc +bash_customize_root_history_settings: false +bash_custom_history_directory: /var/log/users_root_history +bash_custom_history_settings_file: files/root_bashrc_history.sh # # Define the following variables to manage additional disks and mount points, even static nfs ones additional_disks: false diff --git a/files/root_bashrc.sh b/files/root_bashrc.sh new file mode 100644 index 0000000..7531d43 --- /dev/null +++ b/files/root_bashrc.sh @@ -0,0 +1,2 @@ +export _ROOT_COLOR=33 # yellow +PS1="\[\033[01;${_ROOT_COLOR}m\]${debian_chroot:+($debian_chroot)}ROOT\[\033[01;34m\]@$(hostname -f) \w \@ #\[\033[00m\] " diff --git a/files/root_bashrc_history.sh b/files/root_bashrc_history.sh new file mode 100644 index 0000000..a0625ef --- /dev/null +++ b/files/root_bashrc_history.sh @@ -0,0 +1,3 @@ +export HISTTIMEFORMAT="%Y%m%d %H:%M:%S " +export HISTSIZE=10000 +export HISTFILE=/var/log/users_root_history/history-$(who am i | awk '{print $1}';exit) diff --git a/files/skel_bashrc.sh b/files/skel_bashrc.sh new file mode 100644 index 0000000..75389a7 --- /dev/null +++ b/files/skel_bashrc.sh @@ -0,0 +1,7 @@ +export _ROOT_COLOR=33 # yellow +export _USER_COLOR=36 # cyan +if [ "`id -u`" -eq 0 ]; then + PS1="\[\033[01;${_ROOT_COLOR}m\]${debian_chroot:+($debian_chroot)}ROOT\[\033[01;34m\]@$(hostname -f) \w \@ #\[\033[00m\] " +else + PS1="\[\033[01;${_USER_COLOR}m\]${debian_chroot:+($debian_chroot)}\u\[\033[01;34m\]@$(hostname -f) \w \@ $\[\033[00m\] " +fi diff --git a/tasks/custom_bashrc.yml b/tasks/custom_bashrc.yml new file mode 100644 index 0000000..aa300d2 --- /dev/null +++ b/tasks/custom_bashrc.yml @@ -0,0 +1,113 @@ +--- +- name: custom_bashrc | Manage the skel bashrc customizations + when: bash_customize_skel_bashrc + tags: + - bashrc + - etcskel + block: + - name: custom_bashrc | Manage the skel bashrc customizations + ansible.builtin.blockinfile: + path: "{{ bash_etc_skel_file }}" + marker_begin: 'bashrc_customizations_start' + marker_end: 'bashrc_customizations_end' + marker: "# {mark} Customization to bashrc installed via ansible" + block: "{{ item }}" + loop: + - "{{ lookup('ansible.builtin.file', '{{ bash_custom_skel_bashrc_file }}') }}" + state: present + +- name: custom_bashrc | Remove the skel bashrc customizations + when: not bash_customize_skel_bashrc + tags: + - bashrc + - etcskel + block: + - name: custom_bashrc | Remove the skel bashrc customizations + ansible.builtin.blockinfile: + path: "{{ bash_etc_skel_file }}" + marker_begin: 'bashrc_customizations_start' + marker_end: 'bashrc_customizations_end' + marker: "# {mark} Customization to bashrc installed via ansible" + block: "{{ item }}" + loop: + - "{{ lookup('ansible.builtin.file', '{{ bash_custom_skel_bashrc_file }}') }}" + state: absent + +- name: custom_bashrc | Manage the root user bashrc customization + when: bash_customize_root_bashrc + tags: + - bashrc + - root_bashrc + block: + - name: custom_bashrc | Manage the root user bashrc customization + ansible.builtin.blockinfile: + path: "{{ bash_root_bashrc_file }}" + marker_begin: 'bashrc_customizations_start' + marker_end: 'bashrc_customizations_end' + marker: "# {mark} Customization to bashrc installed via ansible" + block: "{{ item }}" + loop: + - "{{ lookup('ansible.builtin.file', '{{ bash_custom_root_bashrc_file }}') }}" + state: present + +- name: custom_bashrc | Remove the root user bashrc customization + when: not bash_customize_root_bashrc + tags: + - bashrc + - root_bashrc + block: + - name: custom_bashrc | Remove the root user bashrc customization + ansible.builtin.blockinfile: + path: "{{ bash_root_bashrc_file }}" + marker_begin: 'bashrc_customizations_start' + marker_end: 'bashrc_customizations_end' + marker: "# {mark} Customization to bashrc installed via ansible" + block: "{{ item }}" + loop: + - "{{ lookup('ansible.builtin.file', '{{ bash_custom_root_bashrc_file }}') }}" + state: absent + +- name: custom_bashrc | Manage the root user bashrc history settings + when: bash_customize_root_history_settings + tags: + - bashrc + - root_bashrc_history_settings + block: + - name: custom_bashrc | Create the bash history logs directory + ansible.builtin.file: + path: "{{ bash_custom_history_directory }}" + state: directory + owner: root + group: root + mode: "0700" + - name: custom_bashrc | Manage the root user bashrc history settings + ansible.builtin.blockinfile: + path: "{{ bash_root_bashrc_file }}" + marker_begin: 'history_customizations_start' + marker_end: 'history_customizations_end' + marker: "# {mark} Customization to the bash prompt installed via ansible" + block: "{{ item }}" + loop: + - "{{ lookup('ansible.builtin.file', '{{ bash_custom_history_settings_file }}') }}" + state: present + +- name: custom_bashrc | Remove the root user bashrc history settings + when: not bash_customize_root_history_settings + tags: + - bashrc + - root_bashrc_history_settings + block: + - name: custom_bashrc | Remove the root user bashrc history settings + ansible.builtin.blockinfile: + path: "{{ bash_root_bashrc_file }}" + marker_begin: 'history_customizations_start' + marker_end: 'history_customizations_end' + marker: "# {mark} Customization to the bash prompt installed via ansible" + block: "{{ item }}" + loop: + - "{{ lookup('ansible.builtin.file', '{{ bash_custom_history_settings_file }}') }}" + state: absent + - name: custom_bashrc | Remove the bash history logs directory + ansible.builtin.file: + path: "{{ bash_custom_history_directory }}" + state: absent diff --git a/tasks/main.yml b/tasks/main.yml index da8ca6c..2736145 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -41,3 +41,5 @@ when: - nfs_server_enabled - not nfs_server_ganesha_enabled +- name: Custom bashrc settings + ansible.builtin.import_tasks: custom_bashrc.yml