Debian Codegraph @

test/integration/targets/user/tasks/test_create_user_home.yml

raw
1 # https://github.com/ansible/ansible/issues/42484
2 # Skipping macOS for now since there is a bug when changing home directory
3 - name: Test home directory creation
4 when: ansible_facts.system != 'Darwin'
5 block:
6 - name: create user specifying home
7 user:
8 name: ansibulluser
9 state: present
10 home: "{{ user_home_prefix[ansible_facts.system] }}/ansibulluser"
11 register: user_test3_0
12
13 - name: create user again specifying home
14 user:
15 name: ansibulluser
16 state: present
17 home: "{{ user_home_prefix[ansible_facts.system] }}/ansibulluser"
18 register: user_test3_1
19
20 - name: change user home
21 user:
22 name: ansibulluser
23 state: present
24 home: "{{ user_home_prefix[ansible_facts.system] }}/ansibulluser-mod"
25 register: user_test3_2
26
27 - name: change user home back
28 user:
29 name: ansibulluser
30 state: present
31 home: "{{ user_home_prefix[ansible_facts.system] }}/ansibulluser"
32 register: user_test3_3
33
34 - name: validate results for testcase 3
35 assert:
36 that:
37 - user_test3_0 is not changed
38 - user_test3_1 is not changed
39 - user_test3_2 is changed
40 - user_test3_3 is changed
41
42 # https://github.com/ansible/ansible/issues/41393
43 # Create a new user account with a path that has parent directories that do not exist
44 - name: Create user with home path that has parents that do not exist
45 user:
46 name: ansibulluser2
47 state: present
48 home: "{{ user_home_prefix[ansible_facts.system] }}/in2deep/ansibulluser2"
49 register: create_home_with_no_parent_1
50
51 - name: Create user with home path that has parents that do not exist again
52 user:
53 name: ansibulluser2
54 state: present
55 home: "{{ user_home_prefix[ansible_facts.system] }}/in2deep/ansibulluser2"
56 register: create_home_with_no_parent_2
57
58 - name: Check the created home directory
59 stat:
60 path: "{{ user_home_prefix[ansible_facts.system] }}/in2deep/ansibulluser2"
61 register: home_with_no_parent_3
62
63 - name: Ensure user with non-existing parent paths was created successfully
64 assert:
65 that:
66 - create_home_with_no_parent_1 is changed
67 - create_home_with_no_parent_1.home == user_home_prefix[ansible_facts.system] ~ '/in2deep/ansibulluser2'
68 - create_home_with_no_parent_2 is not changed
69 - home_with_no_parent_3.stat.uid == create_home_with_no_parent_1.uid
70 - home_with_no_parent_3.stat.gr_name == default_user_group[ansible_facts.distribution] | default('ansibulluser2')
71
72 - name: Cleanup test account
73 user:
74 name: ansibulluser2
75 home: "{{ user_home_prefix[ansible_facts.system] }}/in2deep/ansibulluser2"
76 state: absent
77 remove: yes
78
79 - name: Remove testing dir
80 file:
81 path: "{{ user_home_prefix[ansible_facts.system] }}/in2deep/"
82 state: absent
83
84
85 # https://github.com/ansible/ansible/issues/60307
86 # Make sure we can create a user when the home directory is missing
87 - name: Create user with home path that does not exist
88 user:
89 name: ansibulluser3
90 state: present
91 home: "{{ user_home_prefix[ansible_facts.system] }}/nosuchdir"
92 createhome: no
93
94 - name: Cleanup test account
95 user:
96 name: ansibulluser3
97 state: absent
98 remove: yes
99
100 # https://github.com/ansible/ansible/issues/70589
101 # Create user with create_home: no and parent directory does not exist.
102 - name: "Check if parent dir for home dir for user exists (before)"
103 stat:
104 path: "{{ user_home_prefix[ansible_facts.system] }}/thereisnodir"
105 register: create_user_no_create_home_with_no_parent_parent_dir_before
106
107 - name: "Create user with create_home == no and home path parent dir does not exist"
108 user:
109 name: randomuser
110 state: present
111 create_home: false
112 home: "{{ user_home_prefix[ansible_facts.system] }}/thereisnodir/randomuser"
113 register: create_user_no_create_home_with_no_parent
114
115 - name: "Check if parent dir for home dir for user exists (after)"
116 stat:
117 path: "{{ user_home_prefix[ansible_facts.system] }}/thereisnodir"
118 register: create_user_no_create_home_with_no_parent_parent_dir_after
119
120 - name: "Check if home for user is created"
121 stat:
122 path: "{{ user_home_prefix[ansible_facts.system] }}/thereisnodir/randomuser"
123 register: create_user_no_create_home_with_no_parent_home_dir
124
125 - name: "Ensure user with non-existing parent paths with create_home: no was created successfully"
126 assert:
127 that:
128 - not create_user_no_create_home_with_no_parent_parent_dir_before.stat.exists
129 - not create_user_no_create_home_with_no_parent_parent_dir_after.stat.isdir is defined
130 - not create_user_no_create_home_with_no_parent_home_dir.stat.exists
131
132 - name: Cleanup test account
133 user:
134 name: randomuser
135 state: absent
136 remove: yes
137
138 - name: Create user home directory with /dev/null as skeleton, https://github.com/ansible/ansible/issues/75063
139 # create_homedir is mostly used by linux, rest of OSs take care of it themselves via -k option (which fails this task)
140 # OS X actually breaks since it does not implement getpwnam()
141 when: ansible_system == 'Linux'
142 block:
143 - name: "Create user home directory with /dev/null as skeleton"
144 user:
145 name: withskeleton
146 state: present
147 skeleton: "/dev/null"
148 createhome: yes
149 register: create_user_with_skeleton_dev_null
150 always:
151 - name: "Remove test user"
152 user:
153 name: withskeleton
154 state: absent
155 remove: yes
156
157 - name: Create user home directory with skel that contains symlinks
158 tags: symlink_home
159 when: ansible_system == 'Linux'
160 become: True
161 vars:
162 flag: '{{tempdir.path}}/root_flag.conf'
163 block:
164 - name: make tempdir for skel
165 tempfile: state=directory
166 register: tempdir
167
168 - name: create flag file
169 file: path={{flag}} owner=root state=touch
170
171 - name: copy skell to target
172 copy:
173 dest: '{{tempdir.path}}/skel'
174 src: files/skel
175 register: skel
176
177 - name: create the bad symlink
178 file:
179 src: '{{flag}}'
180 dest: '{{tempdir.path}}/skel/should_not_change_own'
181 state: link
182
183 - name: "Create user home directory with skeleton"
184 user:
185 name: withskeleton
186 state: present
187 skeleton: "{{tempdir.path}}/skel"
188 createhome: yes
189 home: /home/missing/withskeleton
190 register: create_user_with_skeleton_symlink
191
192 - name: Check flag
193 stat: path={{flag}}
194 register: test_flag
195
196 - name: ensure we didn't change owner for flag
197 assert:
198 that:
199 - test_flag.stat.uid != create_user_with_skeleton_symlink.uid
200
201 always:
202 - name: "Remove test user"
203 user:
204 name: withskeleton
205 state: absent
206 remove: yes
207
208 - name: get files to delete
209 find: path="{{tempdir.path}}"
210 register: remove
211 when:
212 - tempdir is defined
213 - tempdir is success
214
215 - name: "Remove temp files"
216 file:
217 path: '{{item}}'
218 state: absent
219 loop: "{{remove.files|default([])}}"
220 when:
221 - remove is success