Sha256: 401aea25b36772c674fcaf4abaa0fab70fca3ad10930adb28c41e197d3cce12f

Contents?: true

Size: 1.34 KB

Versions: 3

Compression:

Stored size: 1.34 KB

Contents

require 'securerandom'
module ForemanAnsible
  # Service to list an inventory to be passed to the ansible-playbook binary
  class InventoryCreator
    attr_reader :hosts

    def initialize(hosts)
      @hosts = hosts
    end

    # It returns a hash in a format that Ansible understands.
    # See http://docs.ansible.com/ansible/developing_inventory.html
    # for more details.
    # For now, we don't group the hosts based on different paramters
    # (use https://github.com/theforeman/foreman_ansible_inventory for
    # more advanced cases). Therefore we have only the 'all' group
    # with all hosts.
    def to_hash
      { 'all' => { 'hosts' => hosts.map(&:fqdn) },
        '_meta' => { 'hostvars' => host_vars } }
    end

    def host_vars
      hosts.reduce({}) do |hash, host|
        hash.update(host.fqdn =>
                    { 'foreman' => host_attributes(host),
                      'foreman_params' => host_params(host),
                      'foreman_ansible_roles' => host_roles(host) })
      end
    end

    def host_roles(host)
      host.all_ansible_roles.map(&:name)
    end

    def host_attributes(host)
      render_rabl(host, 'api/v2/hosts/main')
    end

    def host_params(host)
      host.host_params
    end

    private

    def render_rabl(host, template)
      Rabl.render(host, template, :format => 'hash')
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
foreman_ansible-1.2.1 app/services/foreman_ansible/inventory_creator.rb
foreman_ansible-1.2 app/services/foreman_ansible/inventory_creator.rb
foreman_ansible-1.1 app/services/foreman_ansible/inventory_creator.rb