Skip to content

Commit 78c6875

Browse files
committed
add option to specify the users home directory
1 parent e6aef6f commit 78c6875

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ The following attributes are required for each user:
2626

2727
* username - The user's username.
2828
* name - The full name of the user (gecos field)
29+
* home - the home directory of the user to create (optional, defaults to /home/username)
2930
* uid - The numeric user id for the user. This is required for uid consistency
3031
across systems.
3132
* gid - The numeric group id for the group (optional). Otherwise, the
@@ -51,6 +52,7 @@ Example:
5152
name: Foo Barrington
5253
groups: ['wheel','systemd-journal']
5354
uid: 1001
55+
home: /local/home/foo
5456
profile: |
5557
alias ll='ls -lah'
5658
ssh_key:

tasks/main.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
password: "{{item.password if item.password is defined else '!'}}"
2222
comment: "{{item.name if item.name is defined else ''}}"
2323
uid: "{{item.uid}}"
24+
home: "{{ item.home | default('/home/' + item.username) }}"
2425
createhome: "{{'yes' if users_create_homedirs else 'no'}}"
2526
with_items: "{{users}}"
2627
tags: ['users','configuration']
@@ -29,7 +30,7 @@
2930
authorized_key:
3031
user: "{{item.0.username}}"
3132
key: "{{item.1}}"
32-
path: "/home/{{item.0.username}}/.ssh/authorized_keys"
33+
path: "{{ item.0.home | default('/home/' + item.0.username) }}/.ssh/authorized_keys"
3334
with_subelements:
3435
- "{{users}}"
3536
- ssh_key
@@ -38,7 +39,7 @@
3839
- name: Setup user profiles
3940
blockinfile:
4041
block: "{{item.profile}}"
41-
dest: "/home/{{item.username}}/.profile"
42+
dest: "{{ item.home | default('/home/' + item.username) }}/.profile"
4243
owner: "{{item.username}}"
4344
group: "{{item.username}}"
4445
mode: 0644

tests/test.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,22 @@
1717

1818
roles:
1919
- ansible-users
20+
21+
- hosts: localhost
22+
remote_user: root
23+
vars:
24+
users:
25+
- name: Ansible Test User2
26+
username: ansibletestuser2
27+
uid: 2222
28+
groups: [users, bin]
29+
shell: /bin/sh
30+
home: /home/otherdirectory
31+
profile: |
32+
alias ll='ls -lah'
33+
alias cp='cp -iv'
34+
ssh_key:
35+
- "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDVpUJQCOaPg3p5xro9e+1fkGRWNOGrrExiKMqTE91Fwu349bxfMnMzRS0PAERouR9EEL+Ee4Yzhav/uNc35eCtXzACtluXnAncMrQj6pM3IqASynhvXTygHljmcMbBSDQtLrTZeW+YzIcOgk5UM1yBi26WoUYva2aCr9IRvKdYreAK08OiMdZedpOye0ZdvIYJGcyITwc6YMmrAhP7jZlrk/mDEkf2a4eBp+475o7MJtaC9npqYkToM8vqvx5AGEKqXt7/f1/paOY7KsR+VGPQy6k2RkXjWBsXPesZ3d3XLZHE60wAk0EsuJO8A25+uWSB6ILQeRSYYmGea/WIf6kd [email protected]"
36+
37+
roles:
38+
- ansible-users

0 commit comments

Comments
 (0)