Thoughts on Design and Computation

Decorating New Homes

One of the first tasks after launching a new remote host is getting comfortable in a new home…​ directory, that is. This is my little script to automate a couple creature comforts I like to have configured for remote systems that I mainly interact with over SSH.

Decorations Include
  1. Copy SSH ID

  2. Setup tmux to autolaunch on SSH connection

Most of the heavy lifting is offloaded to Ansible.

You can find the project on my Gitlab site at https://gitlab.com/borgermatthew/home-decorator.

Decorate Script Snippet
ssh-copy-id $1
ansible-playbook --inventory $1, decorate.yml
Tip
The key to making this work for ad-hoc hosts is that Ansible does support providing hosts as a commandline argument. It’s also done through --inventory but if it’s a comma separated list, or a single entry with a comma suffix, then Ansible uses that argument as the host list instead of looking for a named inventory file. A hidden gem!

Simple execute decorate with a hostname or address of the remote system you want to decorate. Currently the remote username is not configurable, SSH and Ansible will assume the current username.

Ansible Playbook
---
- name: Decorate home
  hosts: all
  tasks:
    - name: Check for package
      ansible.builtin.shell: command -v {{ item }}
      changed_when: no
      loop:
        - tmux
        - htop

    - name: Copy tmux config
      ansible.builtin.copy:
        dest: ~/.config/tmux/tmux.conf
        content: |
          # Detach and hangup parent process, useful for exiting an SSH session
          bind-key Q detach-client -P

          new-session -s ssh -n htop htop

    - name: Append profile config
      ansible.builtin.blockinfile:
        path: ~/.profile
        block: |
          export EDITOR=vim
          alias ls='ls --color=auto'

          # if tmux is executable and not inside a tmux session, then try to attach.
          # if attachment fails, start a new session
          [ -x "$(command -v tmux)" ] \
            && [ -z "${TMUX}" ] \
            && { tmux attach -t ssh || tmux new -s ssh -n htop htop; } >/dev/null 2>&1