mirror of
https://github.com/kelseyhightower/kubernetes-the-hard-way.git
synced 2025-12-16 09:48:57 +03:00
VTWO-14496: configure nodes with ansible instead of bash scripts
This commit is contained in:
54
Vagrantfile
vendored
54
Vagrantfile
vendored
@@ -15,8 +15,6 @@ hosts = {
|
||||
workers: (1..2).map { |i| "worker-node-#{i}" }
|
||||
}
|
||||
|
||||
generated_ansible_inventory_file="./inventory/generated"
|
||||
|
||||
Vagrant.configure("2") do |config|
|
||||
config.vm.box = "debian/stretch64"
|
||||
config.vm.box_version = "= 9.9.1"
|
||||
@@ -26,32 +24,40 @@ Vagrant.configure("2") do |config|
|
||||
# greet from every configured VM, revealing its hostname
|
||||
config.vm.provision "shell", inline: "echo Hello from \$HOSTNAME"
|
||||
|
||||
(hosts[:masters] + hosts[:workers]).each do |node_name|
|
||||
config.vm.define node_name do |node|
|
||||
node.vm.hostname = node_name
|
||||
|
||||
node.vm.provider :virtualbox do |v|
|
||||
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
|
||||
v.customize ["modifyvm", :id, "--memory", 512]
|
||||
v.customize ["modifyvm", :id, "--name", node_name]
|
||||
# complete the ansible inventory with groups
|
||||
config.trigger.before :up do |trigger|
|
||||
inventory = File.open("./inventory/generated", "w")
|
||||
all_hosts = []
|
||||
|
||||
hosts.keys.each do |group_name|
|
||||
inventory.puts "[#{group_name}]"
|
||||
hosts[group_name].each do |node_name|
|
||||
inventory.puts node_name
|
||||
all_hosts << node_name
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
config.trigger.after :up do |trigger|
|
||||
File.open(generated_ansible_inventory_file, "w") do |w|
|
||||
w.puts "[masters]"
|
||||
hosts[:masters].each { |host| w.puts host }
|
||||
|
||||
w.puts "[workers]"
|
||||
hosts[:workers].each { |host| w.puts host }
|
||||
inventory.puts "[k8s_nodes]"
|
||||
all_hosts.each do |node_name|
|
||||
inventory.puts node_name
|
||||
end
|
||||
end
|
||||
|
||||
"""
|
||||
config.trigger.after :destroy do |trigger|
|
||||
File.delete(generated_ansible_inventory_file) if File.exist?(generated_ansible_inventory_file)
|
||||
|
||||
# provision the vms
|
||||
hosts.keys.each do |node_group|
|
||||
hosts[node_group].each do |node_name|
|
||||
|
||||
config.vm.define node_name do |node|
|
||||
node.vm.hostname = node_name
|
||||
|
||||
node.vm.provider :virtualbox do |v|
|
||||
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
|
||||
v.customize ["modifyvm", :id, "--memory", 512]
|
||||
v.customize ["modifyvm", :id, "--name", node_name]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
"""
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user