-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathVagrantfile
129 lines (115 loc) · 5.11 KB
/
Vagrantfile
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# --- About ---
# Configuration of 4 managed nodes + 1 control node
# --- Good to know ---
# Password for root user is "vagrant"
# --- IPs ---
# controller 192.168.56.100
# managedX 192.168.56.(100 + X)
# managedX 192.168.57.(100 + X)
# Where X ranges from 1 to 4
# --- Accessing the nodes ---
# Each node can be accessed by its short name - controller, manged1, manged2, manged3,manged4
# Alternatively fqdn can be used, e. g. controller.example.com, managed1.example.com
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
NODES_NUMBER = ENV['NODES_NUMBER'] = '4'
ADDITIONAL_DISK_SIZE = 1024 * 5 # 5GiB
USER = ENV['USER'] = 'root'
USER_HOME = ENV['USER_HOME'] = '/root'
USER_PASSWORD = ENV['USER_PASSWORD'] = 'vagrant'
BOX = 'bento/centos-8'
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Vagrant.configure '2' do |config|
config.ssh.username = USER
config.ssh.password = USER_PASSWORD
config.ssh.insert_key = 'true'
config.vm.synced_folder ".", "/vagrant", type: "rsync"
config.vm.box_check_update = false
config.vm.provision "shell", inline: <<-INPUT
# # # # # # BEGIN: Install python interpreter mandatory to use Ansible
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
yum module install -y python36
yum clean all -y
# # # # # # END
INPUT
(1..NODES_NUMBER.to_i).each do |i|
config.vm.define "managed#{i}" do |node|
disk_file = "./storage/disk#{i}.vdi"
node.vm.box = BOX
node.vm.hostname = "managed#{i}"
node.vm.network "private_network", ip: "192.168.56.#{i + 100}"
node.vm.network "private_network", ip: "192.168.57.#{i + 100}", auto_config: false
node.vm.provider "virtualbox" do |v|
unless File.exist? disk_file
v.customize ['createhd', '--filename', disk_file, '--size', ADDITIONAL_DISK_SIZE]
end
v.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', disk_file]
end
node.vm.provision "shell", inline: <<-INPUT
# # # # # # BEGIN: Define fqdn and short names
echo "127.0.0.1 localhost managed#{i} managed#{i}.example.com" > /etc/hosts
echo "127.0.1.1 managed#{i}" >> /etc/hosts
for ((i=1; i<=#{ ENV['NODES_NUMBER'] }; i++))
do
echo "192.168.56.10$i managed$i managed$i.example.com" >> /etc/hosts
done
echo "192.168.56.100 controller controller.example.com" >> /etc/hosts
# # # # # # END
INPUT
end
end
config.vm.define "controller" do |controller|
controller.vm.box = BOX
controller.vm.hostname = "controller"
controller.vm.network "private_network", ip: "192.168.56.100"
controller.vm.provision "shell", inline: <<-INPUT
# # # # # # BEGIN: Define fqdn and short names
echo "127.0.0.1 localhost controller controller.example.com" > /etc/hosts
echo "127.0.1.1 controller" >> /etc/hosts
for ((i=1; i<=#{ ENV['NODES_NUMBER'] }; i++))
do
echo "192.168.56.10$i managed$i managed$i.example.com" >> /etc/hosts
done
# # # # # # END
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
# # # # # # BEGIN: Install man pages
yum install -y man-pages
# # # # # # END
# # # # # # BEGIN: Install editing tools and repo containing ansible
yum install -y epel-release --nogpgcheck
sed -i 's|^metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-8&arch=$basearch&infra=$infra&content=$contentdir|baseurl=https://dl.fedoraproject.org/pub/archive/epel/8.5.2022-05-10/Everything/x86_64/|' /etc/yum.repos.d/epel.repo
yum install -y vim --nogpgcheck
wget http://mirror.centos.org/centos/8-stream/AppStream/x86_64/os/Packages/sshpass-1.09-4.el8.x86_64.rpm
rpm -i sshpass*.rpm
# # # # # # END
# # # # # # BEGIN: Define path where ssh keys are going to be stored
export SSH_PATH=#{ ENV['USER_HOME'] }/.ssh
# # # # # # END
# # # # # # BEGIN: Generate public and private key pairs - id_rsa, id_rsa.pub
mkdir -pv $SSH_PATH
ssh-keygen -N "" -f $SSH_PATH/id_rsa
# # # # # # END
# # # # # # BEGIN: Add fingerprints of all managed servers
for ((i=1; i<=#{ ENV['NODES_NUMBER'] }; i++))
do
for name in {managed$i.example.com,managed$i}
do
export FINGERPRINT=$(ssh-keyscan -t rsa $name 2> /dev/null)
if ! grep -Fxq "$FINGERPRINT" ~/.ssh/known_hosts 2> /dev/null
then
echo $FINGERPRINT >> ~/.ssh/known_hosts
fi
done
done
# # # # # # END
# # # # # # BEGIN: Push public key to all managed servers
for ((i=1; i<=#{ ENV['NODES_NUMBER'] }; i++))
do
sshpass -p "#{ ENV['USER_PASSWORD'] }" ssh-copy-id -f -i $SSH_PATH/id_rsa.pub #{ ENV['USER'] }@managed$i 2> /dev/null
done
# # # # # # END
yum clean all -y
INPUT
end
end