feat: install dotfiles (improvements needed)

This commit is contained in:
eneller
2023-03-31 21:38:48 +02:00
parent d393ba8b57
commit 9dbc688171
6 changed files with 75 additions and 21 deletions

4
hosts
View File

@@ -1,13 +1,16 @@
[desktop] [desktop]
# dont put localhost here, ansible will attempt to connect via ssh
[headless] [headless]
server[1:7] server[1:7]
openmediavault openmediavault
kali
[oracleServer] [oracleServer]
server[1:7] server[1:7]
[home] [home]
kali
[ubuntu] [ubuntu]
server[1:7] server[1:7]
@@ -15,6 +18,7 @@ server[1:7]
[arm64] [arm64]
server[2:6:2] server[2:6:2]
server7 server7
kali
[amd64] [amd64]
server[1:5:2] server[1:5:2]

8
install_dotfiles.yml Normal file
View File

@@ -0,0 +1,8 @@
---
- name: Install dotfiles on target
hosts: 'kali'
tasks:
- name: Install dotfiles
include_role:
name: install_dotfiles

View File

@@ -1,8 +1,6 @@
--- ---
roles: roles:
- name: jan_matthis.git_dotfiles - name: jan_matthis.git_dotfiles
- name: geerlingguy.dotfiles
version: "1.2.1"
- name: geerlingguy.docker - name: geerlingguy.docker
version: "6.1.0" version: "6.1.0"
- name: geerlingguy.git - name: geerlingguy.git

View File

@@ -1,3 +1,22 @@
dotfiles_repo_source: "git@github.com:eneller/.dotfiles" # The git source of your dotfiles. Will be cloned as a bare repository.
dotfiles_repo_commit: "headless" # Make sure you have set up git correctly before using.
dotfiles_repo_destination: ".dotfiles" dotfiles_repo_source: "https://github.com/eneller/.dotfiles.git"
# can be branch name or commit hash, used for ansible.builtin.git version
dotfiles_repo_version: "headless"
# Will become the parent-directory of your dotfiles
dotfiles_dest: "{{ ansible_user_dir }}"
# The folder name to use for the bare repository
dotfiles_git_dir: "{{ ansible_user_dir}}/.dotfiles"
# Decide whether to back up already existing dotfiles that would conflict with your repo.
# Will overwrite them if false
dotfiles_do_backup: true
# Uses a temporary directory to run git ls-files if {{ dotfiles_do_backup }} is true
dotfiles_tmp_dir: "/tmp/dotfiles"
# An alias to access the bare repo for internal use
dotfiles_alias: "git --git-dir={{ dotfiles_git_dir }} --work-tree={{ dotfiles_dest }}"

View File

@@ -1,29 +1,54 @@
- name: Check for existing dotfiles repository - name: Check for existing dotfiles repository
ansible.builtin.stat: ansible.builtin.stat:
path: "{{ dotfiles_repo_destination }}HEAD" path: "{{ dotfiles_git_dir }}/HEAD"
register: dotfiles_repo_head register: dotfiles_repo_head
changed_when: false changed_when: false # this never applies changes
when: dotfiles_do_backup
- name: Clone into /tmp - name: Clone into /tmp
ansible.builtin.git: ansible.builtin.git:
dest: /tmp/dotfiles dest: "{{ dotfiles_tmp_dir }}"
repo: "{{ dotfiles_repo_source }}" repo: "{{ dotfiles_repo_source }}"
version: "{{ dotfiles_repo_commit }}" version: "{{ dotfiles_repo_version }}"
when: not dotfiles_repo_head.stat.exists when: not dotfiles_repo_head.stat.exists
- name: Back up existing dotfiles
ansible.builtin.shell: - name: Get list of dotfiles
chdir: /tmp/dotfiles ansible.builtin.command:
cmd: | chdir: "{{ dotfiles_tmp_dir }}"
git ls-files | while read -r line; do cmd: git ls-files
mv $HOME/$line $HOME/$line.bak register: dotfiles_lsfiles # needs to be used with .stdout_lines for iteratio
done
# when: # TODO only if bare repo doesnt already exist
when: not dotfiles_repo_head.stat.exists when: not dotfiles_repo_head.stat.exists
- name: Back up dotfiles that would be overwritten by checkout
ansible.builtin.command:
cmd: "mv {{ dotfiles_dest }}/{{ item }} {{ dotfiles_dest }}/{{ item }}.bak"
args:
creates: "{{ dotfiles_dest }}/{{ item }}.bak"
removes: "{{ dotfiles_dest }}/{{ item }}"
with_items: "{{ dotfiles_lsfiles.stdout_lines }}"
when: not dotfiles_repo_head.stat.exists
- name: Initialize dotfiles repository in user home - name: Initialize dotfiles repository in user home
ansible.builtin.git: ansible.builtin.git:
bare: true bare: true
update: true update: true
dest: "{{ ansible_user_directory }}/{{ dotfiles_repo_destination }}" force: true # this is okay here because we backed up already existing files
dest: "{{ dotfiles_git_dir }}"
repo: "{{ dotfiles_repo_source }}" repo: "{{ dotfiles_repo_source }}"
version: "{{ dotfiles_repo_commit }}" version: "{{ dotfiles_repo_version }}"
# TODO improve these cmd tasks, i dont really know how though
# i dont know why this checkout is necessary, was expecting the previous task to do this
- name: Configure git repository
ansible.builtin.command:
cmd: |
{{ dotfiles_alias }}
checkout {{ dotfiles_repo_version }}
--force
- name: Configure git repository
ansible.builtin.command:
cmd: |
{{ dotfiles_alias }}
config status.showUntrackedFiles no