AnsiblePlaybooksHistory timestamps

Enabling history timestamps

By default, the ouput of the history command does not include timestamps. This playbook aims to change that.

Creating the playbook

Executed:

aadmin@uvm0:~/code/it-wiz/devops/ansible/projects/playbooks$ touch enable_history_timstamps.yml

Brought enable_history_timstamps.yml to the following form:

- name: Updating /etc/bash.bashrc
  hosts: all
  gather_facts: no

  tasks:
  - name: Checking if the HISTTIMEFORMAT line exists
    shell: grep -q '^HISTTIMEFORMAT=' /etc/bash.bashrc
    register: histtimeformat_line
    failed_when: false

  - name: Create /etc/bash.bashrc.bak
    copy:
      src: /etc/bash.bashrc
      dest: /etc/bash.bashrc.bak
      remote_src: yes
    when: histtimeformat_line.rc != 0

  - name: Adding the HISTTIMEFORMAT line and its comment
    shell: echo "\n# add timestamps to the command history\nHISTTIMEFORMAT=\"%Y.%m.%d %T \"\n" >> /etc/bash.bashrc
    when: histtimeformat_line.rc != 0

Intended result

If the HISTTIMEFORMAT line exists in a /etc/bash.bashrc file, no changes are made. If it doesn’t, the following block gets added at the end of /etc/bash.bashrc:

# add timestamps to the command history
HISTTIMEFORMAT="%Y.%m.%d %T "

Explanations

The first task determines if /etc/bash.bashrc needs to be changed and registers the histtimeformat_line variable. The last two tasks get executed based on the value of this variable.

The playbook should be executed only once. Nevertheless, multiple executions should have no effect on nodes that were already updated.

First execution of the playbook

Executed:

aadmin@uvm0:~/code/it-wiz/devops/ansible/projects$ ansible-playbook playbooks/enable_history_timstamps.yml --ask-vault-pass

Details

    aadmin@uvm0:~/code/it-wiz/devops/ansible/projects$ ansible-playbook playbooks/enable_history_timstamps.yml --ask-vault-pass
    Vault password: <typed the vault password>
 
    PLAY [Updating /etc/bash.bashrc] *********************************************************************************
 
    TASK [Checking if the HISTTIMEFORMAT line exists] ****************************************************************
    changed: [uvm27]
    changed: [uvm26]
 
    TASK [Create /etc/bash.bashrc.bak] *******************************************************************************
    ok: [uvm26]
    ok: [uvm27]
 
    TASK [Adding the HISTTIMEFORMAT line] ****************************************************************************
    changed: [uvm26]
    changed: [uvm27]
 
    PLAY RECAP *******************************************************************************************************
    uvm26                      : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    uvm27                      : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Result

The HISTTIMEFORMAT line was not found. A backup got created. The following block got added at the end of /etc/bash.bashrc:

# add timestamps to the command history
HISTTIMEFORMAT="%Y.%m.%d %T "

Subsequent executions of the playbook

Executed:

aadmin@uvm0:~/code/it-wiz/devops/ansible/projects$ ansible-playbook playbooks/enable_history_timstamps.yml --ask-vault-pass

Details

    aadmin@uvm0:~/code/it-wiz/devops/ansible/projects$ ansible-playbook playbooks/enable_history_timstamps.yml --ask-vault-pass
    Vault password: <typed the vault password>
 
    PLAY [Updating /etc/bash.bashrc] ************************************************************************************
 
    TASK [Checking if the HISTTIMEFORMAT line exists] *******************************************************************
    changed: [uvm26]
    changed: [uvm27]
 
    TASK [Create /etc/bash.bashrc.bak] **********************************************************************************
    skipping: [uvm26]
    skipping: [uvm27]
 
    TASK [Adding the HISTTIMEFORMAT line] *******************************************************************************
    skipping: [uvm26]
    skipping: [uvm27]
 
    PLAY RECAP **********************************************************************************************************
    uvm26                      : ok=1    changed=1    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0
    uvm27                      : ok=1    changed=1    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0

Result

The HISTTIMEFORMAT line was found. No changes were made.