Skip to content

Commit e897092

Browse files
authored
create setup-user.sh
1 parent fb9f240 commit e897092

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

ansible/IaC/setup-user.sh

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#!/bin/bash
2+
3+
now=`date +%d%b%Y-%H%M`
4+
5+
exp()
6+
{
7+
"$1" <(cat <<-EOF
8+
spawn passwd $USER
9+
expect "Enter new UNIX password:"
10+
send -- "$passw\r"
11+
expect "Retype new UNIX password:"
12+
send -- "$passw\r"
13+
expect eof
14+
EOF
15+
)
16+
echo "password for USER $USER updated successfully - adding to sudoers file now"
17+
}
18+
19+
setup_pass()
20+
{
21+
22+
if [ $1 == "sles" ];then
23+
24+
if [ ! -f /usr/bin/expect ] && [ ! -f /bin/expect ];then
25+
# zypper -y update
26+
zypper install -y expect
27+
exp "/usr/bin/expect"
28+
else
29+
exp "/usr/bin/expect"
30+
fi
31+
32+
elif [ $1 == "ubuntu" ];then
33+
34+
if [ ! -f /usr/bin/expect ] && [ ! -f /bin/expect ];then
35+
apt-get update
36+
apt install -y expect
37+
exp "/usr/bin/expect"
38+
else
39+
exp "/usr/bin/expect"
40+
fi
41+
42+
elif [ $1 == "amzn" ];then
43+
44+
echo $1
45+
if [ ! -f /usr/bin/expect ] && [ ! -f /bin/expect ];then
46+
rpm -Uvh http://epel.mirror.net.in/epel/6/x86_64/epel-release-6-8.noarch.rpm
47+
yum install -y expect
48+
exp "/usr/bin/expect"
49+
else
50+
exp "/usr/bin/expect"
51+
fi
52+
53+
elif [ $1 == "centos" ];then
54+
55+
echo $1
56+
if [ ! -f /usr/bin/expect ] && [ ! -f /bin/expect ];then
57+
rpm -Uvh http://epel.mirror.net.in/epel/6/x86_64/epel-release-6-8.noarch.rpm
58+
yum install -y expect
59+
exp "/bin/expect"
60+
else
61+
exp "/bin/expect"
62+
fi
63+
else
64+
echo "could not find case $1"
65+
fi
66+
67+
}
68+
69+
update_conf()
70+
{
71+
sudofile="/etc/sudoers"
72+
sshdfile="/etc/ssh/sshd_config"
73+
mkdir -p /home/backup
74+
if [ -f $sudofile ];then
75+
cp -p $sudofile /home/backup/sudoers-$now
76+
sa=`grep $USER $sudofile | wc -l`
77+
if [ $sa -gt 0 ];then
78+
echo "$USER user already present in $sudofile - no changes required"
79+
grep $USER $sudofile
80+
else
81+
# echo "$USER ALL=(ALL) ALL" >> $sudofile
82+
echo "$USER ALL=(ALL) NOPASSWD: ALL" >> $sudofile
83+
echo "updated the sudoers file successfully"
84+
fi
85+
else
86+
echo "could not find $sudofile"
87+
fi
88+
89+
if [ -f $sshdfile ];then
90+
cp -p $sshdfile /home/backup/sshd_config-$now
91+
sed -i '/ClientAliveInterval.*0/d' $sshdfile
92+
echo "ClientAliveInterval 240" >> $sshdfile
93+
sed -i '/PasswordAuthentication.*no/d' $sshdfile
94+
sed -i '/PasswordAuthentication.*yes/d' $sshdfile
95+
echo "PasswordAuthentication yes" >> $sshdfile
96+
#sed -i '/PermitRootLogin.*yes/d' $sshdfile
97+
#sed -i '/PermitRootLogin.*prohibit-password/d' $sshdfile
98+
#echo "PermitRootLogin yes" >> $sshdfile
99+
echo "updated $sshdfile Successfully -- restarting sshd service"
100+
service sshd restart
101+
else
102+
echo "could not find $sshdfile"
103+
fi
104+
}
105+
106+
############### MAIN ###################
107+
108+
USER="devops"
109+
GROUP="devops"
110+
passw="today@1234"
111+
112+
if id -u "$USER" &>/dev/null; then
113+
echo "devops user exists no action required.."
114+
exit 0
115+
else
116+
echo "devops user missing, continue to create it.."
117+
fi
118+
119+
if [ -f /etc/os-release ];then
120+
osname=`grep ID /etc/os-release | egrep -v 'VERSION|LIKE|VARIANT|PLATFORM' | cut -d'=' -f2 | sed -e 's/"//' -e 's/"//'`
121+
echo $osname
122+
else
123+
echo "can not locate /etc/os-release - unable find the osname"
124+
exit 8
125+
fi
126+
127+
case "$osname" in
128+
sles|amzn|ubuntu|centos)
129+
userdel -r $USER
130+
groupdel $GROUP
131+
sleep 3
132+
groupadd $GROUP
133+
useradd $USER -m -d /home/$USER -s /bin/bash -g $GROUP
134+
setup_pass $osname
135+
update_conf
136+
;;
137+
*)
138+
echo "could not determine the correct osname -- found $osname"
139+
;;
140+
esac
141+
exit 0

0 commit comments

Comments
 (0)