#!/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 :groups, 'Specify ansible groupings file', required: false, type: :string opt :ansible, 'Synonym for --groups', required: false, 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 opt[:groups] = opt[:ansible] if opt[:groups].nil? 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 if opt[:groups].nil? logger.fatal "Need to specify --groups file" exit 1 end manifest = JSON.parse(File.read(opt[:manifest])) ansible_spec = JSON.parse(File.read(opt[:groups])) inventory = BjnInventory::Inventory.new(manifest.merge({logger: logger})) puts JSON.pretty_generate(inventory.by('name').to_ansible(ansible_spec))