-
Notifications
You must be signed in to change notification settings - Fork 5
/
RUNME.sh
executable file
·71 lines (56 loc) · 1.97 KB
/
RUNME.sh
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
#!/bin/sh
SUDOERS_ANSIBLE_CONTENT="
# Allow ansible user sudo access without a password prompt
ansible ALL=(ALL) NOPASSWD:ALL
"
SSHD_CONFIG_EXTRA_CONTENT="
# Enable PubkeyAuthentication
PubkeyAuthentication yes
# Disable PasswordAuthentication
PasswordAuthentication no
"
SSHD_CONFIG_FILE='/etc/ssh/sshd_config'
ANSIBLE_HOME='/ansible'
INCLUDE_SUDOERSD='#includedir /etc/sudoers.d/'
if [[ `cat /proc/version | grep -i 'red hat'` ]]; then
DISTRIBUTION='RHEL'
else
DISTRIBUTION=''
fi
if [ $DISTRIBUTION == 'RHEL' ]; then
SSHD_SERVICE="sshd"
else
SSHD_SERVICE="ssh"
fi
# Make sure we are in root's home directory
cd /root
# Create the ansible user
useradd ansible --home=$ANSIBLE_HOME
# Give ansible user permissions to run as root without a password prompt
mkdir -p /etc/sudoers.d
echo -e "$SUDOERS_ANSIBLE_CONTENT" > /etc/sudoers.d/ansible
chmod 440 /etc/sudoers.d/ansible
# Make sure the /etc/sudoers.d directory is included in the /etc/sudoers file
if [[ -z `grep $INCLUDE_SUDOERSD /etc/sudoers` ]] ; then
chmod +w /etc/sudoers
echo $INCLUDE_SUDOERSD >> /etc/sudoers
chmod -w /etc/sudoers
fi
# Enable public key authentication and disable password authentication
# Remove any existing references to PasswordAuthentication
sed -i "s/.*PasswordAuthentication.*//g" $SSHD_CONFIG_FILE
# Remove any existing references to PubkeyAuthentication
sed -i "s/.*PubkeyAuthentication.*//g" $SSHD_CONFIG_FILE
# Write the configuration we want to the sshd config file
echo -e "$SSHD_CONFIG_EXTRA_CONTENT" >> $SSHD_CONFIG_FILE
service $SSHD_SERVICE restart
# Create home directory for the ansible user
mkdir -p $ANSIBLE_HOME
chown ansible $ANSIBLE_HOME
chgrp ansible $ANSIBLE_HOME
# Add ansible.pub to the ansible user's authorized keys
cp /root/ansible.pub /tmp
sudo -u ansible -H mkdir -p $ANSIBLE_HOME/.ssh
sudo -u ansible -H chmod 700 $ANSIBLE_HOME/.ssh
sudo -u ansible -H cp /tmp/ansible.pub $ANSIBLE_HOME/.ssh/authorized_keys
sudo -u ansible -H chmod 644 $ANSIBLE_HOME/.ssh/authorized_keys