knife-ansible-inventory 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c417432dc7fc1ec9b0e1e0c0b77cb5b5ba788dc9
4
+ data.tar.gz: 634949acbd4b7ff98e9bd7673380e829fe6ab5c7
5
+ SHA512:
6
+ metadata.gz: dfcfc7d072b947c5b6ebc8df3c0c596ec911cf8a75cde2aba661fbdc8625e2e6f73f54a5691a53bde77b472d59382ab8f7dbd88aa67f01bd55d664a4d330dd24
7
+ data.tar.gz: e54c414b9c5ae9c7c4c5c02d3de18abc80089b574ba7e1d83bcf01c0cc92701d75d6b1285aff2dd6c02f78859a25c6f630ddbeec48b409a4bd07bd233f319888
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.14.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in knife-ansible-inventory.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Austin Heiman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,46 @@
1
+ # knife-ansible-inventory
2
+
3
+ Custom [Chef knife plugin](https://docs.chef.io/plugin_knife_custom.html) that returns Chef nodes in a JSON format suitable for [a custom Ansible inventory](http://docs.ansible.com/ansible/dev_guide/developing_inventory.html).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'knife-ansible-inventory'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install knife-ansible-inventory
20
+
21
+ ## Usage
22
+
23
+ ```shell
24
+ $ knife ansible inventory
25
+ --group-by-attribute ATTRIBUTE
26
+ --host-attribute ATTRIBUTE
27
+ --hostvars RETURNED_KEY=ATTRIBUTE.PATH[,RETURNED_KEY=ATTRIBUTE.PATH]
28
+ --query QUERY
29
+ --help
30
+ ```
31
+
32
+ See the [`spec/`](/spec/) directory for examples of commands and their output JSON.
33
+
34
+ ## Development
35
+
36
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
37
+
38
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
39
+
40
+ ## Contributing
41
+
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/atheiman/knife-ansible-inventory.
43
+
44
+ ## License
45
+
46
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+ Gem::Specification.new do |spec|
3
+ spec.name = 'knife-ansible-inventory'
4
+ spec.version = '0.1.0'
5
+ spec.authors = ['Austin Heiman']
6
+ spec.email = ['atheimanksu@gmail.com']
7
+
8
+ spec.summary = 'Chef knife plugin for generating an Ansible inventory'
9
+ spec.homepage = 'https://github.com/atheiman/knife-ansible-inventory'
10
+ spec.license = 'MIT'
11
+
12
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
13
+ f.match(%r{^(test|spec|features)/})
14
+ end
15
+ spec.require_paths = ['lib']
16
+
17
+ spec.add_dependency 'chef', '~> 12.12'
18
+ spec.add_development_dependency 'bundler', '~> 1.14'
19
+ spec.add_development_dependency 'rake', '~> 10.0'
20
+ spec.add_development_dependency 'rspec', '~> 3.0'
21
+ end
@@ -0,0 +1,87 @@
1
+ class Chef::Knife::AnsibleInventory < Chef::Knife
2
+ banner 'knife ansible inventory'
3
+
4
+ deps do
5
+ require 'chef/search/query'
6
+ end
7
+
8
+ option :group_by_attribute,
9
+ long: '--group-by-attribute ATTRIBUTE',
10
+ description: "Attribute to group hosts by"
11
+
12
+ option :host_attribute,
13
+ long: '--host-attribute ATTRIBUTE',
14
+ description: "Attribute to identify hosts by",
15
+ default: 'ipaddress'
16
+
17
+ option :hostvars,
18
+ long: '--hostvars RETURNED_KEY=ATTRIBUTE.PATH[,RETURNED_KEY=ATTRIBUTE.PATH]',
19
+ description: "Comma-delimited mapping of returned hostvars to Chef attribute paths",
20
+ default: 'ipaddress=ipaddress'
21
+
22
+ option :query,
23
+ long: '--query QUERY',
24
+ description: 'Chef node search query'
25
+
26
+ def validate_options
27
+ missing = []
28
+ %i(group_by_attribute host_attribute hostvars query).each do |opt|
29
+ missing << opt unless config[opt]
30
+ end
31
+ raise ArgumentError, "Missing option(s): #{missing}" unless missing.empty?
32
+ end
33
+
34
+ def str_to_attr_path(str)
35
+ str.split('.')
36
+ end
37
+
38
+ # limit node query results to the group_by_attribute and the hostvars
39
+ def filter_result
40
+ filter_result = {}
41
+ config[:hostvars].split(',').each do |attr|
42
+ key, value = attr.split('=')
43
+ filter_result[key] = str_to_attr_path(value)
44
+ end
45
+ filter_result.merge(
46
+ config[:group_by_attribute] => str_to_attr_path(config[:group_by_attribute]),
47
+ config[:host_attribute] => str_to_attr_path(config[:host_attribute])
48
+ )
49
+ end
50
+
51
+ # get all the nodes matching the given query
52
+ def nodes_search
53
+ nodes = []
54
+ Chef::Search::Query.new.search(
55
+ :node,
56
+ config[:query],
57
+ filter_result: filter_result
58
+ ) { |n| nodes << n }
59
+ nodes
60
+ end
61
+
62
+ def run
63
+ validate_options
64
+
65
+ nodes = nodes_search
66
+
67
+ inventory = {}
68
+ until nodes.empty?
69
+ # TODO: currently this doesnt handle nodes that should exist in multiple groups
70
+ # organize nodes into groups based on matching group_by_attribute values
71
+ match_value = str_to_attr_path(config[:group_by_attribute]).inject(nodes.first, &:[])
72
+ group_name = match_value.join(',')
73
+ inventory[group_name] = { 'hosts' => [], 'vars' => {} }
74
+ filtered_nodes = nodes.select do |n|
75
+ n[config[:group_by_attribute]] == match_value
76
+ end
77
+ filtered_nodes.each do |n|
78
+ inventory[group_name]['hosts'] << n
79
+ nodes.delete(n)
80
+ end
81
+ # TODO: fill out group vars with
82
+ end
83
+
84
+ require 'pp'
85
+ pp inventory
86
+ end
87
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-ansible-inventory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Austin Heiman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: chef
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '12.12'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '12.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - atheimanksu@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - knife-ansible-inventory.gemspec
84
+ - lib/chef/knife/ansible_inventory.rb
85
+ homepage: https://github.com/atheiman/knife-ansible-inventory
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.6.9
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Chef knife plugin for generating an Ansible inventory
109
+ test_files: []