Refactor dotfiles role
This commit is contained in:
@@ -1,115 +1,82 @@
|
|||||||
---
|
---
|
||||||
- name: Find home dotfiles (excluding templates)
|
- name: Build file lists
|
||||||
ansible.builtin.find:
|
ansible.builtin.set_fact:
|
||||||
paths:
|
dotfiles_files_home: "{{ lookup('community.general.filetree',
|
||||||
- "{{ playbook_dir }}/dotfiles/common/home"
|
playbook_dir ~ '/dotfiles/common/home',
|
||||||
- "{{ playbook_dir }}/dotfiles/{{ group_names[0] }}/home"
|
playbook_dir ~ '/dotfiles/' ~ group_names[0] ~ '/home') | selectattr('state', 'equalto', 'file') | rejectattr('path', 'match', '.*\\.j2$') | list }}"
|
||||||
recurse: true
|
dotfiles_templates_home: "{{ lookup('community.general.filetree',
|
||||||
file_type: file
|
playbook_dir ~ '/dotfiles/common/home',
|
||||||
hidden: true
|
playbook_dir ~ '/dotfiles/' ~ group_names[0] ~ '/home') | selectattr('state', 'equalto', 'file') | selectattr('path', 'match', '.*\\.j2$') | list }}"
|
||||||
excludes: "*.j2"
|
dotfiles_files_root: "{{ lookup('community.general.filetree',
|
||||||
delegate_to: localhost
|
playbook_dir ~ '/dotfiles/common/root',
|
||||||
register: dotfiles_home_files
|
playbook_dir ~ '/dotfiles/' ~ group_names[0] ~ '/root') | selectattr('state', 'equalto', 'file') | rejectattr('path', 'match', '.*\\.j2$') | list }}"
|
||||||
run_once: true
|
dotfiles_templates_root: "{{ lookup('community.general.filetree',
|
||||||
ignore_errors: true
|
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)
|
- name: Ensure home directories
|
||||||
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)
|
|
||||||
ansible.builtin.file:
|
ansible.builtin.file:
|
||||||
src: "{{ item.path }}"
|
path: "{{ ansible_facts.env.HOME }}/{{ item.path | dirname }}"
|
||||||
dest: "{{ ansible_facts['env']['HOME'] }}/{{ item.path | regex_replace('^.+/dotfiles/(?:common|' + group_names[0] + ')/home/', '') }}"
|
state: directory
|
||||||
state: link
|
mode: "{{ '0700' if (item.path | dirname).startswith('.ssh') else '0755' }}"
|
||||||
force: true
|
loop: "{{ dotfiles_files_home + dotfiles_templates_home }}"
|
||||||
loop: "{{ dotfiles_home_files.files }}"
|
when: item.path | dirname != ''
|
||||||
when: ansible_connection in ['local', 'localhost']
|
|
||||||
|
|
||||||
- name: Symlink root dotfiles (local)
|
- name: Deploy home files (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
|
|
||||||
ansible.builtin.file:
|
ansible.builtin.file:
|
||||||
src: "{{ item.src }}"
|
src: "{{ item.src }}"
|
||||||
dest: "{{ item.dest }}"
|
dest: "{{ ansible_facts.env.HOME }}/{{ item.path }}"
|
||||||
state: link
|
state: link
|
||||||
force: true
|
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