Sha256: 9deaa615e025674e6d56ef4cfa4f51318bcf45ef9dfca0903bce1dc2afd361f6

Contents?: true

Size: 1.4 KB

Versions: 19

Compression:

Stored size: 1.4 KB

Contents

require 'json'

module QB
  class AnsibleModule
    def self.stringify_keys hash
      hash.map {|k, v| [k.to_s, v]}.to_h
    end
    
    def initialize
      @changed = false
      @input_file = ARGV[0]
      @input = File.read @input_file
      @args = JSON.load @input
      @facts = {}
      
      # if QB_STDIO_ env vars are set send stdout and stderr
      # to those sockets to print in the parent process
      
      if ENV['QB_STDIO_OUT']
        $stdout = UNIXSocket.new ENV['QB_STDIO_OUT']
      end
      
      if ENV['QB_STDIO_ERR']
        $stderr = UNIXSocket.new ENV['QB_STDIO_ERR']
      end
    end
    
    def run
      result = main
      
      case result
      when nil
        # pass
      when Hash
        @facts.merge! result
      else
        raise "result of #main should be nil or Hash, found #{ result.inspect }"
      end
      
      done
    end
    
    def changed! facts = {}
      @changed = true
      @facts.merge! facts
      done
    end
    
    def done
      exit_json changed: @changed,
                ansible_facts: self.class.stringify_keys(@facts)
    end
    
    def exit_json hash
      # print JSON response to process' actual STDOUT (instead of $stdout,
      # which may be pointing to the qb parent process)
      STDOUT.print JSON.dump(self.class.stringify_keys(hash))
      exit 0
    end
    
    def fail msg
      exit_json failed: true, msg: msg
    end
  end
end # QB

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
qb-0.1.55 lib/qb/ansible_module.rb
qb-0.1.54 lib/qb/ansible_module.rb
qb-0.1.53 lib/qb/ansible_module.rb
qb-0.1.52 lib/qb/ansible_module.rb
qb-0.1.51 lib/qb/ansible_module.rb
qb-0.1.50 lib/qb/ansible_module.rb
qb-0.1.49 lib/qb/ansible_module.rb
qb-0.1.48 lib/qb/ansible_module.rb
qb-0.1.47 lib/qb/ansible_module.rb
qb-0.1.46 lib/qb/ansible_module.rb
qb-0.1.45 lib/qb/ansible_module.rb
qb-0.1.44 lib/qb/ansible_module.rb
qb-0.1.43 lib/qb/ansible_module.rb
qb-0.1.42 lib/qb/ansible_module.rb
qb-0.1.41 lib/qb/ansible_module.rb
qb-0.1.40 lib/qb/ansible_module.rb
qb-0.1.39 lib/qb/ansible_module.rb
qb-0.1.38 lib/qb/ansible_module.rb
qb-0.1.37 lib/qb/ansible_module.rb