Refactor dotfiles role
This commit is contained in:
@@ -1,115 +1,82 @@
|
||||
---
|
||||
- name: Find home dotfiles (excluding templates)
|
||||
ansible.builtin.find:
|
||||
paths:
|
||||
- "{{ playbook_dir }}/dotfiles/common/home"
|
||||
- "{{ playbook_dir }}/dotfiles/{{ group_names[0] }}/home"
|
||||
recurse: true
|
||||
file_type: file
|
||||
hidden: true
|
||||
excludes: "*.j2"
|
||||
delegate_to: localhost
|
||||
register: dotfiles_home_files
|
||||
run_once: true
|
||||
ignore_errors: true
|
||||
- name: Build file lists
|
||||
ansible.builtin.set_fact:
|
||||
dotfiles_files_home: "{{ lookup('community.general.filetree',
|
||||
playbook_dir ~ '/dotfiles/common/home',
|
||||
playbook_dir ~ '/dotfiles/' ~ group_names[0] ~ '/home') | selectattr('state', 'equalto', 'file') | rejectattr('path', 'match', '.*\\.j2$') | list }}"
|
||||
dotfiles_templates_home: "{{ lookup('community.general.filetree',
|
||||
playbook_dir ~ '/dotfiles/common/home',
|
||||
playbook_dir ~ '/dotfiles/' ~ group_names[0] ~ '/home') | selectattr('state', 'equalto', 'file') | selectattr('path', 'match', '.*\\.j2$') | list }}"
|
||||
dotfiles_files_root: "{{ lookup('community.general.filetree',
|
||||
playbook_dir ~ '/dotfiles/common/root',
|
||||
playbook_dir ~ '/dotfiles/' ~ group_names[0] ~ '/root') | selectattr('state', 'equalto', 'file') | rejectattr('path', 'match', '.*\\.j2$') | list }}"
|
||||
dotfiles_templates_root: "{{ lookup('community.general.filetree',
|
||||
playbook_dir ~ '/dotfiles/common/root',
|
||||
playbook_dir ~ '/dotfiles/' ~ group_names[0] ~ '/root') | selectattr('state', 'equalto', 'file') | selectattr('path', 'match', '.*\\.j2$') | list }}"
|
||||
|
||||
- name: Find root dotfiles (excluding templates)
|
||||
ansible.builtin.find:
|
||||
paths:
|
||||
- "{{ playbook_dir }}/dotfiles/common/root"
|
||||
- "{{ playbook_dir }}/dotfiles/{{ group_names[0] }}/root"
|
||||
recurse: true
|
||||
file_type: file
|
||||
hidden: true
|
||||
excludes: "*.j2"
|
||||
delegate_to: localhost
|
||||
register: dotfiles_root_files
|
||||
run_once: true
|
||||
ignore_errors: true
|
||||
|
||||
- name: Find home template files
|
||||
ansible.builtin.find:
|
||||
paths:
|
||||
- "{{ playbook_dir }}/dotfiles/common/home"
|
||||
- "{{ playbook_dir }}/dotfiles/{{ group_names[0] }}/home"
|
||||
recurse: true
|
||||
file_type: file
|
||||
hidden: true
|
||||
patterns: "*.j2"
|
||||
delegate_to: localhost
|
||||
register: dotfiles_home_templates
|
||||
run_once: true
|
||||
ignore_errors: true
|
||||
|
||||
- name: Find root template files
|
||||
ansible.builtin.find:
|
||||
paths:
|
||||
- "{{ playbook_dir }}/dotfiles/common/root"
|
||||
- "{{ playbook_dir }}/dotfiles/{{ group_names[0] }}/root"
|
||||
recurse: true
|
||||
file_type: file
|
||||
hidden: true
|
||||
patterns: "*.j2"
|
||||
delegate_to: localhost
|
||||
register: dotfiles_root_templates
|
||||
run_once: true
|
||||
ignore_errors: true
|
||||
|
||||
- name: Symlink home dotfiles (local)
|
||||
- name: Ensure home directories
|
||||
ansible.builtin.file:
|
||||
src: "{{ item.path }}"
|
||||
dest: "{{ ansible_facts['env']['HOME'] }}/{{ item.path | regex_replace('^.+/dotfiles/(?:common|' + group_names[0] + ')/home/', '') }}"
|
||||
state: link
|
||||
force: true
|
||||
loop: "{{ dotfiles_home_files.files }}"
|
||||
when: ansible_connection in ['local', 'localhost']
|
||||
path: "{{ ansible_facts.env.HOME }}/{{ item.path | dirname }}"
|
||||
state: directory
|
||||
mode: "{{ '0700' if (item.path | dirname).startswith('.ssh') else '0755' }}"
|
||||
loop: "{{ dotfiles_files_home + dotfiles_templates_home }}"
|
||||
when: item.path | dirname != ''
|
||||
|
||||
- name: Symlink root dotfiles (local)
|
||||
ansible.builtin.file:
|
||||
src: "{{ item.path }}"
|
||||
dest: "/{{ item.path | regex_replace('^.+/dotfiles/(?:common|' + group_names[0] + ')/root/', '') }}"
|
||||
state: link
|
||||
force: true
|
||||
loop: "{{ dotfiles_root_files.files }}"
|
||||
when: ansible_connection in ['local', 'localhost']
|
||||
become: true
|
||||
|
||||
- name: Deploy home dotfiles (remote)
|
||||
ansible.builtin.copy:
|
||||
src: "{{ item.path }}"
|
||||
dest: "{{ ansible_facts['env']['HOME'] }}/{{ item.path | regex_replace('^.+/dotfiles/(?:common|' + group_names[0] + ')/home/', '') }}"
|
||||
mode: preserve
|
||||
loop: "{{ dotfiles_home_files.files }}"
|
||||
when: ansible_connection not in ['local', 'localhost']
|
||||
|
||||
- name: Deploy root dotfiles (remote)
|
||||
ansible.builtin.copy:
|
||||
src: "{{ item.path }}"
|
||||
dest: "/{{ item.path | regex_replace('^.+/dotfiles/(?:common|' + group_names[0] + ')/root/', '') }}"
|
||||
mode: preserve
|
||||
loop: "{{ dotfiles_root_files.files }}"
|
||||
when: ansible_connection not in ['local', 'localhost']
|
||||
become: true
|
||||
|
||||
- name: Template home dotfiles
|
||||
ansible.builtin.template:
|
||||
src: "{{ item.path }}"
|
||||
dest: "{{ ansible_facts['env']['HOME'] }}/{{ item.path | regex_replace('^.+/dotfiles/(?:common|' + group_names[0] + ')/home/', '') | replace('.j2', '') }}"
|
||||
mode: '0600'
|
||||
loop: "{{ dotfiles_home_templates.files }}"
|
||||
|
||||
- name: Template root dotfiles
|
||||
ansible.builtin.template:
|
||||
src: "{{ item.path }}"
|
||||
dest: "/{{ item.path | regex_replace('^.+/dotfiles/(?:common|' + group_names[0] + ')/root/', '') | replace('.j2', '') }}"
|
||||
mode: '0600'
|
||||
loop: "{{ dotfiles_root_templates.files }}"
|
||||
become: true
|
||||
|
||||
- name: Deploy symlinks
|
||||
- name: Deploy home files (local)
|
||||
ansible.builtin.file:
|
||||
src: "{{ item.src }}"
|
||||
dest: "{{ item.dest }}"
|
||||
dest: "{{ ansible_facts.env.HOME }}/{{ item.path }}"
|
||||
state: link
|
||||
force: true
|
||||
loop: "{{ symlinks | default([]) }}"
|
||||
loop: "{{ dotfiles_files_home }}"
|
||||
when: ansible_connection in ['local', 'localhost']
|
||||
|
||||
- name: Deploy home files (remote)
|
||||
ansible.builtin.copy:
|
||||
src: "{{ item.src }}"
|
||||
dest: "{{ ansible_facts.env.HOME }}/{{ item.path }}"
|
||||
mode: preserve
|
||||
loop: "{{ dotfiles_files_home }}"
|
||||
when: ansible_connection not in ['local', 'localhost']
|
||||
|
||||
- name: Render home templates
|
||||
ansible.builtin.template:
|
||||
src: "{{ item.src }}"
|
||||
dest: "{{ ansible_facts.env.HOME }}/{{ item.path | replace('.j2', '') }}"
|
||||
mode: preserve
|
||||
loop: "{{ dotfiles_templates_home }}"
|
||||
|
||||
- name: Ensure root directories
|
||||
ansible.builtin.file:
|
||||
path: "/{{ item.path | dirname }}"
|
||||
state: directory
|
||||
mode: '0755'
|
||||
loop: "{{ dotfiles_files_root + dotfiles_templates_root }}"
|
||||
become: true
|
||||
|
||||
- name: Deploy root files (local)
|
||||
ansible.builtin.file:
|
||||
src: "{{ item.src }}"
|
||||
dest: "/{{ item.path }}"
|
||||
state: link
|
||||
force: true
|
||||
loop: "{{ dotfiles_files_root }}"
|
||||
when: ansible_connection in ['local', 'localhost']
|
||||
become: true
|
||||
|
||||
- name: Deploy root files (remote)
|
||||
ansible.builtin.copy:
|
||||
src: "{{ item.src }}"
|
||||
dest: "/{{ item.path }}"
|
||||
mode: preserve
|
||||
loop: "{{ dotfiles_files_root }}"
|
||||
when: ansible_connection not in ['local', 'localhost']
|
||||
become: true
|
||||
|
||||
- name: Render root templates
|
||||
ansible.builtin.template:
|
||||
src: "{{ item.src }}"
|
||||
dest: "/{{ item.path | replace('.j2', '') }}"
|
||||
mode: preserve
|
||||
loop: "{{ dotfiles_templates_root }}"
|
||||
become: true
|
||||
|
||||
Reference in New Issue
Block a user