From 8927cc07e63c782360ff0002cf2649cefea9d28d Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Mon, 10 Jan 2022 05:24:47 +0100 Subject: [PATCH] Feat: dotfiles deployement tested and works --- roles/dotfiles/defaults/main.yml | 6 +-- roles/dotfiles/files/autofs.nas | 2 - roles/dotfiles/molecule/default/converge.yml | 37 ++++++++++++++++++- roles/dotfiles/molecule/default/molecule.yml | 2 + .../molecule/default/tests/test_default.py | 22 +++++++++-- roles/dotfiles/tasks/home_mounts.yml | 18 --------- roles/dotfiles/tasks/main.yml | 23 ++++++------ 7 files changed, 70 insertions(+), 40 deletions(-) delete mode 100644 roles/dotfiles/files/autofs.nas delete mode 100644 roles/dotfiles/tasks/home_mounts.yml diff --git a/roles/dotfiles/defaults/main.yml b/roles/dotfiles/defaults/main.yml index fee5eb8..3d498bf 100644 --- a/roles/dotfiles/defaults/main.yml +++ b/roles/dotfiles/defaults/main.yml @@ -1,8 +1,4 @@ --- -me: - username: 'user1' +username: 'user1' config2stow: ["nvim", "tmux", "zsh"] - -autofs: - - host: nas diff --git a/roles/dotfiles/files/autofs.nas b/roles/dotfiles/files/autofs.nas deleted file mode 100644 index ecee41e..0000000 --- a/roles/dotfiles/files/autofs.nas +++ /dev/null @@ -1,2 +0,0 @@ -Nas_commun -rw,soft,intr,rsize=8192,wsize=8192 nas:/mnt/DocNas/Commun -Nas_perso -rw,soft,intr,rsize=8192,wsize=8192 nas:/mnt/DocNas/Benjamin diff --git a/roles/dotfiles/molecule/default/converge.yml b/roles/dotfiles/molecule/default/converge.yml index c316079..aef6149 100644 --- a/roles/dotfiles/molecule/default/converge.yml +++ b/roles/dotfiles/molecule/default/converge.yml @@ -1,8 +1,43 @@ --- - name: Converge hosts: all + vars: + users: + - username: user1 + config2stow: ["nvim", "tmux", "zsh"] + password: "$y$j9T$PR8GfM2MjGudOCd7hF9NP1$/qRGtuNuWaRVVGmB1A4rgtaT0MMB9IoB4fnaxW1kvf4" #plop + - username: user2 + password: "$y$j9T$PR8GfM2MjGudOCd7hF9NP1$/qRGtuNuWaRVVGmB1A4rgtaT0MMB9IoB4fnaxW1kvf4" #plop + config2stow: ["nvim", "tmux"] + + pre_tasks: + - name: update_cache for arch + pacman: + update_cache: yes + become: yes + when: ansible_os_family == "Archlinux" + + - name: update_cache for debian + apt: + update_cache: yes + become: yes + when: ansible_os_family == "Debian" + + - name: Create user + ansible.builtin.user: + name: "{{ item.username }}" + password: "{{ item.password }}" #plop + state: present + with_items: "{{ users }}" + become: yes + tasks: - name: "Include dotfiles" include_role: name: "dotfiles" - become: true + vars: + username: "{{ user.username }}" + config2stow: "{{ user.config2stow }}" + loop: "{{ users }}" + loop_control: + loop_var: user diff --git a/roles/dotfiles/molecule/default/molecule.yml b/roles/dotfiles/molecule/default/molecule.yml index ba85140..40a4759 100644 --- a/roles/dotfiles/molecule/default/molecule.yml +++ b/roles/dotfiles/molecule/default/molecule.yml @@ -12,3 +12,5 @@ provisioner: name: ansible verifier: name: testinfra + options: + sudo: true diff --git a/roles/dotfiles/molecule/default/tests/test_default.py b/roles/dotfiles/molecule/default/tests/test_default.py index ea997a3..5a3de91 100644 --- a/roles/dotfiles/molecule/default/tests/test_default.py +++ b/roles/dotfiles/molecule/default/tests/test_default.py @@ -1,17 +1,23 @@ """Role testing files using testinfra.""" +USER1 = "user1" +USER2 = "user2" def test_stow_installed(host): stow = host.package("stow") assert stow.is_installed def test_dotfile_cloned(host): - homepath = host.run("pwd").stdout[:-1] - dotfiles = host.file(homepath + "/.dotfiles") + dotfiles = host.file(f"/home/{USER1}/.dotfiles") assert dotfiles.exists + assert dotfiles.user == USER1 + + dotfiles = host.file(f"/home/{USER2}/.dotfiles") + assert dotfiles.exists + assert dotfiles.user == USER2 def test_dotfile_stowed(host): - homepath = host.run("pwd").stdout[:-1] + homepath = f"/home/{USER1}" nvim = host.file(homepath + "/.config/nvim/") assert nvim.exists tmux = host.file(homepath + "/.tmux/") @@ -21,3 +27,13 @@ def test_dotfile_stowed(host): zsh = host.file(homepath + "/.config/zsh/") assert zsh.exists + homepath = f"/home/{USER2}" + nvim = host.file(homepath + "/.config/nvim/") + assert nvim.exists + tmux = host.file(homepath + "/.tmux/") + assert tmux.exists + zshenv = host.file(homepath + "/.zshenv") + assert not zshenv.exists + zsh = host.file(homepath + "/.config/zsh/") + assert not zsh.exists + diff --git a/roles/dotfiles/tasks/home_mounts.yml b/roles/dotfiles/tasks/home_mounts.yml deleted file mode 100644 index eae6f6d..0000000 --- a/roles/dotfiles/tasks/home_mounts.yml +++ /dev/null @@ -1,18 +0,0 @@ ---- -- name: Create NAS share directory - file: - path: "/media/{{ item.host }}" - state: directory - with_items: "{{ autofs }}" - -- name: Activate NAS shares for autofs - lineinfile: - path: /etc/autofs/auto.master - line: "/media/{{ item.host }} /etc/autofs/auto.{{ item.host }} --timeout=60 --browse" - with_items: "{{ autofs }}" - -- name: NAS autofs config file - template: - src: "files/autofs.{{ item.host }}" - dest: "/etc/autofs/auto.{{ item.host }}" - with_items: "{{ autofs }}" diff --git a/roles/dotfiles/tasks/main.yml b/roles/dotfiles/tasks/main.yml index fc01b1c..82ce634 100644 --- a/roles/dotfiles/tasks/main.yml +++ b/roles/dotfiles/tasks/main.yml @@ -10,23 +10,24 @@ - name: Clone dotfiles ansible.builtin.git: repo: 'https://git.opytex.org/lafrite/dotfiles.git' - dest: ~/.dotfiles - become: true - become_user: "{{ me.username }}" + dest: "/home/{{ username }}/.dotfiles" + become: yes + +- name: user owns its dotfiles + ansible.builtin.file: + path: "/home/{{ username }}/.dotfiles" + owner: "{{ username }}" + become: yes - name: stow configs ansible.builtin.command: cmd: stow {{ item }} - chdir: ~/.dotfiles/ + chdir: "/home/{{ username }}/.dotfiles" with_items: "{{ config2stow }}" - become: true - become_user: "{{ me.username}}" - -- name: home mounts - import_tasks: home_mounts.yml + become: yes - name: Gnome config import_tasks: gnome.yml - become: true - become_user: "{{ me.username }}" + #become: true + become_user: "{{ username }}" when: no # soucis avec la version de psutil