fix(samba-user.yml): improve command structure and enhance error handling for samba user management

This commit is contained in:
2026-07-29 23:30:04 +02:00
parent 71e9cf6551
commit e125f9952f
+24 -13
View File
@@ -35,13 +35,16 @@
# Samba users mut be also system users # Samba users mut be also system users
# #
- name: (samba_user.yml) Add (system) users if not yet exists.. - name: (samba_user.yml) Add (system) users if not yet exists..
ansible.builtin.command: "/root/bin/admin-stuff/add_new_user.sh {{ item.name }} '{{ item.password }}'" ansible.builtin.command:
cmd: "/root/bin/admin-stuff/add_new_user.sh {{ item.name }} '{{ item.password }}'"
creates: "/home/{{ item.name }}"
loop: "{{ samba_user }}" loop: "{{ samba_user }}"
loop_control: loop_control:
label: "{{ item.name }}" label: "{{ item.name }}"
when: when:
- ansible_facts.getent_passwd is defined - ansible_facts.getent_passwd is defined
- item.name not in ansible_facts.getent_passwd - item.name not in ansible_facts.getent_passwd
changed_when: true
tags: tags:
- samba-server - samba-server
- samba-user - samba-user
@@ -52,7 +55,7 @@
name: "{{ item.name }}" name: "{{ item.name }}"
state: present state: present
uid: "{{ item.user_id | default(omit) }}" uid: "{{ item.user_id | default(omit) }}"
#group: '{{ item.0.name | default(omit) }}' # group: '{{ item.0.name | default(omit) }}'
groups: "{{ item.groups | join(', ') }}" groups: "{{ item.groups | join(', ') }}"
password: "{{ item.password | password_hash('sha512') }}" password: "{{ item.password | password_hash('sha512') }}"
update_password: on_create update_password: on_create
@@ -66,10 +69,14 @@
- system-user - system-user
- name: (samba-user.yml) Check if samba user exists - name: (samba-user.yml) Check if samba user exists
ansible.builtin.shell: pdbedit -w -L | awk -F":" '{ print $1 }' | grep -e "^{{ item.name }}" ansible.builtin.shell:
register: samba_user_present executable: /bin/bash
changed_when: "samba_user_present.rc == 1" cmd: |
failed_when: "samba_user_present.rc > 1" set -o pipefail
pdbedit -w -L | awk -F":" '{ print $1 }' | grep -e "^{{ item.name }}"
register: common_samba_user_present
changed_when: "common_samba_user_present.rc == 1"
failed_when: "common_samba_user_present.rc > 1"
loop: "{{ samba_user }}" loop: "{{ samba_user }}"
loop_control: loop_control:
label: "{{ item.name }}" label: "{{ item.name }}"
@@ -77,24 +84,28 @@
- samba-server - samba-server
- samba-user - samba-user
- name: (samba-user.yml) Add user to samba (with system users password) - name: (samba-user.yml) Add user to samba (with system users password) # noqa no-handler
ansible.builtin.shell: > ansible.builtin.shell:
(echo '{{ item.item.password }}'; echo '{{ item.item.password }}') | smbpasswd -s -a {{ item.item.name }} executable: /bin/bash
loop: "{{ samba_user_present.results }}" cmd: |
set -o pipefail
(echo '{{ item.item.password }}'; echo '{{ item.item.password }}') | smbpasswd -s -a {{ item.item.name }}
loop: "{{ common_samba_user_present.results }}"
when: item.changed when: item.changed
loop_control: loop_control:
label: "{{ item.item.name }}" label: "{{ item.item.name }}"
changed_when: true
tags: tags:
- samba-server - samba-server
- samba-user - samba-user
# Only on fileservers: # Only on fileservers:
# zapata.opp.netz # zapata.opp.netz
- name: (samba_user.yml) Check if folder '/data/backup' exists using file module - name: (samba_user.yml) Check if folder '/data/backup' exists using file module
ansible.builtin.stat: ansible.builtin.stat:
path: /data/backup path: /data/backup
register: data_backup_dir register: common_data_backup_dir
when: when:
- inventory_hostname == 'zapata.opp.netz' - inventory_hostname == 'zapata.opp.netz'
tags: tags:
@@ -114,7 +125,7 @@
label: "{{ item.name }}" label: "{{ item.name }}"
when: when:
- inventory_hostname == 'zapata.opp.netz' - inventory_hostname == 'zapata.opp.netz'
- data_backup_dir.stat.isdir is defined and data_backup_dir.stat.isdir - common_data_backup_dir.stat.isdir is defined and common_data_backup_dir.stat.isdir
tags: tags:
- samba-server - samba-server
- samba-user - samba-user