diff --git a/docs/rolereference.rst b/docs/rolereference.rst index dd96a844ec9f15c710512f2bc2cd460714845d59..c47623001dc4d6cac995935c66b757847d139793 100644 --- a/docs/rolereference.rst +++ b/docs/rolereference.rst @@ -239,6 +239,8 @@ The role implements the following: * Configures apt to use caching proxy (if any was specified). * Sets-up umask for all logins to ``0027``. * Installs sudo. +* Sets-up uniform bash prompt for all accounts (optionally coloured and with + identifier). This is useful for distinguishing machines and/or environments. * Installs additional base packages, as configured. * Creates additional operating system groups, as configured. * Creates additional operating system users, as configured. @@ -341,6 +343,22 @@ Parameters higher than ``incoming_connection_limit``), even if it would go above the specified connection limit. +**prompt_colour** (string, optional, ``none``) + Colour for showing the Bash prompt. Supported values are: + + ``black``, ``red``, ``green``, ``brown``, ``blue``, ``purple``, ``cyan``, + ``light_gray``, ``dark_gray``, ``light_red``, ``light_green``, ``yellow``, + ``light_blue``, ``light_purple``, ``light_cyan``, ``white``, ``none``. + + You should probably *not* use the ``black`` colour. Setting affects Bash + shells *only*. Setting the value to ``none`` uses default terminal colour. + +**prompt_id** (string, optional, ``NONE``) + Optional identifier appended to regular Bash prompt, useful for visually + identifying distinct environments. For example, if set to ``test``, resulting + prompt will be similar to ``admin@web[test]:~$``. Setting affects Bash shells + *only*. + Examples ~~~~~~~~ @@ -380,6 +398,10 @@ packages on all servers: incoming_connection_limit_burst: 6 + prompt_colour: light_green + + prompt_id: PROD + .. _ldap_client: LDAP Client diff --git a/roles/common/defaults/main.yml b/roles/common/defaults/main.yml index 65a7ef0de0758a98541dc78b86dfd98d0cb8a088..985f053802953c6372fb484b83b2e4888cf80955 100644 --- a/roles/common/defaults/main.yml +++ b/roles/common/defaults/main.yml @@ -6,4 +6,26 @@ os_users: [] os_groups: [] ca_certificates: {} incoming_connection_limit: 3/second -incoming_connection_limit_burst: 9 \ No newline at end of file +incoming_connection_limit_burst: 9 +prompt_colour: none +prompt_id: null + +# Internal use only. +prompt_colour_mapping: + black: "0;30" + red: "0;31" + green: "0;32" + brown: "0;33" + blue: "0;34" + purple: "0;35" + cyan: "0;36" + light_gray: "0;37" + dark_gray: "1;30" + light_red: "1;31" + light_green: "1;32" + yellow: "1;33" + light_blue: "1;34" + light_purple: "1;35" + light_cyan: "1;36" + white: "1;37" + none: "0" \ No newline at end of file diff --git a/roles/common/files/bashrc b/roles/common/files/bashrc new file mode 100644 index 0000000000000000000000000000000000000000..bc96216b61d30d22cb9e93f128624bd6066eabe0 --- /dev/null +++ b/roles/common/files/bashrc @@ -0,0 +1,52 @@ +# System-wide .bashrc file for interactive bash(1) shells. + +# To enable the settings / commands in this file for login shells as well, +# this file has to be sourced in /etc/profile. + +# If not running interactively, don't do anything +[ -z "$PS1" ] && return + +# check the window size after each command and, if necessary, +# update the values of LINES and COLUMNS. +shopt -s checkwinsize + +# set variable identifying the chroot you work in (used in the prompt below) +if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then + debian_chroot=$(cat /etc/debian_chroot) +fi + +# Commented out, don't overwrite xterm -T "title" -n "icontitle" by default. +# If this is an xterm set the title to user@host:dir +#case "$TERM" in +#xterm*|rxvt*) +# PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"' +# ;; +#*) +# ;; +#esac + +# enable bash completion in interactive shells +#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 + +# if the command-not-found package is installed, use it +if [ -x /usr/lib/command-not-found -o -x /usr/share/command-not-found/command-not-found ]; then + function command_not_found_handle { + # check because c-n-f could've been removed in the meantime + if [ -x /usr/lib/command-not-found ]; then + /usr/lib/command-not-found -- "$1" + return $? + elif [ -x /usr/share/command-not-found/command-not-found ]; then + /usr/share/command-not-found/command-not-found -- "$1" + return $? + else + printf "%s: command not found\n" "$1" >&2 + return 127 + fi + } +fi diff --git a/roles/common/files/skel_bashrc b/roles/common/files/skel_bashrc new file mode 100644 index 0000000000000000000000000000000000000000..6b96e712227c2adafeae83ad3a9e462c544f16ad --- /dev/null +++ b/roles/common/files/skel_bashrc @@ -0,0 +1,76 @@ +# ~/.bashrc: executed by bash(1) for non-login shells. +# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc) +# for examples + +# If not running interactively, don't do anything +case $- in + *i*) ;; + *) return;; +esac + +# don't put duplicate lines or lines starting with space in the history. +# See bash(1) for more options +HISTCONTROL=ignoreboth + +# append to the history file, don't overwrite it +shopt -s histappend + +# for setting history length see HISTSIZE and HISTFILESIZE in bash(1) +HISTSIZE=1000 +HISTFILESIZE=2000 + +# check the window size after each command and, if necessary, +# update the values of LINES and COLUMNS. +shopt -s checkwinsize + +# If set, the pattern "**" used in a pathname expansion context will +# match all files and zero or more directories and subdirectories. +#shopt -s globstar + +# make less more friendly for non-text input files, see lesspipe(1) +#[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)" + +# set variable identifying the chroot you work in (used in the prompt below) +if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then + debian_chroot=$(cat /etc/debian_chroot) +fi + +# 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)" + alias ls='ls --color=auto' + #alias dir='dir --color=auto' + #alias vdir='vdir --color=auto' + + #alias grep='grep --color=auto' + #alias fgrep='fgrep --color=auto' + #alias egrep='egrep --color=auto' +fi + +# colored GCC warnings and errors +#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01' + +# some more ls aliases +#alias ll='ls -l' +#alias la='ls -A' +#alias l='ls -CF' + +# Alias definitions. +# You may want to put all your additions into a separate file like +# ~/.bash_aliases, instead of adding them here directly. +# See /usr/share/doc/bash-doc/examples in the bash-doc package. + +if [ -f ~/.bash_aliases ]; then + . ~/.bash_aliases +fi + +# enable programmable completion features (you don't need to enable +# this, if it's already enabled in /etc/bash.bashrc and /etc/profile +# sources /etc/bash.bashrc). +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 diff --git a/roles/common/tasks/main.yml b/roles/common/tasks/main.yml index f7b90d6dc63f986bc337a7002ca7f315c8c5f854..9ea11bc8c81f2508892ee14981c1dbf9f7e8f136 100644 --- a/roles/common/tasks/main.yml +++ b/roles/common/tasks/main.yml @@ -19,6 +19,17 @@ - name: Set home directory mask lineinfile: dest=/etc/adduser.conf state=present backrefs=yes regexp='^DIR_MODE=' line='DIR_MODE=0750' +- name: Deploy bash profile configuration for fancier prompts + template: src="bash_prompt.sh.j2" dest="/etc/profile.d/bash_prompt.sh" + owner=root group=root mode=644 + +- name: Replace default and skeleton bashrc + copy: src="{{ item.key }}" dest="{{ item.value }}" + owner=root group=root mode=644 + with_dict: + skel_bashrc: "/etc/skel/.bashrc" + bashrc: "/etc/bash.bashrc" + - name: Install sudo apt: name=sudo state=present diff --git a/roles/common/templates/bash_prompt.sh.j2 b/roles/common/templates/bash_prompt.sh.j2 new file mode 100644 index 0000000000000000000000000000000000000000..a557071db6b44896ef42716be3c7236d58615341 --- /dev/null +++ b/roles/common/templates/bash_prompt.sh.j2 @@ -0,0 +1,9 @@ +# Set-up colours. +if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then + _colour_terminal=$(tput colors 2>&1) + if [[ -t 1 ]] && (( ${_colour_terminal} > 0 )); then + export PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[{{ prompt_colour_mapping[prompt_colour] }}m\]\u@\h{% if prompt_id %}[{{ prompt_id }}]{% endif %}:\w\$ \[\033[0m\]' + else + export PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h{% if prompt_id %}[{{ prompt_id }}]{% endif %}:\w\$ ' + fi +fi diff --git a/testsite/group_vars/all.yml b/testsite/group_vars/all.yml index d3f3f02b715362ffc71de53c998518e8d3ed5656..f07efee641c1354efe591ee4bd668a64624b831a 100644 --- a/testsite/group_vars/all.yml +++ b/testsite/group_vars/all.yml @@ -79,3 +79,7 @@ backup_server_host_ssh_public_keys: - "{{ lookup('file', inventory_dir + '/ssh/backup_server_ecdsa_key.pub') }}" backup_ssh_key: "{{ lookup('file', inventory_dir + '/ssh/' + ansible_fqdn) }}" + +# Set-up prompt. +prompt_colour: light_purple +prompt_id: MAR \ No newline at end of file