---
- name: install tmux 
  hosts: all
  become: true  


  tasks:
  - name: apt_update
    ansible.builtin.apt:
      update_cache: yes

  - name: Install tmux on Ubuntu/Debian
    apt:
      name: tmux
      state: present
    become: true

  
  - name: Append tmux script  autostart to /root/.bashrc
    blockinfile:
      path: /root/.bashrc
      block: |
          if [[ -z $TMUX && -n $SSH_TTY ]]; then
              me=$(whoami)
              real_tmux=$(command -v tmux)

              if [ -z $real_tmux ]; then
                  echo "No tmux installed."
              fi

              export TERM="xterm-256color"

              if $real_tmux has-session -t $me 2>/dev/null; then
                  $real_tmux attach-session -t $me
              else
                  if [[ -n $SSH_TTY ]]; then
                      (tmux new-session -d -s $me && tmux attach-session -t $me)
                  fi
              fi
          fi
