user: fix Alpine move_home ordering (#87044)

This commit is contained in:
Minh Vu
2026-06-02 13:40:30 +02:00
committed by GitHub
parent 1d398ae8af
commit 2a628c04c9
3 changed files with 44 additions and 10 deletions
@@ -0,0 +1,2 @@
bugfixes:
- user - fix ``move_home`` on BusyBox/Alpine to move existing home contents before rewriting ``/etc/passwd`` (https://github.com/ansible/ansible/pull/87044).
+14 -10
View File
@@ -3331,6 +3331,20 @@ class BusyBox(User):
if rc is not None and rc != 0:
self.module.fail_json(name=self.name, msg=err, rc=rc)
# Move the home before rewriting /etc/passwd so usermod can still
# locate the current home directory contents.
if self.move_home and self.home is not None and user_info[5] != self.home:
usermod_bin = self.module.get_bin_path('usermod')
if usermod_bin is not None:
cmd = [usermod_bin, '-d', self.home, '-m', self.name]
rc, out, err = self.execute_command(cmd)
if rc is not None and rc != 0:
self.module.fail_json(name=self.name, msg=err, rc=rc)
user_info = self.user_info()
else:
self.module.warn("usermod command not found, skipping home directory move")
# Manage user settings
uid = user_info[2]
if self.uid is not None:
@@ -3368,16 +3382,6 @@ class BusyBox(User):
self.module.backup_local(self.PASSWORDFILE)
self.module.atomic_move(tmpfile, self.PASSWORDFILE)
# Manage home directory
if self.move_home:
usermod_bin = self.module.get_bin_path('usermod')
if usermod_bin is not None:
cmd = [usermod_bin, '-d', self.home, '-m', self.name]
rc, out, err = self.execute_command(cmd)
if rc is not None and rc != 0:
self.module.fail_json(name=self.name, msg=err, rc=rc)
else:
self.module.warn("usermod command not found, skipping home directory move")
return rc, out, err
@@ -14,6 +14,14 @@
home: /tmp/ansibulluser
state: present
- name: Create sentinel file in the old home directory
copy:
content: sentinel
dest: /tmp/ansibulluser/sentinel
owner: ansibulluser
group: ansibulluser
mode: '0644'
- name: Move user home directory
user:
name: ansibulluser
@@ -26,12 +34,24 @@
path: /tmp/ansibulluser-moved
register: user_home_directory
- name: Stat sentinel file in the new home directory
stat:
path: /tmp/ansibulluser-moved/sentinel
register: moved_sentinel_file
- name: Stat sentinel file in the old home directory
stat:
path: /tmp/ansibulluser/sentinel
register: old_sentinel_file
- name: Check if user home directory is moved
assert:
that:
- user_home_directory.stat.exists
- user_home_directory.stat.isdir
- user_home_directory.stat.pw_name == 'ansibulluser'
- moved_sentinel_file.stat.exists
- not old_sentinel_file.stat.exists
always:
- name: Remove user home directory
@@ -39,5 +59,13 @@
name: ansibulluser
state: absent
- name: Remove old and new home directories
file:
path: "{{ item }}"
state: absent
loop:
- /tmp/ansibulluser
- /tmp/ansibulluser-moved
- name: Remove package
command: apk del shadow