Refactor Ansible tasks and templates for improved consistency and clarity

This commit is contained in:
2026-07-31 02:24:42 +02:00
parent 81ef678904
commit 46327da2ac
52 changed files with 1679 additions and 1461 deletions
+90 -56
View File
@@ -6,16 +6,22 @@
# system_user
- name: (system-user-systemfiles.yml) Check if local template directory exists for default users
local_action: stat path={{ inventory_dir }}/files/{{ nis_domain }}/homedirs/{{ item.name }}
ansible.builtin.stat:
path: "{{ inventory_dir }}/files/{{ nis_domain }}/homedirs/{{ item.name }}"
delegate_to: localhost
become: false
with_items: "{{ system_users }}"
loop_control:
label: '{{ item.name }}'
register: local_template_dir_system_users
register: common_local_template_dir_system_users
# root
- name: (system-user-systemfiles.yml) Check if local template directory exists for root
local_action: stat path={{ inventory_dir }}/files/{{ nis_domain }}/homedirs/root
register: local_template_dir_root
ansible.builtin.stat:
path: "{{ inventory_dir }}/files/{{ nis_domain }}/homedirs/root"
delegate_to: localhost
become: false
register: common_local_template_dir_root
# --
@@ -23,9 +29,9 @@
# ---
- name: (user-systemfiles.yml) Check if users file '.profile.ORIG' exists
stat:
ansible.builtin.stat:
path: "~{{ item.name }}/.profile.ORIG"
register: profile_user_orig_exists
register: common_profile_user_orig_exists
loop: "{{ system_users }}"
loop_control:
label: '{{ item.name }}'
@@ -33,12 +39,19 @@
- profile
- name: (user-systemfiles.yml) Backup existing users .profile file
command: cp -a ~{{ item.item.name }}/.profile ~{{ item.item.name }}/.profile.ORIG
loop: "{{ profile_user_orig_exists.results }}"
ansible.builtin.copy:
src: "~{{ item.item.name }}/.profile"
dest: "~{{ item.item.name }}/.profile.ORIG"
remote_src: true
owner: "{{ item.item.name }}"
group: "{{ item.item.name }}"
mode: "0644"
force: false
loop: "{{ common_profile_user_orig_exists.results }}"
loop_control:
label: '{{ item.item.name }}'
when:
- item.stat.exists == False
- not item.stat.exists
tags:
- profile
@@ -49,17 +62,17 @@
delegate_to: localhost
become: false
loop: "{{ default_user }}"
register: profile_stats
register: common_profile_stats
loop_control:
label: "{{ item.name }}"
# 2) Prüfe ob eine lokale default _baschrc existiert
- name: stat DEFAULT _profile
- name: Stat DEFAULT _profile
ansible.builtin.stat:
path: "{{ inventory_dir }}/files/{{ nis_domain }}/homedirs/DEFAULT/_profile"
delegate_to: localhost
become: false
register: default_profile_stat
register: common_default_profile_stat
# 2) Falls User _profile vorhanden, kopieren
- name: (system-user-systemfiles.yml) copy .profile if it exists
@@ -70,7 +83,7 @@
group: "{{ user.name }}"
mode: "0644"
become: true
loop: "{{ default_user | zip(profile_stats.results) | list }}"
loop: "{{ default_user | zip(common_profile_stats.results) | list }}"
loop_control:
label: "{{ user.name }}"
when:
@@ -89,12 +102,12 @@
group: "{{ user.name }}"
mode: "0644"
become: true
loop: "{{ default_user | zip(profile_stats.results) | list }}"
loop: "{{ default_user | zip(common_profile_stats.results) | list }}"
loop_control:
label: "{{ user.name }}"
when:
- not stat_result.stat.exists
- default_profile_stat.stat.exists | bool
- common_default_profile_stat.stat.exists | bool
vars:
user: "{{ item.0 }}"
stat_result: "{{ item.1 }}"
@@ -105,15 +118,22 @@
# ---
- name: (system-user-systemfiles.yml) Check if file '/root/.profile.ORIG' exists
stat:
ansible.builtin.stat:
path: /root/.profile.ORIG
register: profile_root_orig_exists
register: common_profile_root_orig_exists
tags:
- bash
- name: (system-user-systemfiles.yml) Backup /root/.profile file
command: cp /root/.profile /root/.profile.ORIG
when: profile_root_orig_exists.stat.exists == False
ansible.builtin.copy:
src: /root/.profile
dest: /root/.profile.ORIG
remote_src: true
owner: root
group: root
mode: "0644"
force: false
when: not common_profile_root_orig_exists.stat.exists
tags:
- bash
@@ -123,10 +143,10 @@
path: "{{ inventory_dir }}/files/{{ nis_domain }}/homedirs/root/_profile"
delegate_to: localhost
become: false
register: profile_root_stat
register: common_profile_root_stat
# 2) Wenn vorhanden, kopieren wir sie nach /root/.profile auf dem Zielhost
- name: copy root .profile if it exists
- name: Copy root .profile if it exists
ansible.builtin.copy:
src: "{{ inventory_dir }}/files/{{ nis_domain }}/homedirs/root/_profile"
dest: /root/.profile
@@ -134,7 +154,7 @@
group: root
mode: '0644'
become: true
when: profile_root_stat.stat.exists
when: common_profile_root_stat.stat.exists
tags:
- bash
@@ -144,9 +164,9 @@
# ---
- name: (system-user-systemfiles.yml) Check if users file '.bashrc.ORIG' exists
stat:
ansible.builtin.stat:
path: "~{{ item.name }}/.bashrc.ORIG"
register: bashrc_user_orig_exists
register: common_bashrc_user_orig_exists
loop: "{{ system_users }}"
loop_control:
label: '{{ item.name }}'
@@ -154,11 +174,18 @@
- bashrc
- name: (system-user-systemfiles.yml) Backup existing users .bashrc file
command: cp -a ~{{ item.item.name }}/.bashrc ~{{ item.item.name }}/.bashrc.ORIG
loop: "{{ bashrc_user_orig_exists.results }}"
ansible.builtin.copy:
src: "~{{ item.item.name }}/.bashrc"
dest: "~{{ item.item.name }}/.bashrc.ORIG"
remote_src: true
owner: "{{ item.item.name }}"
group: "{{ item.item.name }}"
mode: "0644"
force: false
loop: "{{ common_bashrc_user_orig_exists.results }}"
loop_control:
label: '{{ item.item.name }}'
when: item.stat.exists == False
when: not item.stat.exists
tags:
- bashrc
@@ -169,17 +196,17 @@
delegate_to: localhost
become: false
loop: "{{ default_user }}"
register: bashrc_stats
register: common_bashrc_stats
loop_control:
label: "{{ item.name }}"
# 2) Prüfe ob eine lokale default _baschrc existiert
- name: stat DEFAULT _bashrc
- name: Stat DEFAULT _bashrc
ansible.builtin.stat:
path: "{{ inventory_dir }}/files/{{ nis_domain }}/homedirs/DEFAULT/_bashrc"
delegate_to: localhost
become: false
register: default_bashrc_stat
register: common_default_bashrc_stat
# 2) Falls User _bashrc vorhanden, kopieren
- name: (system-user-systemfiles.yml) copy .bashrc if it exists
@@ -190,7 +217,7 @@
group: "{{ user.name }}"
mode: "0644"
become: true
loop: "{{ default_user | zip(bashrc_stats.results) | list }}"
loop: "{{ default_user | zip(common_bashrc_stats.results) | list }}"
loop_control:
label: "{{ user.name }}"
when:
@@ -209,12 +236,12 @@
group: "{{ user.name }}"
mode: "0644"
become: true
loop: "{{ default_user | zip(bashrc_stats.results) | list }}"
loop: "{{ default_user | zip(common_bashrc_stats.results) | list }}"
loop_control:
label: "{{ user.name }}"
when:
- not stat_result.stat.exists
- default_bashrc_stat.stat.exists | bool
- common_default_bashrc_stat.stat.exists | bool
vars:
user: "{{ item.0 }}"
stat_result: "{{ item.1 }}"
@@ -225,28 +252,35 @@
# ---
- name: (system-user-systemfiles.yml) Check if file '/root/.bashrc.ORIG' exists
stat:
ansible.builtin.stat:
path: /root/.bashrc.ORIG
register: bashrc_root_orig_exists
register: common_bashrc_root_orig_exists
tags:
- bash
- name: (system-user-systemfiles.yml) Backup /root/.bashrc file
command: cp /root/.bashrc /root/.bashrc.ORIG
when: bashrc_root_orig_exists.stat.exists == False
ansible.builtin.copy:
src: /root/.bashrc
dest: /root/.bashrc.ORIG
remote_src: true
owner: root
group: root
mode: "0644"
force: false
when: not common_bashrc_root_orig_exists.stat.exists
tags:
- bash
# 1) Prüfen ob die _bashrc für root auf dem Control-Node existiert
- name: stat root _bashrc on control node
- name: Stat root _bashrc on control node
ansible.builtin.stat:
path: "{{ inventory_dir }}/files/{{ nis_domain }}/homedirs/root/_bashrc"
delegate_to: localhost
become: false
register: bashrc_root_stat
register: common_bashrc_root_stat
# 2) Wenn vorhanden, kopieren wir sie nach /root/.bashrc auf dem Zielhost
- name: copy root .bashrc if it exists
- name: Copy root .bashrc if it exists
ansible.builtin.copy:
src: "{{ inventory_dir }}/files/{{ nis_domain }}/homedirs/root/_bashrc"
dest: /root/.bashrc
@@ -254,7 +288,7 @@
group: root
mode: '0644'
become: true
when: bashrc_root_stat.stat.exists
when: common_bashrc_root_stat.stat.exists
tags:
- bash
@@ -269,19 +303,19 @@
delegate_to: localhost
become: false
loop: "{{ default_user }}"
register: vimrc_stats
register: common_vimrc_stats
loop_control:
label: '{{ item.name }}'
# 2. Falls vorhanden, Datei kopieren
- name: (system-user-systemfiles.yml) copy .vimrc if it exists
- name: (system-user-systemfiles.yml) Copy .vimrc if it exists
ansible.builtin.copy:
src: "{{ inventory_dir }}/files/{{ nis_domain }}/homedirs/{{ user.name }}/_vimrc"
dest: "~{{ user.name }}/.vimrc"
owner: "{{ user.name }}"
group: "{{ user.name }}"
mode: '0644'
loop: "{{ default_user | zip(vimrc_stats.results) | list }}"
loop: "{{ default_user | zip(common_vimrc_stats.results) | list }}"
loop_control:
label: "{{ user.name }}"
when:
@@ -300,7 +334,7 @@
delegate_to: localhost
become: false
loop: "{{ default_user }}"
register: dotvim_stats
register: common_dotvim_stats
loop_control:
label: "{{ item.name }}"
@@ -312,7 +346,7 @@
dest: "~{{ user.name }}/"
mode: preserve # oder weglassen; 0644 wäre für Verzeichnisse falsch
become: true
loop: "{{ default_user | zip(dotvim_stats.results) | list }}"
loop: "{{ default_user | zip(common_dotvim_stats.results) | list }}"
loop_control:
label: "{{ user.name }}"
when:
@@ -332,7 +366,7 @@
recurse: true
state: directory
become: true
loop: "{{ default_user | zip(dotvim_stats.results) | list }}"
loop: "{{ default_user | zip(common_dotvim_stats.results) | list }}"
loop_control:
label: "{{ user.name }}"
when:
@@ -353,10 +387,10 @@
path: "{{ inventory_dir }}/files/{{ nis_domain }}/homedirs/root/_vimrc"
delegate_to: localhost
become: false
register: vimrc_root_stat
register: common_vimrc_root_stat
# 2) Wenn vorhanden, kopieren wir sie nach /root/.vimrc auf dem Zielhost
- name: (system-user-systemfiles.yml)copy root .vimrc if it exists
- name: (system-user-systemfiles.yml) Copy root .vimrc if it exists
ansible.builtin.copy:
src: "{{ inventory_dir }}/files/{{ nis_domain }}/homedirs/root/_vimrc"
dest: /root/.vimrc
@@ -365,17 +399,17 @@
mode: '0644'
become: true
when:
- vimrc_root_stat.stat.exists
- common_vimrc_root_stat.stat.exists
tags:
- bash
# 1) Lokal prüfen, ob ./files/{{ nis_domain }}/homedirs/root/.vim existiert
# 1) Lokal prüfen, ob ./files/{{ nis_domain }}/homedirs/root/.vim existiert
- name: (system-user-systemfiles.yml) stat local .vim for root
ansible.builtin.stat:
path: "{{ inventory_dir }}/files/{{ nis_domain }}/homedirs/root/.vim"
delegate_to: localhost
become: false
register: root_dotvim_stat
register: common_root_dotvim_stat
tags: [vim]
@@ -387,11 +421,11 @@
mode: preserve # oder weglassen; nicht 0644 bei Verzeichnissen
become: true
when:
- root_dotvim_stat.stat.exists | bool
- common_root_dotvim_stat.stat.exists | bool
tags: [vim]
# 2) Wenn vorhanden, nach /root/ kopieren
#- name: (system-user-systemfiles.yml) rsync root .vim if it exists
# - name: (system-user-systemfiles.yml) rsync root .vim if it exists
# ansible.posix.synchronize:
# src: "{{ inventory_dir }}/files/{{ nis_domain }}/homedirs/root/.vim/"
# dest: "/root/.vim/"
@@ -400,7 +434,7 @@
# rsync_path: "sudo -n rsync" # -n = kein Passwort-Prompt; erfordert NOPASSWD
# delegate_to: localhost
# when:
# - root_dotvim_stat.stat.exists | bool
# - common_root_dotvim_stat.stat.exists | bool
# tags: [vim]
# 3) Ownership sicherstellen (rekursiv)
@@ -413,5 +447,5 @@
state: directory
become: true
when:
- root_dotvim_stat.stat.exists | bool
- common_root_dotvim_stat.stat.exists | bool
tags: [vim]