- Modified 'roles/tmux/tasks/main.yml' to explicitly filter out
the root directory ('/') as a valid home directory.
- Modified 'roles/tmux/tasks/tmux_config.yml' by adding a conditional
check () to all tasks. This prevents the
tasks from running for users with invalid home directories, which
resolves the 'chown failed' error.
44 lines
1.3 KiB
YAML
44 lines
1.3 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] != '/'
|
|
|
|
- name: Display the list of collected home directories
|
|
ansible.builtin.debug:
|
|
var: user_home_dirs
|
|
|
|
- name: Tmux config Play
|
|
include_tasks: tmux_config.yml
|
|
loop: "{{ user_home_dirs }}"
|
|
|
|
# ========================================================================
|
|
# 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 }}"
|