-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathansible-instance
executable file
·56 lines (50 loc) · 1.71 KB
/
ansible-instance
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
#!/usr/bin/env ruby
#
# Copyright 2016-2019 Victor Penso
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
begin
# current working directory
path = ENV['PWD']
# name of the virtual machine instance
name = File.basename(path).split('.')[0]
# get the network configuration
null, ip, mac = `virsh-nat-bridge lookup #{name}`.split
# target configuration file for Ansible
config_file = "#{path}/ansible.ini"
# Ansible configuration
config = <<-EOF
#{name} ansible_user=root ansible_host=#{ip} host_key_checking=no ansible_ssh_private_key_file=#{path}/keys/id_rsa
EOF
# Create a configuration file if missing
unless File.exist? config_file
File.open(config_file,'w') do |f|
f.puts config.gsub(/^ /,'')
end
$stdout.puts "#{config_file} written."
end
$stdout.puts `ansible -i #{config_file} #{name} #{ARGV.join(' ')}`
rescue => exc
$stderr.puts "ERROR: #{exc.message}"
$stderr.puts " use -h for detailed instructions"
if $options.debug
$stderr.puts '-- Stack Trace --'
$stderr.puts exc.backtrace
else
$stderr.puts 'You may want run this in debug mode with \'-d\''
end
exit 1
end
exit 0