This repository has been archived by the owner on Sep 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_ansible_env.rb
executable file
·71 lines (57 loc) · 2.26 KB
/
dynamic_ansible_env.rb
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
#!/usr/bin/env ruby
# @author Nikolaj Majorov
# create Ansible inventory
# dynamically from vagrant file
# example of ssh-config
#Host jira-server
# HostName 127.0.0.1
# User vagrant
# Port 2222
# UserKnownHostsFile /dev/null
# StrictHostKeyChecking no
# PasswordAuthentication no
# IdentityFile /home/nmajorov/workspace/ansible-jira/.vagrant/machines/jira-server/virtualbox/private_key
# IdentitiesOnly yes
# LogLevel FATAL
require 'json'
cmd = `vagrant ssh-config`
inventory = Hash.new
inventory["hosts"]=[]
cmd.each_line do |line|
if line.strip.start_with?("Host") && !line.strip.start_with?("HostName")
#puts "find host #{line}"
inventory["hosts"] << line.strip.slice(5..line.size)
end
# puts "line: #{line}"
if line.strip.start_with?("User") && !line.strip.start_with?("UserKnown")
#puts "find User line #{line}"
ansible_user = line.strip.slice("User ".size..line.size)
if inventory.key?("vars")
inventory["vars"]= inventory["vars"].merge({"ansible_ssh_user" => ansible_user})
else
ansible_user = line.strip.slice("User".size..line.size)
inventory["vars"]={"ansible_ssh_user" => ansible_user.strip}
end
end
if line.strip.start_with?("Port")
ansible_port = line.strip.slice("Port".size..line.size)
if inventory.key?("vars")
inventory["vars"] = inventory["vars"].merge({"ansible_port" => ansible_port.strip})
else
inventory["vars"]={"ansible_port" => ansible_port.strip}
end
end
# define ansible_ssh_private_key_file
if line.strip.start_with?("IdentityFile")
#a_ssh_key = line.strip.slice("IdentityFile".size..line.size).strip
a_ssh_key= "./id_key"
if inventory.key?("vars")
inventory["vars"] = inventory["vars"].merge({"ansible_ssh_private_key_file" => a_ssh_key})
else
inventory["vars"]={"ansible_ssh_private_key_file" => a_ssh_key}
end
#inventory["vars"]= inventory["vars"].merge({"ansible_ssh_extra_args" =>" -o IdentitiesOnly=yes" })
inventory["vars"]= inventory["vars"].merge({"ansible_ssh_extra_args" =>"-o IdentitiesOnly=yes" })
end
end
puts JSON.pretty_generate({"okd" => inventory})