- In 'roles/tmux/tasks/main.yml', strengthened the filter for creating 'user_home_dirs' to explicitly exclude empty home directory paths. - Reverted 'roles/tmux/tasks/tmux_config.yml' to remove redundant conditionals. - In 'roles/tmux/tasks/main.yml', wrapped the 'include_tasks' for 'tmux_config.yml' in a block and added a robust 'when' condition to prevent execution for 'root' or for invalid home directories. This directly addresses the user's request and provides multiple layers of protection against the 'chown failed' error.
48 lines
1.5 KiB
YAML
48 lines
1.5 KiB
YAML
---
|
|
- name: Install tmux
|
|
package:
|
|
name: "{{ item }}"
|
|
state: present
|
|
loop:
|
|
- "tmux"
|
|
- "git"
|
|
|
|
- name: Get all users from /etc/passwd
|
|
ansible.builtin.getent:
|
|
database: passwd
|
|
register: all_users
|
|
|
|
- name: Create a list of home directories for bash and zsh users
|
|
set_fact:
|
|
user_home_dirs: "{{ user_home_dirs | default([]) + [item.value[4]] }}"
|
|
loop: "{{ all_users.ansible_facts.getent_passwd | dict2items }}"
|
|
when:
|
|
- item.value[5] in ['/bin/bash', '/bin/zsh', '/usr/bin/bash', '/usr/bin/zsh', '/usr/bin/fish']
|
|
- item.value[0] != 'root'
|
|
- item.value[4] is defined
|
|
- item.value[4] != '/'
|
|
- item.value[4] != ''
|
|
|
|
- name: Display the list of collected home directories
|
|
ansible.builtin.debug:
|
|
var: user_home_dirs
|
|
|
|
- name: Tmux config block
|
|
block:
|
|
- name: Tmux config Play
|
|
include_tasks: tmux_config.yml
|
|
loop: "{{ user_home_dirs }}"
|
|
when: item | basename != 'root' and item != '/' and item != ''
|
|
|
|
# ========================================================================
|
|
# EXAMPLE: Here is how you can reuse the 'user_home_dirs' list
|
|
# This example task iterates through the list of home directories
|
|
# and creates a file named 'test.txt' in each directory.
|
|
# ========================================================================
|
|
# - name: Example - Create a file in each home directory
|
|
# ansible.builtin.file:
|
|
# path: "{{ item }}/test.txt"
|
|
# state: touch
|
|
# mode: '0644'
|
|
# loop: "{{ user_home_dirs }}"
|