vagrant-ansible_inventory 0.0.6 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 05ede9a6d6ab64d6dc570af0ef6815a4cabc4ade
4
- data.tar.gz: 50bc7e225a0db6a0c1d69c4ebeb4b4e790dfdda9
3
+ metadata.gz: bcca8d0ad9622d35a0eaa52d56d51c39a67a4558
4
+ data.tar.gz: 6e42679961730ac630312264f4d69daf881cdca2
5
5
  SHA512:
6
- metadata.gz: 2ea698d66a46afce758015d05bb2c7e82b251e6ffc885cae12bc2d2f6037b0e2b84a031d19f2f54906e9265f8f194a477030896ebdbcfd4d8b545235610446bd
7
- data.tar.gz: 3d07fe63b6b3ba452e30dabc7f1663b91393d2f860cca3ccd8dea491894ee6dfbdb3a99aa3d9917edbb778e49e80b1314467b7837a1b8ba13d8119eab033e393
6
+ metadata.gz: 9887708fbfe36c554c269926b47ae194462556a6bd21fda6eae230c3ffcba7c00bb4ba393e8b5952eb6c13d6fb5fbbb09bea24305dbc32b284076b2f730b8339
7
+ data.tar.gz: 38a08aedc1772303617686bcb1912c7bdff959601d9ce018a3245e4e8152d42dfbf23e31db6547529cd600f69c4bde3a23fa11521b02641e10a7107e5dd6b1a9
data/Gemfile CHANGED
@@ -1,6 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  group :development do
4
+ gem 'pry'
4
5
  gem 'vagrant', github: 'mitchellh/vagrant'
5
6
  end
6
7
 
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
@@ -1,4 +1,4 @@
1
1
  Vagrant.configure(2) do |config|
2
- config.vm.box = 'trusty64'
2
+ config.vm.box = 'ubuntu/trusty64'
3
3
  config.vm.network :private_network, type: :dhcp
4
4
  end
@@ -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
- print build
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
- meta = groups.select{|group| group.include?(':children') }
19
- exp = groups.reject{|group| meta.include?(group) }
20
- imp = (nodes.keys - exp.values.flatten)
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
- imp.each{|gname| inv[gname] = nodes[gname].merge id: gname }
24
- exp.each{|gname, gnodes| inv[gname] = gnodes.map{|n| nodes[n].merge id: n }}
25
- meta.each{|gname, gnames| inv[gname] = gnames }
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
- inv
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 groups
35
- @groups ||= config.ansible.groups
68
+ def get_explicit_groups
69
+ groups.reject{|group| @meta_groups.include?(group) } || []
36
70
  end
37
71
 
38
- def nodes
39
- @nodes ||= with_target_vms{}.each_with_object({}) do |machine, hash|
40
- raise Vagrant::Errors::SSHNotReady unless machine.ssh_info
41
- hash[machine.name.to_s] = {
42
- ssh_user: 'vagrant',
43
- ssh_host: machine.provider.driver.read_guest_ip(1),
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
- def node_attrs(id, node)
51
- "#{node.delete(id)} #{node.map{|k| "ansible_#{k[0]}=#{k[1]}"}.join(' ')}"
80
+
81
+ def groups
82
+ @groups ||= config.ansible.groups
52
83
  end
53
84
 
54
- def print(inventory)
55
- out = "# Generated by vagrant-ansible_inventory plugin\n\n"
85
+ def ini
86
+ build
56
87
 
57
- inventory.each do |entries|
88
+ output = "# Generated by vagrant-ansible_inventory\n\n"
89
+
90
+ @inventory.each do |entries|
58
91
  group_name, children = *entries
59
- out << "[#{group_name}]\n"
92
+ output << "[#{group_name}]\n"
60
93
 
61
94
  if children.kind_of? Hash
62
- out << "#{node_attrs(:id, children)}\n"
95
+ output << "#{node_attributes(:id, children)}\n"
63
96
  else
64
97
  children.each do |child|
65
- out << "#{(child.kind_of?(Hash) ? node_attrs(:id, child) : child)}\n"
98
+ output << "#{(child.kind_of?(Hash) ? node_attributes(:id, child) : child)}\n"
66
99
  end
67
100
  end
68
101
 
69
- out << "\n"
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
- puts out
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
- 'display ansible inventory'
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 = "Usage: vagrant ansible <subcommand> [<args>]"
40
- o.separator ""
41
- o.separator "Available subcommands:"
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 }
@@ -1,5 +1,5 @@
1
1
  module VagrantPlugins
2
2
  module AnsibleInventory
3
- VERSION = '0.0.6'
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
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.6
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-02-27 00:00:00.000000000 Z
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.5
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.