Create home and parent directories only when requested (#70790)

The home user and the parents directories should only be created when
create_home == True

(cherry picked from commit f3dd8d3052)
This commit is contained in:
Baptiste Mille-Mathias 2020-07-23 03:23:47 +02:00 committed by GitHub
parent 4170786cd9
commit 1eb2afac63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 7 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- "user - don't create home directory and missing parents when create_home == false (https://github.com/ansible/ansible/pull/70600)."

View file

@ -634,11 +634,12 @@ class User(object):
if self.home is not None: if self.home is not None:
# If the specified path to the user home contains parent directories that # If the specified path to the user home contains parent directories that
# do not exist, first create the home directory since useradd cannot # do not exist and create_home is True first create the parent directory
# create parent directories # since useradd cannot create it.
parent = os.path.dirname(self.home) if self.create_home:
if not os.path.isdir(parent): parent = os.path.dirname(self.home)
self.create_homedir(self.home) if not os.path.isdir(parent):
self.create_homedir(self.home)
cmd.append('-d') cmd.append('-d')
cmd.append(self.home) cmd.append(self.home)
@ -2940,7 +2941,7 @@ def main():
# Check to see if the provided home path contains parent directories # Check to see if the provided home path contains parent directories
# that do not exist. # that do not exist.
path_needs_parents = False path_needs_parents = False
if user.home: if user.home and user.create_home:
parent = os.path.dirname(user.home) parent = os.path.dirname(user.home)
if not os.path.isdir(parent): if not os.path.isdir(parent):
path_needs_parents = True path_needs_parents = True

View file

@ -226,7 +226,6 @@
- user_test3_3 is changed - user_test3_3 is changed
when: ansible_facts.system != 'Darwin' when: ansible_facts.system != 'Darwin'
# https://github.com/ansible/ansible/issues/41393 # https://github.com/ansible/ansible/issues/41393
# Create a new user account with a path that has parent directories that do not exist # Create a new user account with a path that has parent directories that do not exist
- name: Create user with home path that has parents that do not exist - name: Create user with home path that has parents that do not exist
@ -285,6 +284,43 @@
state: absent state: absent
remove: yes remove: yes
# https://github.com/ansible/ansible/issues/70589
# Create user with create_home: no and parent directory does not exist.
- name: "Check if parent dir for home dir for user exists (before)"
stat:
path: "{{ user_home_prefix[ansible_facts.system] }}/thereisnodir"
register: create_user_no_create_home_with_no_parent_parent_dir_before
- name: "Create user with create_home == no and home path parent dir does not exist"
user:
name: randomuser
state: present
create_home: false
home: "{{ user_home_prefix[ansible_facts.system] }}/thereisnodir/randomuser"
register: create_user_no_create_home_with_no_parent
- name: "Check if parent dir for home dir for user exists (after)"
stat:
path: "{{ user_home_prefix[ansible_facts.system] }}/thereisnodir"
register: create_user_no_create_home_with_no_parent_parent_dir_after
- name: "Check if home for user is created"
stat:
path: "{{ user_home_prefix[ansible_facts.system] }}/thereisnodir/randomuser"
register: create_user_no_create_home_with_no_parent_home_dir
- name: "Ensure user with non-existing parent paths with create_home: no was created successfully"
assert:
that:
- not create_user_no_create_home_with_no_parent_parent_dir_before.stat.exists
- not create_user_no_create_home_with_no_parent_parent_dir_after.stat.isdir is defined
- not create_user_no_create_home_with_no_parent_home_dir.stat.exists
- name: Cleanup test account
user:
name: randomuser
state: absent
remove: yes
## user check ## user check