mirror of
https://github.com/kelseyhightower/kubernetes-the-hard-way.git
synced 2025-12-15 09:18:58 +03:00
wip ansible inventory
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,4 +1,5 @@
|
|||||||
.vagrant
|
.vagrant
|
||||||
kubernetes/
|
kubernetes/
|
||||||
etcd*/
|
etcd*/
|
||||||
cluster.config
|
cluster.config
|
||||||
|
inventory/generated
|
||||||
49
Vagrantfile
vendored
49
Vagrantfile
vendored
@@ -4,6 +4,19 @@ require 'open-uri'
|
|||||||
|
|
||||||
Vagrant.require_version ">= 2.2.4"
|
Vagrant.require_version ">= 2.2.4"
|
||||||
|
|
||||||
|
unless Vagrant.has_plugin?("vagrant-scp")
|
||||||
|
raise 'vagrant-scp is not installed! Please run vagrant plugin install vagrant-scp'
|
||||||
|
end
|
||||||
|
|
||||||
|
hosts = {
|
||||||
|
masters: [
|
||||||
|
"master-node"
|
||||||
|
],
|
||||||
|
workers: (1..2).map { |i| "worker-node-#{i}" }
|
||||||
|
}
|
||||||
|
|
||||||
|
generated_ansible_inventory_file="./inventory/generated"
|
||||||
|
|
||||||
Vagrant.configure("2") do |config|
|
Vagrant.configure("2") do |config|
|
||||||
config.vm.box = "debian/stretch64"
|
config.vm.box = "debian/stretch64"
|
||||||
config.vm.box_version = "= 9.9.1"
|
config.vm.box_version = "= 9.9.1"
|
||||||
@@ -13,26 +26,32 @@ Vagrant.configure("2") do |config|
|
|||||||
# greet from every configured VM, revealing its hostname
|
# greet from every configured VM, revealing its hostname
|
||||||
config.vm.provision "shell", inline: "echo Hello from \$HOSTNAME"
|
config.vm.provision "shell", inline: "echo Hello from \$HOSTNAME"
|
||||||
|
|
||||||
config.vm.define "master-node" do |node|
|
(hosts[:masters] + hosts[:workers]).each do |node_name|
|
||||||
node.vm.hostname = "master-node"
|
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", "master-node"]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
(1..2).each do |i|
|
|
||||||
config.vm.define "worker-node-#{i}" do |node|
|
|
||||||
node.vm.hostname = "worker-node-#{i}"
|
|
||||||
|
|
||||||
node.vm.provider :virtualbox do |v|
|
node.vm.provider :virtualbox do |v|
|
||||||
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
|
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
|
||||||
v.customize ["modifyvm", :id, "--memory", 512]
|
v.customize ["modifyvm", :id, "--memory", 512]
|
||||||
v.customize ["modifyvm", :id, "--name", "worker-node-#{i}"]
|
v.customize ["modifyvm", :id, "--name", node_name]
|
||||||
end
|
end
|
||||||
end
|
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 }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
"""
|
||||||
|
config.trigger.after :destroy do |trigger|
|
||||||
|
File.delete(generated_ansible_inventory_file) if File.exist?(generated_ansible_inventory_file)
|
||||||
|
end
|
||||||
|
"""
|
||||||
end
|
end
|
||||||
|
|||||||
3
ansible.cfg
Normal file
3
ansible.cfg
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
[defaults]
|
||||||
|
remote_tmp = /tmp/$USER/ansible
|
||||||
|
inventory = inventory/
|
||||||
51
inventory/vagrant.py
Executable file
51
inventory/vagrant.py
Executable file
@@ -0,0 +1,51 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# Shameless copy of https://gist.github.com/d-a-n/baabf3b010a6851f0e84
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
import paramiko
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def parse_args():
|
||||||
|
parser = argparse.ArgumentParser(description="Vagrant inventory script")
|
||||||
|
group = parser.add_mutually_exclusive_group(required=True)
|
||||||
|
group.add_argument('--list', action='store_true')
|
||||||
|
group.add_argument('--host')
|
||||||
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
def list_running_hosts():
|
||||||
|
cmd = "vagrant status --machine-readable"
|
||||||
|
status = subprocess.check_output(cmd.split()).rstrip()
|
||||||
|
hosts = []
|
||||||
|
for line in status.split('\n'):
|
||||||
|
(_, host, key, value) = line.split(',',3)
|
||||||
|
if key == 'state' and value == 'running':
|
||||||
|
hosts.append(host)
|
||||||
|
return hosts
|
||||||
|
|
||||||
|
|
||||||
|
def get_host_details(host):
|
||||||
|
cmd = "vagrant ssh-config {}".format(host)
|
||||||
|
p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
|
||||||
|
config = paramiko.SSHConfig()
|
||||||
|
config.parse(p.stdout)
|
||||||
|
c = config.lookup(host)
|
||||||
|
return {'ansible_ssh_host': c['hostname'],
|
||||||
|
'ansible_ssh_port': c['port'],
|
||||||
|
'ansible_ssh_user': c['user'],
|
||||||
|
'ansible_ssh_private_key_file': c['identityfile'][0]}
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
args = parse_args()
|
||||||
|
if args.list:
|
||||||
|
hosts = list_running_hosts()
|
||||||
|
json.dump({'vagrant': hosts}, sys.stdout)
|
||||||
|
else:
|
||||||
|
details = get_host_details(args.host)
|
||||||
|
json.dump(details, sys.stdout)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
0
masters.yml
Normal file
0
masters.yml
Normal file
0
workers.yml
Normal file
0
workers.yml
Normal file
Reference in New Issue
Block a user