vagrant-ansible_inventory 0.0.6 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/README.md +2 -0
- data/Vagrantfile +3 -3
- data/Vagrantfile2 +1 -1
- data/lib/vagrant/ansible_inventory/commands/inventory.rb +119 -32
- data/lib/vagrant/ansible_inventory/commands/root.rb +4 -4
- data/lib/vagrant/ansible_inventory/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bcca8d0ad9622d35a0eaa52d56d51c39a67a4558
|
4
|
+
data.tar.gz: 6e42679961730ac630312264f4d69daf881cdca2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9887708fbfe36c554c269926b47ae194462556a6bd21fda6eae230c3ffcba7c00bb4ba393e8b5952eb6c13d6fb5fbbb09bea24305dbc32b284076b2f730b8339
|
7
|
+
data.tar.gz: 38a08aedc1772303617686bcb1912c7bdff959601d9ce018a3245e4e8152d42dfbf23e31db6547529cd600f69c4bde3a23fa11521b02641e10a7107e5dd6b1a9
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# Vagrant::AnsibleInventory
|
2
2
|
|
3
|
+
[![Join the chat at https://gitter.im/joiggama/vagrant-ansible_inventory](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/joiggama/vagrant-ansible_inventory?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
4
|
+
|
3
5
|
Vagrant plugin for building ansible inventory files.
|
4
6
|
|
5
7
|
Helps defining and building ansible inventory files programatically via
|
data/Vagrantfile
CHANGED
@@ -11,9 +11,9 @@ Vagrant.configure(2) do |config|
|
|
11
11
|
end
|
12
12
|
}
|
13
13
|
|
14
|
-
define_vm.call 'master', 'trusty32', 256
|
15
|
-
define_vm.call 'slave-1', 'trusty32', 256
|
16
|
-
define_vm.call 'slave-2', 'trusty32', 256
|
14
|
+
define_vm.call 'master', 'ubuntu/trusty32', 256
|
15
|
+
define_vm.call 'slave-1', 'ubuntu/trusty32', 256
|
16
|
+
define_vm.call 'slave-2', 'ubuntu/trusty32', 256
|
17
17
|
|
18
18
|
config.ansible.groups = {
|
19
19
|
'cluster:children' => ['master', 'slaves'],
|
data/Vagrantfile2
CHANGED
@@ -5,71 +5,158 @@ module VagrantPlugins
|
|
5
5
|
module Commands
|
6
6
|
class Inventory < Vagrant.plugin(2, :command)
|
7
7
|
|
8
|
+
def self.synopsis
|
9
|
+
'dynamic ansible inventory'
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(args, env)
|
13
|
+
super
|
14
|
+
|
15
|
+
@sub_args, @env = args, env
|
16
|
+
end
|
17
|
+
|
8
18
|
def execute
|
9
|
-
|
19
|
+
opts = OptionParser.new do |op|
|
20
|
+
op.banner = 'Usage: vagrant ansible inventory [<options>]'
|
21
|
+
op.separator ''
|
22
|
+
op.separator 'Available options:'
|
23
|
+
|
24
|
+
op.on('-l', '--list', 'List all hosts as json') do |target|
|
25
|
+
@env.ui.info json, prefix: false
|
26
|
+
exit
|
27
|
+
end
|
28
|
+
|
29
|
+
op.on('-h', '--help', 'Show this message') do
|
30
|
+
@env.ui.info opts.help, prefix: false
|
31
|
+
exit
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
opts.parse!(ARGV[2..-1])
|
36
|
+
|
37
|
+
@env.ui.info ini, prefix: false
|
10
38
|
end
|
11
39
|
|
12
40
|
private
|
13
41
|
|
14
42
|
def build
|
15
|
-
inv = {}
|
16
|
-
|
17
43
|
# Detect meta, explicit and implicit groups
|
18
|
-
|
19
|
-
|
20
|
-
|
44
|
+
@meta_groups = get_meta_groups
|
45
|
+
@explicit_groups = get_explicit_groups
|
46
|
+
@implicit_groups = get_implicit_groups
|
47
|
+
|
48
|
+
@inventory = {}
|
21
49
|
|
22
50
|
# Assign grouped nodes to inventory
|
23
|
-
|
24
|
-
|
25
|
-
|
51
|
+
@implicit_groups.each do |group_name|
|
52
|
+
@inventory[group_name] = nodes[group_name].merge id: group_name
|
53
|
+
end
|
54
|
+
|
55
|
+
@explicit_groups.each do |group_name, group_nodes|
|
56
|
+
@inventory[group_name] = group_nodes.map{|node| nodes[node].merge id: node }
|
57
|
+
end
|
26
58
|
|
27
|
-
|
59
|
+
@meta_groups.each do |group_name, group_names|
|
60
|
+
@inventory[group_name] = group_names
|
61
|
+
end
|
28
62
|
end
|
29
63
|
|
30
64
|
def config
|
31
65
|
@config ||= with_target_vms{}.first.config
|
32
66
|
end
|
33
67
|
|
34
|
-
def
|
35
|
-
|
68
|
+
def get_explicit_groups
|
69
|
+
groups.reject{|group| @meta_groups.include?(group) } || []
|
36
70
|
end
|
37
71
|
|
38
|
-
def
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
ssh_port: 22,
|
45
|
-
ssh_private_key_file: machine.ssh_info[:private_key_path].first
|
46
|
-
}
|
47
|
-
end
|
72
|
+
def get_implicit_groups
|
73
|
+
nodes.keys - @explicit_groups.values.flatten
|
74
|
+
end
|
75
|
+
|
76
|
+
def get_meta_groups
|
77
|
+
groups.select{|group| group.include?(':children') }
|
48
78
|
end
|
49
79
|
|
50
|
-
|
51
|
-
|
80
|
+
|
81
|
+
def groups
|
82
|
+
@groups ||= config.ansible.groups
|
52
83
|
end
|
53
84
|
|
54
|
-
def
|
55
|
-
|
85
|
+
def ini
|
86
|
+
build
|
56
87
|
|
57
|
-
|
88
|
+
output = "# Generated by vagrant-ansible_inventory\n\n"
|
89
|
+
|
90
|
+
@inventory.each do |entries|
|
58
91
|
group_name, children = *entries
|
59
|
-
|
92
|
+
output << "[#{group_name}]\n"
|
60
93
|
|
61
94
|
if children.kind_of? Hash
|
62
|
-
|
95
|
+
output << "#{node_attributes(:id, children)}\n"
|
63
96
|
else
|
64
97
|
children.each do |child|
|
65
|
-
|
98
|
+
output << "#{(child.kind_of?(Hash) ? node_attributes(:id, child) : child)}\n"
|
66
99
|
end
|
67
100
|
end
|
68
101
|
|
69
|
-
|
102
|
+
output << "\n"
|
103
|
+
end
|
104
|
+
|
105
|
+
output
|
106
|
+
end
|
107
|
+
|
108
|
+
def json
|
109
|
+
build
|
110
|
+
|
111
|
+
groups = {}
|
112
|
+
hostvars = {}
|
113
|
+
|
114
|
+
@implicit_groups.each do |group|
|
115
|
+
host = @inventory[group][:ansible_ssh_host]
|
116
|
+
groups[group] = [host]
|
117
|
+
vars = @inventory[group].except(:id, :ansible_ssh_host)
|
118
|
+
hostvars[host] = vars
|
119
|
+
end
|
120
|
+
|
121
|
+
@explicit_groups.each do |group|
|
122
|
+
name = group.first
|
123
|
+
groups[name] = []
|
124
|
+
@inventory[name].each do |node|
|
125
|
+
groups[name] << node[:ansible_ssh_host]
|
126
|
+
host = node[:ansible_ssh_host]
|
127
|
+
vars = node.except(:id, :ansible_ssh_host)
|
128
|
+
hostvars[host] = vars
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
@meta_groups.each do |group|
|
133
|
+
name = group.first.split(':').first
|
134
|
+
groups[name] = group.last.map do |member|
|
135
|
+
if @inventory[member].is_a? Hash
|
136
|
+
@inventory[member][:ansible_ssh_host]
|
137
|
+
else
|
138
|
+
@inventory[member].map{|n| n[:ansible_ssh_host] }
|
139
|
+
end if @inventory[member]
|
140
|
+
end.compact.flatten
|
70
141
|
end
|
71
142
|
|
72
|
-
|
143
|
+
groups.merge({_meta: {hostvars: hostvars}}).to_json
|
144
|
+
end
|
145
|
+
|
146
|
+
def nodes
|
147
|
+
@nodes ||= with_target_vms{}.each_with_object({}) do |machine, hash|
|
148
|
+
raise Vagrant::Errors::SSHNotReady unless machine.ssh_info
|
149
|
+
hash[machine.name.to_s] = {
|
150
|
+
ansible_ssh_user: 'vagrant',
|
151
|
+
ansible_ssh_host: machine.provider.driver.read_guest_ip(1),
|
152
|
+
ansible_ssh_port: 22,
|
153
|
+
ansible_ssh_private_key_file: machine.ssh_info[:private_key_path].first
|
154
|
+
}
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
def node_attributes(id, node)
|
159
|
+
"#{node.delete(id)} #{node.map{|k| "#{k[0]}=#{k[1]}"}.join(' ')}"
|
73
160
|
end
|
74
161
|
|
75
162
|
end
|
@@ -6,7 +6,7 @@ module VagrantPlugins
|
|
6
6
|
class Root < Vagrant.plugin(2, :command)
|
7
7
|
|
8
8
|
def self.synopsis
|
9
|
-
'
|
9
|
+
'build ansible inventory'
|
10
10
|
end
|
11
11
|
|
12
12
|
def initialize(argv, env)
|
@@ -36,9 +36,9 @@ module VagrantPlugins
|
|
36
36
|
|
37
37
|
def help
|
38
38
|
opts = OptionParser.new do |o|
|
39
|
-
o.banner =
|
40
|
-
o.separator
|
41
|
-
o.separator
|
39
|
+
o.banner = 'Usage: vagrant ansible <subcommand> [<options>]'
|
40
|
+
o.separator ''
|
41
|
+
o.separator 'Available subcommands:'
|
42
42
|
|
43
43
|
keys = []
|
44
44
|
@subcommands.each { |key, value| keys << key.to_s }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vagrant-ansible_inventory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ignacio Galindo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-07-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -81,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
81
|
version: '0'
|
82
82
|
requirements: []
|
83
83
|
rubyforge_project:
|
84
|
-
rubygems_version: 2.4.
|
84
|
+
rubygems_version: 2.4.8
|
85
85
|
signing_key:
|
86
86
|
specification_version: 4
|
87
87
|
summary: Vagrant plugin for building ansible inventory files.
|