nfs-server: Add autocreate support for home/role directories

This commit is contained in:
Timo Makinen 2021-03-23 17:01:39 +00:00
parent cc3f8748a0
commit 5282a19463
3 changed files with 131 additions and 0 deletions

View file

@ -0,0 +1,56 @@
#!/bin/bash
set -e
_basedn="$(awk '{ if ($1 == "BASE") print $2 }' /etc/openldap/ldap.conf)"
if [ -z "$_basedn" ]; then
logger -i -t "$(basename "$0")" -p user.error -s \
"ERROR: Failed to get LDAP basedn" 1>&2
exit 1
fi
if [ $# -eq 1 ]; then
_filter="(&(automountKey=$1)(objectClass=automount))"
elif [ $# -eq 0 ]; then
_filter="(objectClass=automount)"
else
echo "Usage: $(basename "$0") [username]" 1>&2
exit 1
fi
ldapsearch -Q -LLL -b "ou=People,${_basedn}" "$_filter" automountInformation | \
awk -v_hostname="$(hostname -f)" '{
if ($1 == "automountInformation:") {
split($2, _, ":");
if (_[1] == _hostname) {
print _[2];
}
}
}' | while read -r _target ; do
_user="$(basename "$_target")"
_basedir="$(dirname "$_target")"
[ -d "$_target" ] && continue
if ! getent passwd "$_user" > /dev/null 2>&1 ; then
logger -i -t "$(basename "$0")" -p user.error -s \
"ERROR: Cannot find user '${_user}'" 1>&2
continue
fi
if ! getent group "$_user" > /dev/null 2>&1 ; then
logger -i -t "$(basename "$0")" -p user.error -s \
"ERROR: Cannot find group '${_user}'" 1>&2
continue
fi
if [ ! -d "$_basedir" ]; then
logger -i -t "$(basename "$0")" -p user.error -s \
"ERROR: Cannot find base direcory '${_basedir}'"
continue
fi
logger -i -t "$(basename "$0")" -p user.info \
"Creating home directory '${_target}' for user '${_user}'"
install -d -o "$_user" -g "$_user" -m 0700 "$_target"
su "$_user" -s /bin/bash -c "umask 077 ; cp -r /etc/skel/. '${_target}'"
done

View file

@ -0,0 +1,54 @@
#!/bin/bash
set -e
_basedn="$(awk '{ if ($1 == "BASE") print $2 }' /etc/openldap/ldap.conf)"
if [ -z "$_basedn" ]; then
logger -i -t "$(basename "$0")" -p user.error -s \
"ERROR: Failed to get LDAP basedn" 1>&2
exit 1
fi
if [ $# -eq 1 ]; then
_filter="(&(automountKey=$1)(objectClass=automount))"
elif [ $# -eq 0 ]; then
_filter="(objectClass=automount)"
else
echo "Usage: $(basename "$0") [role]" 1>&2
exit 1
fi
ldapsearch -Q -LLL -b "ou=Groups,${_basedn}" "$_filter" automountInformation | \
awk -v_hostname="$(hostname -f)" '{
if ($1 == "automountInformation:") {
split($2, _, ":");
if (_[1] == _hostname) {
print _[2];
}
}
}' | while read -r _target ; do
_role="$(basename "$_target")"
_basedir="$(dirname "$_target")"
[ -d "$_target" ] && continue
if ! getent group "$_role" > /dev/null 2>&1 ; then
logger -i -t "$(basename "$0")" -p user.error -s \
"ERROR: Cannot find group '${_role}'" 1>&2
continue
fi
if [ ! -d "$_basedir" ]; then
logger -i -t "$(basename "$0")" -p user.error -s \
"ERROR: Cannot find base direcory '${_basedir}'"
continue
fi
logger -i -t "$(basename "$0")" -p user.info \
"Creating role directory '${_target}' for role '${_role}'"
install -d -o root -g "$_role" -m 2751 "$_target"
install -d -o root -g "$_role" -m 2770 "${_target}/development"
install -d -o root -g "$_role" -m 2770 "${_target}/external"
install -d -o root -g "$_role" -m 2770 "${_target}/library"
install -d -o root -g "$_role" -m 2775 "${_target}/public"
done

View file

@ -17,6 +17,27 @@
insertbefore: vers2=n
notify: restart nfs-server
- name: install home/role autocreate scripts
copy:
dest: "/usr/local/sbin/{{ item }}"
src: "{{ item }}.sh"
mode: 0755
owner: root
group: "{{ ansible_wheel }}"
with_items:
- mknfshomedir
- mknfsroledir
- name: add home/role autocreate cron jobs
cron:
name: "{{ item }}"
user: root
minute: "*/15"
job: "/usr/local/sbin/{{ item }}"
with_items:
- mknfshomedir
- mknfsroledir
- name: enable nfs server services
service:
name: nfs-server