Specify inventory for Ansible job



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?



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



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?



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

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


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