From 9dbc6881714273c13c79fd225805732ab62c4cdd Mon Sep 17 00:00:00 2001 From: eneller Date: Fri, 31 Mar 2023 21:38:48 +0200 Subject: [PATCH] feat: install dotfiles (improvements needed) --- hosts | 4 ++ install_dotfiles.yml | 8 ++++ requirements.yml | 2 - roles/install_dockerCompose/tasks/main.yml | 4 +- roles/install_dotfiles/defaults/main.yml | 25 ++++++++-- roles/install_dotfiles/tasks/main.yml | 53 ++++++++++++++++------ 6 files changed, 75 insertions(+), 21 deletions(-) create mode 100644 install_dotfiles.yml diff --git a/hosts b/hosts index b7607c5..06b67b4 100644 --- a/hosts +++ b/hosts @@ -1,13 +1,16 @@ [desktop] +# dont put localhost here, ansible will attempt to connect via ssh [headless] server[1:7] openmediavault +kali [oracleServer] server[1:7] [home] +kali [ubuntu] server[1:7] @@ -15,6 +18,7 @@ server[1:7] [arm64] server[2:6:2] server7 +kali [amd64] server[1:5:2] diff --git a/install_dotfiles.yml b/install_dotfiles.yml new file mode 100644 index 0000000..babf661 --- /dev/null +++ b/install_dotfiles.yml @@ -0,0 +1,8 @@ +--- +- name: Install dotfiles on target + hosts: 'kali' + + tasks: + - name: Install dotfiles + include_role: + name: install_dotfiles diff --git a/requirements.yml b/requirements.yml index ec2fb95..b62ae1b 100644 --- a/requirements.yml +++ b/requirements.yml @@ -1,8 +1,6 @@ --- roles: - name: jan_matthis.git_dotfiles - - name: geerlingguy.dotfiles - version: "1.2.1" - name: geerlingguy.docker version: "6.1.0" - name: geerlingguy.git diff --git a/roles/install_dockerCompose/tasks/main.yml b/roles/install_dockerCompose/tasks/main.yml index 374d265..64a264a 100644 --- a/roles/install_dockerCompose/tasks/main.yml +++ b/roles/install_dockerCompose/tasks/main.yml @@ -19,7 +19,7 @@ - name: Add Docker Repository become: true apt_repository: - repo: "deb [arch={{ arch }}] https://download.docker.com/linux/{{ansible_distribution|lower}} {{ansible_distribution_release}} {{release_branch_docker}}" + repo: "deb [arch={{ arch }}] https://download.docker.com/linux/{{ ansible_distribution|lower }} {{ ansible_distribution_release }} {{ release_branch_docker }}" state: present - name: Install Docker packages @@ -43,5 +43,5 @@ - name: Create docker folder in home directory file: - path: "{{ansible_user_dir}}/docker" + path: "{{ ansible_user_dir }}/docker" state: directory diff --git a/roles/install_dotfiles/defaults/main.yml b/roles/install_dotfiles/defaults/main.yml index 1255a1f..ddf67db 100644 --- a/roles/install_dotfiles/defaults/main.yml +++ b/roles/install_dotfiles/defaults/main.yml @@ -1,3 +1,22 @@ -dotfiles_repo_source: "git@github.com:eneller/.dotfiles" -dotfiles_repo_commit: "headless" -dotfiles_repo_destination: ".dotfiles" \ No newline at end of file +# The git source of your dotfiles. Will be cloned as a bare repository. +# Make sure you have set up git correctly before using. +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 }}" \ No newline at end of file diff --git a/roles/install_dotfiles/tasks/main.yml b/roles/install_dotfiles/tasks/main.yml index 9c0c5ff..3b33f1b 100644 --- a/roles/install_dotfiles/tasks/main.yml +++ b/roles/install_dotfiles/tasks/main.yml @@ -1,29 +1,54 @@ - name: Check for existing dotfiles repository ansible.builtin.stat: - path: "{{ dotfiles_repo_destination }}HEAD" + path: "{{ dotfiles_git_dir }}/HEAD" register: dotfiles_repo_head - changed_when: false + changed_when: false # this never applies changes + when: dotfiles_do_backup - name: Clone into /tmp ansible.builtin.git: - dest: /tmp/dotfiles + dest: "{{ dotfiles_tmp_dir }}" repo: "{{ dotfiles_repo_source }}" - version: "{{ dotfiles_repo_commit }}" + version: "{{ dotfiles_repo_version }}" when: not dotfiles_repo_head.stat.exists -- name: Back up existing dotfiles - ansible.builtin.shell: - chdir: /tmp/dotfiles - cmd: | - git ls-files | while read -r line; do - mv $HOME/$line $HOME/$line.bak - done - # when: # TODO only if bare repo doesnt already exist + +- name: Get list of dotfiles + ansible.builtin.command: + chdir: "{{ dotfiles_tmp_dir }}" + cmd: git ls-files + register: dotfiles_lsfiles # needs to be used with .stdout_lines for iteratio 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 ansible.builtin.git: bare: 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 }}" - 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