feat: generate dev VM IP address based on inventory

This commit is contained in:
Khue Doan 2022-01-02 14:02:09 +07:00
parent 66c70c023f
commit 6f0f8dd6f9
3 changed files with 30 additions and 30 deletions

1
metal/.gitignore vendored
View File

@ -1 +0,0 @@
host_vars/dev*.yml

53
metal/Vagrantfile vendored
View File

@ -1,47 +1,42 @@
# require 'ipaddr' # TODO dynamic IP based on inventory OR dynamic inventory
require 'yaml'
ip_prefix = '192.168.1.' # TODO see above
inventory = YAML.load_file(File.join(__dir__, './inventories/dev.yml'))
group_vars = YAML.load_file(File.join(__dir__, './group_vars/all.yml'))
Vagrant.configure("2") do |config|
config.vm.box = "rockylinux/8"
node_count = 1
# TODO clean up
# - support multiple nodes
# - generate inventory dynamically https://www.simonholywell.com/post/2016/02/intelligent-vagrant-and-ansible-files/
Dir.mkdir(File.join(__dir__, './host_vars')) unless Dir.exist?(File.join(__dir__, './host_vars'))
(0..(node_count-1)).each do |i|
config.vm.define "dev#{i}" do |dev|
dev.vm.network "public_network", ip: "#{ip_prefix}#{200+i+1}"
dev.vm.hostname = "dev#{i}"
dev.vm.disk :disk, size: "256GB", primary: true
File.open("#{File.join(__dir__, './host_vars')}/dev#{i}.yml" ,'w') do |f|
f.write "ansible_host: #{ip_prefix}#{200+i+1}"
inventory['metal']['children'].each do |group, properties|
properties['hosts'].each do |host, host_vars|
config.vm.define host do |node|
ip = host_vars['ansible_host']
node.vm.network "private_network", ip: ip
node.vm.hostname = host
node.vm.disk :disk, size: "256GB", primary: true
end
end
end
config.vm.provider "virtualbox" do |vb|
# TODO this is ridiculous for a dev VM, optimize it
# TODO this is ridiculous for a local environment, optimize it
vb.cpus = 6
vb.memory = "10240"
end
config.vm.provision "file", source: "#{group_vars['ansible_ssh_private_key_file']}.pub", destination: "/tmp/id_ed25519.pub"
config.vm.provision "file",
source: "#{group_vars['ansible_ssh_private_key_file']}.pub",
destination: "/tmp/id_ed25519.pub"
# TODO move part of this to Ansible?
config.vm.provision "shell", inline: <<-SHELL
mkdir /#{group_vars['ansible_user']}/.ssh
cat /tmp/id_ed25519.pub >> /#{group_vars['ansible_user']}/.ssh/authorized_keys
setenforce Permissive
dnf install -y iscsi-initiator-utils
systemctl enable --now iscsid
sysctl fs.inotify.max_user_instances=8192
sysctl -p
SHELL
# TODO move most of this to Ansible?
config.vm.provision "shell",
reboot: true,
inline: <<-SHELL
mkdir /#{group_vars['ansible_user']}/.ssh
cat /tmp/id_ed25519.pub >> ~#{group_vars['ansible_user']}/.ssh/authorized_keys
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
dnf install -y iscsi-initiator-utils
systemctl enable --now iscsid
sysctl fs.inotify.max_user_instances=8192
sysctl -p
SHELL
end

View File

@ -1,5 +1,11 @@
# TODO support multiple nodes (doesn't work yet)
metal:
children:
masters:
hosts:
dev0: {ansible_host: 192.168.56.10}
# dev1: {ansible_host: 192.168.56.11}
# dev2: {ansible_host: 192.168.56.12}
workers:
hosts: {}
# dev3: {ansible_host: 192.168.56.13}