Modified 'roles/tmux/tasks/main.yml' to prevent invalid home
directories from being added to the 'user_home_dirs' list.
Added checks to ensure that the home directory path is defined
and has a length greater than 1, which excludes both empty paths
and the root directory ('/'). This resolves the 'chown failed'
error that occurred when the playbook was run on a system with
users having invalid home directory entries.
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] | length > 1
|
|
|
|
- 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 }}"
|