#!/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))