Sha256: c5406f8f8bbb865964ea88488082b2d1c03323174e3f1d72657471880d67e3da

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

#!/usr/bin/env ruby

# Drive an Ansible inventory from a file manifest
# In order to use dynamically, still need to wrap
# with a shell script (Ansible won't pass arguments)

require 'trollop'
require 'json'
require 'logger'
require 'bjn_inventory'
require 'bjn_inventory/ansible'

parser = Trollop::Parser.new do
    version BjnInventory::VERSION
    banner <<-USAGE.gsub(/^\s{8}/,'')
        Usage:
          ansible-from [options]
    USAGE

    opt :ansible, 'Specify ansible groupings file', required: true, type: :string
    opt :manifest, 'Specify the manifest that defines this inventory', required: true, type: :string
    opt :debug, 'Enable debug output', :short => '-D'
    opt :syslog, 'Log to Syslog', :short => '-S'
    stop_on_unknown
end

opt = Trollop::with_standard_exception_handling parser do
    parser.parse(ARGV)
end

if opt[:syslog]
    require 'syslog/logger'
    logger = Syslog::Logger.new 'ansible-from'
else
    logger = Logger.new STDERR
end

if opt[:debug]
    logger.level = Logger::DEBUG
else
    logger.level = Logger::WARN
end

manifest = JSON.parse(File.read(opt[:manifest]))
ansible_spec = JSON.parse(File.read(opt[:ansible]))

inventory = BjnInventory::Inventory.new(manifest.merge({logger: logger}))
puts JSON.pretty_generate(inventory.by('name').to_ansible(ansible_spec))

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bjn_inventory-1.3.1 bin/ansible-from
bjn_inventory-1.3.0 bin/ansible-from