-
Notifications
You must be signed in to change notification settings - Fork 10
/
ansible_inventory.rb
executable file
·193 lines (143 loc) · 4.78 KB
/
ansible_inventory.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/ruby
# This inventory extracts VM information from OpenNebula to be used by Ansible.
# The following information is obtained:
# * All the parameters in the Context and in the User Template
# * Disk information: size, image_id and persistancy
# * Network information: IP, MAC address, etc
# Only VMs that meet any of these conditions are returned by the inventory
# * It has a context variable called "ANSIBLE". The content of this variable is
# the ansible group it will be placed into
# * It has the "ansible" label
# * It has a label under "ansible". Whatever the label is will be the ansible
# group it will be placed into
# If there is no DNS for the VM, you may use the User Template variable
# "ANSIBLE_HOST" in order to specify what IP should this inventory report to
# ansible:
# * ANSIBLE_HOST is undefined: Ansible will use the VM name
# * ANSIBLE_HOST=yes: Ansible will use the IP of the first interface
# * ANSIBLE_HOST=eth0: Ansible will use the IP of the first interface
# * ANSIBLE_HOST=eth1: Ansible will use the IP of the second interface
# * ANSIBLE_HOST=ethN: Ansible will use the IP of the N-1 interface
ONE_LOCATION=ENV["ONE_LOCATION"]
if !ONE_LOCATION
RUBY_LIB_LOCATION = "/usr/lib/one/ruby"
VAR_LOCATION = "/var/lib/one"
LIB_LOCATION = "/usr/lib/one"
ETC_LOCATION = "/etc/one"
else
RUBY_LIB_LOCATION = ONE_LOCATION + "/lib/ruby"
VAR_LOCATION = ONE_LOCATION + "/var"
LIB_LOCATION = ONE_LOCATION + "/lib"
ETC_LOCATION = ONE_LOCATION + "/etc"
end
$: << RUBY_LIB_LOCATION
require 'opennebula'
include OpenNebula
require 'pp'
require 'json'
class Inventory
attr_reader :inventory, :hostvars
def initialize
@inventory = {}
@hostvars = {}
mkgroup(:all)
end
def host(name, group = :all)
mkgroup(group)
@inventory[group][:hosts] << name
end
def groupvar(group, var, val)
mkgroup(group)
@inventory[group][:vars][var.downcase] = val
end
def hostvar(host, var, val)
@hostvars[host] = {} if @hostvars[host].nil?
@hostvars[host][var.downcase] = val
end
def to_json
{
"_meta" => {
"hostvars" => @hostvars
},
}.merge(@inventory).to_json
end
private
def mkgroup(group)
@inventory[group] = {} if @inventory[group].nil?
@inventory[group][:hosts] = [] if @inventory[group][:hosts].nil?
@inventory[group][:vars] = {} if @inventory[group][:vars].nil?
end
end
client = Client.new
vm_pool = VirtualMachinePool.new(client, -1)
rc = vm_pool.info
if OpenNebula.is_error?(rc)
puts rc.message
exit -1
end
inventory = Inventory.new
vm_pool.each do |vm|
# explizit info pull to gather all Context vairables
vm.info
# Require READY=YES if REPORT_READY=YES
if vm["TEMPLATE/CONTEXT/REPORT_READY"] == "YES" &&
vm["TEMPLATE/CONTEXT/TOKEN"] == "YES" &&
vm["USER_TEMPLATE/READY"] != "YES" then
next
end
labels = vm["USER_TEMPLATE/LABELS"].split(",").select do |e|
e.match(/ansible(\/|$)/)
end rescue nil
ansible_role = vm["TEMPLATE/CONTEXT/ANSIBLE_ROLE"] rescue nil
if (labels.nil? || labels.empty?) && (ansible_role.nil? || ansible_role.empty?)
next
end
id = "one-#{vm['ID']}"
name = vm["NAME"]
vm.each('TEMPLATE/CONTEXT/*') do |e|
var = e.name
val = e.text
next if %w(DISK_ID NETWORK TARGET).include?(var)
inventory.hostvar(name, var, val)
end
vm.each('USER_TEMPLATE/*') do |e|
var = e.name
val = e.text
next if %w(ANSIBLE_HOST).include?(var)
inventory.hostvar(name, var, val)
end
i=0
vm.each('TEMPLATE/DISK') do |disk|
%w(image image_id size).each do |key|
val = disk[key.upcase]
if !val.nil? && !val.empty?
inventory.hostvar(name, "disk_#{i}_#{key}", val)
end
end
persistent = !(disk['CLONE'] == 'YES')
inventory.hostvar(name, "disk_#{i}_persistent", persistent)
i+=1
end
ansible_host = vm["USER_TEMPLATE/ANSIBLE_HOST"]
ansible_host ||= vm["TEMPLATE/CONTEXT/ANSIBLE_HOST"]
ansible_host ||= ENV['ANSIBLE_HOST']
if ansible_host
var_ip = if ansible_host.match(/^eth\d+$/i)
ansible_host.upcase + "_IP"
else
"ETH0_IP"
end
ip = vm["TEMPLATE/CONTEXT/#{var_ip}"]
inventory.hostvar(name, "ansible_host", ip) if ip
end
labels.each do |label|
_, group = label.split("/", 2)
inventory.host(name, group.gsub("/", ",")) if group
end if labels
if ansible_role && !ansible_role.empty?
inventory.host(name, ansible_role)
end
inventory.host(name)
end
puts inventory.to_json
exit 0