summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/user/tasks/test_create_user_home.yml
blob: 1b529f76be3853684287bc76aa8ec08e00c6c39f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# https://github.com/ansible/ansible/issues/42484
# Skipping macOS for now since there is a bug when changing home directory
- name: Test home directory creation
  when: ansible_facts.system != 'Darwin'
  block:
    - name: create user specifying home
      user:
        name: ansibulluser
        state: present
        home: "{{ user_home_prefix[ansible_facts.system] }}/ansibulluser"
      register: user_test3_0

    - name: create user again specifying home
      user:
        name: ansibulluser
        state: present
        home: "{{ user_home_prefix[ansible_facts.system] }}/ansibulluser"
      register: user_test3_1

    - name: change user home
      user:
        name: ansibulluser
        state: present
        home: "{{ user_home_prefix[ansible_facts.system] }}/ansibulluser-mod"
      register: user_test3_2

    - name: change user home back
      user:
        name: ansibulluser
        state: present
        home: "{{ user_home_prefix[ansible_facts.system] }}/ansibulluser"
      register: user_test3_3

    - name: validate results for testcase 3
      assert:
        that:
          - user_test3_0 is not changed
          - user_test3_1 is not changed
          - user_test3_2 is changed
          - user_test3_3 is changed

# https://github.com/ansible/ansible/issues/41393
# 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
  user:
    name: ansibulluser2
    state: present
    home: "{{ user_home_prefix[ansible_facts.system] }}/in2deep/ansibulluser2"
  register: create_home_with_no_parent_1

- name: Create user with home path that has parents that do not exist again
  user:
    name: ansibulluser2
    state: present
    home: "{{ user_home_prefix[ansible_facts.system] }}/in2deep/ansibulluser2"
  register: create_home_with_no_parent_2

- name: Check the created home directory
  stat:
    path: "{{ user_home_prefix[ansible_facts.system] }}/in2deep/ansibulluser2"
  register: home_with_no_parent_3

- name: Ensure user with non-existing parent paths was created successfully
  assert:
    that:
      - create_home_with_no_parent_1 is changed
      - create_home_with_no_parent_1.home == user_home_prefix[ansible_facts.system] ~ '/in2deep/ansibulluser2'
      - create_home_with_no_parent_2 is not changed
      - home_with_no_parent_3.stat.uid == create_home_with_no_parent_1.uid
      - home_with_no_parent_3.stat.gr_name == default_user_group[ansible_facts.distribution] | default('ansibulluser2')

- name: Cleanup test account
  user:
    name: ansibulluser2
    home: "{{ user_home_prefix[ansible_facts.system] }}/in2deep/ansibulluser2"
    state: absent
    remove: yes

- name: Remove testing dir
  file:
    path: "{{ user_home_prefix[ansible_facts.system] }}/in2deep/"
    state: absent


# https://github.com/ansible/ansible/issues/60307
# Make sure we can create a user when the home directory is missing
- name: Create user with home path that does not exist
  user:
    name: ansibulluser3
    state: present
    home: "{{ user_home_prefix[ansible_facts.system] }}/nosuchdir"
    createhome: no

- name: Cleanup test account
  user:
    name: ansibulluser3
    state: absent
    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