Specify inventory for Ansible job

automate

#1

Is it possible to specify the exact Ansible Tower inventory an Ansible job should use from ManageIQ, via the Ruby automate code?
So for example, I press a button on a VM and it launches a job, but the backing Ruby code specifies the inventory that is passed to the job.
I know you can set the limit and extra vars via Ruby, but can you set the inventory also?


#2

Hi

You can pass inventory through args like automation method do it for extra_vars:

https://github.com/ManageIQ/manageiq-content/blob/gaprindashvili-3/content/automate/ManageIQ/AutomationManagement/AnsibleTower/Operations/StateMachines/Job.class/__methods__/launch_ansible_job.rb#L115

#3

Thanks for that! Got it working by specifying the ID of the inventory.

args = {:extra_vars => extra_variables}
args[:limit] = target if target
args[:inventory] = 5

Any idea if it possible to specify the inventory by name, or query the ID associated with a name?
Could do so by calling the Tower API directly in the code, but I wonder if there is already a built-in way of doing it?


#4

@enosullivan

I don’t think there is a built-in method. Currently I am using (during postprovision of VMs) the following way:

INVENTORY_NAME = 'cloudforms'
# get cloudforms inventory
uri = "#{@uri_base}/api/v1/inventories/?name=#{INVENTORY_NAME}"
request = RestClient::Request.new(
    url: uri,
    method: :get,
    headers: @headers,
    verify_ssl: OpenSSL::SSL::VERIFY_NONE
)

begin
  rest_result = request.execute
rescue
end
inventories = JSON.parse(rest_result)
inventory_id = inventories['results'][0]['id'] unless inventories['count'].zero?

#5

Going to try querying Tower as you’ve done for now and see how that goes. Should be sufficient.
Thanks for the help!