106 lines
2.4 KiB
Bash
106 lines
2.4 KiB
Bash
#!/bin/sh
|
|
|
|
#
|
|
#### FUNCTIONS
|
|
#
|
|
|
|
# $group_name $group_number
|
|
group_exists () {
|
|
grep -q "^$1:[^:]*:$2:" /etc/group
|
|
}
|
|
|
|
# $user_name $user_number
|
|
user_exists () {
|
|
grep -q "^$1:[^:]*:$2:$2:" /etc/passwd
|
|
}
|
|
|
|
# $group_name $group_number
|
|
system_groupadd () {
|
|
if group_exists "$1" "$2" ; then return 0 ; fi
|
|
groupadd -r -g "$2" "$1"
|
|
rc=$?
|
|
if [ $rc -ne 0 ] ; then
|
|
echo "ERROR - Unable to add $1 group! ($rc)" 1>&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# $user_name $user_number
|
|
system_useradd () {
|
|
if user_exists "$1" "$2" ; then return 0 ; fi
|
|
system_groupadd "${@}"
|
|
useradd -r -u "$2" -g "$2" -s /bin/bash -m "$1"
|
|
rc=$?
|
|
if [ $rc -ne 0 ] ; then
|
|
echo "ERROR - Unable to add $1 user! ($rc)" 1>&2
|
|
exit 2
|
|
fi
|
|
}
|
|
|
|
# $groups $user_name
|
|
add_groups_to_user () {
|
|
usermod -aG "$1" "$2"
|
|
rc=$?
|
|
if [ $rc -ne 0 ] ; then
|
|
echo "ERROR - Unable to add groups ($1) to user ($2)! ($rc)" 1>&2
|
|
exit 3
|
|
fi
|
|
}
|
|
|
|
|
|
#
|
|
#### PROCESS
|
|
#
|
|
|
|
# Make sure we're running as root
|
|
if [ $(id -u) -ne 0 ] ; then
|
|
echo "ERROR - Not running as root!" 1>&2
|
|
exit 100
|
|
fi
|
|
|
|
# Create system group hpf-sudo for normal sudo users
|
|
system_groupadd hpf-sudo 700
|
|
|
|
# Create system group hpf-sudo-np for special sudo users that don't require a password
|
|
system_groupadd hpf-sudo-np 701
|
|
|
|
# Create the Ansible user
|
|
system_useradd hpf-ans 800
|
|
add_groups_to_user "hpf-sudo-np" hpf-ans
|
|
|
|
# Create /etc/sudoers.d/hpf
|
|
f="/etc/sudoers.d/hpf"
|
|
if [ ! -f "$f" ] ; then
|
|
curl -s -o "$f" \
|
|
"https://gitea.admin-a.hpetersenfamily.com/heath/ansible/raw/branch/main/etc_sudoers_d_hpf"
|
|
chown root:root "$f"
|
|
chmod u=rw,g=r,o= "$f"
|
|
fi
|
|
|
|
# Make sure python3 pip and venv are installed
|
|
if which -s apt ; then
|
|
apt update
|
|
apt install python3-pip python3-venv -y
|
|
fi
|
|
|
|
# Make sure the hpf-ans ansible venv exists
|
|
venv_dir=~hpf-ans/.ansible-venv
|
|
venv_script="$venv_dir/bin/activate"
|
|
if [ ! -f "$venv_script" ] ; then
|
|
su --login hpf-ans -c \
|
|
"python3 -m venv \"$venv_dir\""
|
|
fi
|
|
|
|
# Make sure pip is up to date in .ansible-venv
|
|
su --login hpf-ans -c \
|
|
"source \"$venv_script\"; pip install --upgrade pip"
|
|
|
|
# Make sure ansible is installed in .ansible-venv
|
|
if ! su --login hpf-ans -c "source \"$venv_script\"; which -s ansible" ; then
|
|
su --login hpf-ans -c \
|
|
"source \"$venv_script\"; pip install ansible"
|
|
fi
|
|
|
|
#### ansible-pull --only-if-changed -U https://gitea.admin-a.hpetersenfamily.com/heath/ansible.git 2>&1 | sudo tee -a /var/log/ansible-pull.log
|
|
|