| 1 |
## create user without home and test fallback home dir create |
| 2 |
|
| 3 |
- name: Test home directory creation |
| 4 |
when: ansible_facts.system != 'Darwin' |
| 5 |
block: |
| 6 |
- name: create the user |
| 7 |
user: |
| 8 |
name: ansibulluser |
| 9 |
|
| 10 |
- name: delete the user and home dir |
| 11 |
user: |
| 12 |
name: ansibulluser |
| 13 |
state: absent |
| 14 |
force: true |
| 15 |
remove: true |
| 16 |
|
| 17 |
- name: create the user without home |
| 18 |
user: |
| 19 |
name: ansibulluser |
| 20 |
create_home: no |
| 21 |
|
| 22 |
- name: create the user home dir |
| 23 |
user: |
| 24 |
name: ansibulluser |
| 25 |
register: user_create_home_fallback |
| 26 |
|
| 27 |
- name: stat home dir |
| 28 |
stat: |
| 29 |
path: '{{ user_create_home_fallback.home }}' |
| 30 |
register: user_create_home_fallback_dir |
| 31 |
|
| 32 |
- name: read UMASK from /etc/login.defs and return mode |
| 33 |
shell: | |
| 34 |
import re |
| 35 |
import os |
| 36 |
umask = os.umask(0) |
| 37 |
mode = oct(0o777 & ~umask) |
| 38 |
try: |
| 39 |
for line in open('/etc/login.defs').readlines(): |
| 40 |
m = re.match(r'^HOME_MODE\s+(\d+)$', line) |
| 41 |
if m: |
| 42 |
mode = oct(int(m.group(1), 8)) |
| 43 |
break |
| 44 |
m = re.match(r'^UMASK\s+(\d+)$', line) |
| 45 |
if m: |
| 46 |
umask = int(m.group(1), 8) |
| 47 |
mode = oct(0o777 & ~umask) |
| 48 |
except: |
| 49 |
pass |
| 50 |
print(str(mode).replace('o', '')) |
| 51 |
args: |
| 52 |
executable: "{{ ansible_python_interpreter }}" |
| 53 |
register: user_login_defs_umask |
| 54 |
|
| 55 |
- name: validate that user home dir is created |
| 56 |
assert: |
| 57 |
that: |
| 58 |
- user_create_home_fallback is changed |
| 59 |
- user_create_home_fallback_dir.stat.exists |
| 60 |
- user_create_home_fallback_dir.stat.isdir |
| 61 |
- user_create_home_fallback_dir.stat.pw_name == 'ansibulluser' |
| 62 |
- user_create_home_fallback_dir.stat.mode == user_login_defs_umask.stdout |
| 63 |
|
| 64 |
always: |
| 65 |
- name: Clean up ansibulluser |
| 66 |
user: |
| 67 |
name: ansibulluser |
| 68 |
state: absent |
| 69 |
force: true |
| 70 |
remove: true |
| 71 |
|
| 72 |
- name: Create non-system user |
| 73 |
when: ansible_facts.distribution == "MacOSX" |
| 74 |
block: |
| 75 |
- name: create non-system user on macOS to test the shell is set to /bin/bash |
| 76 |
user: |
| 77 |
name: macosuser |
| 78 |
register: macosuser_output |
| 79 |
|
| 80 |
- name: validate the shell is set to /bin/bash |
| 81 |
assert: |
| 82 |
that: |
| 83 |
- 'macosuser_output.shell == "/bin/bash"' |
| 84 |
|
| 85 |
- name: cleanup |
| 86 |
user: |
| 87 |
name: macosuser |
| 88 |
state: absent |
| 89 |
|
| 90 |
- name: create system user on macOS to test the shell is set to /usr/bin/false |
| 91 |
user: |
| 92 |
name: macosuser |
| 93 |
system: yes |
| 94 |
register: macosuser_output |
| 95 |
|
| 96 |
- name: validate the shell is set to /usr/bin/false |
| 97 |
assert: |
| 98 |
that: |
| 99 |
- 'macosuser_output.shell == "/usr/bin/false"' |
| 100 |
|
| 101 |
- name: cleanup |
| 102 |
user: |
| 103 |
name: macosuser |
| 104 |
state: absent |
| 105 |
|
| 106 |
- name: create non-system user on macos and set the shell to /bin/sh |
| 107 |
user: |
| 108 |
name: macosuser |
| 109 |
shell: /bin/sh |
| 110 |
register: macosuser_output |
| 111 |
|
| 112 |
- name: validate the shell is set to /bin/sh |
| 113 |
assert: |
| 114 |
that: |
| 115 |
- 'macosuser_output.shell == "/bin/sh"' |
| 116 |
|
| 117 |
- name: cleanup |
| 118 |
user: |
| 119 |
name: macosuser |
| 120 |
state: absent |