6.2 KiB
Inventory Refresh Overview
Tower should have an inventory view that is more aligned towards systems management rather than merely maintaining inventory for automation.
Inventory Source Promotion
Starting with Tower 3.2, InventorySource will be associated directly with an Inventory.
API Concerns / Deprecation
The group field for InventorySource has been renamed to deprecated_group and will be removed
from InventorySource completely in Tower 3.3. As a result the related field on Group ,
inventory_source has been renamed deprecated_inventory_source and will also be removed in Tower 3.3.
Fact Searching
Facts generated by an Ansible playbook during a Job Template run are stored by Tower into the database
whenever use_fact_cache=True is set per-Job-Template. New facts are merged with existing
facts and are per-host. These stored facts can be used to filter hosts via the
/api/v2/hosts endpoint, using the GET query parameter host_filter i.e.
/api/v2/hosts?host_filter=ansible_facts__ansible_processor_vcpus=8
The grammer of host_filter allows for:
- grouping via
() - the boolean
andoperator __to reference related fields in relational fields__is used onansible_factsto separate keys in a JSON key path[]is used to denote a json array in the path specification""can be used in the value when spaces are wanted in the value- "classic" Django queries may be embedded in the
host_filter
Examples:
/api/v2/hosts/?host_filter=name=localhost
/api/v2/hosts/?host_filter=ansible_facts__ansible_date_time__weekday_number="3"
/api/v2/hosts/?host_filter=ansible_facts__ansible_processor[]="GenuineIntel"
/api/v2/hosts/?host_filter=ansible_facts__ansible_lo__ipv6[]__scope="host"
/api/v2/hosts/?host_filter=ansible_facts__ansible_processor_vcpus=8
/api/v2/hosts/?host_filter=ansible_facts__ansible_env__PYTHONUNBUFFERED="true"
/api/v2/hosts/?host_filter=(name=localhost or name=database) and (groups__name=east or groups__name="west coast") and ansible_facts__ansible_processor_vcpus=8
Smart Inventory
Starting in Tower 3.2, Tower will support the ability to define a Smart Inventory. You will define the inventories using the same language we currently support in our Smart Search.
Inventory Changes
-
The
Inventorymodel has a new field calledkind. The default of this field will be blank for normal inventories and set tosmartfor smart inventories. -
Inventorymodel has a new field calledhost_filter. The default of this field will be blank for normal inventories. Whenhost_filteris set AND the inventorykindis set tosmartis the combination that makes a Smart Inventory. -
Hostmodel has a new field calledsmart_inventories. This field uses theSmartInventoryMembershipslookup table to provide a set of all of the Smart Inventory a host is a part of. The memberships are generated by theupdate_host_smart_inventory_membershipstask. This task is called when the view for/api/v2/hosts/:id/smart_inventoriesis materialized. NOTE: This task is only run if theAWX_REBUILD_SMART_MEMBERSHIPis set to True. It defaults to False.
Smart Filter (host_filter)
The SmartFilter class handles our translation of the smart search string. We store the
filter value in the host_filter field for an inventory. This value should be expressed
the same way we express our existing smart searches.
host_filter="search=foo"
host_filter="group__search=bar"
host_filter="search=baz and group__search=bang"
host_filter="name=localhost or group__name=local"
Creating a new Smart Inventory for all of our GCE and EC2 groups might look like this:
HTTP POST /api/v2/inventories/
{
"name": "GCE and EC2 Smart Inventory",
"kind": "smart",
"host_filter": "group__search=ec2 and group__search=gce"
...
}
More On Searching
The host_filter you set will search over the entirety of the hosts you have
access to in Tower. If you want to restrict your search in anyway, you will
want to declare that in your host filter.
For example, if you want to restrict the search to only hosts in an inventory
named "US-East", you would create a host_filter that looked something like this:
{
"name": "NYC Hosts",
"kind": "smart",
"host_filter": "inventory__name='US-East' and search='nyc'",
...
}
In the above example, you are limiting the search to the "US-East" inventory and hosts with a name containing "nyc".
Acceptance Critera
When verifying acceptance we should ensure the following statements are true:
Inventoryhas a new field namedkindthat defaults to empty and can only be set tosmart.Inventoryhas a new field namedhost_filterto empty and can only be set to a valid SmartFilter string.Inventorywith ahost_filterset and akindofsmartwill have ahostslist reflecting the results of searching/api/v2/hostswith the same search that is set in thehost_filter.
API Concerns
There are no breaking or backwards incompatible changes for this feature.
Other Changes
Inventory update all inventory_sources
A new endpoint /api/v2/inventories/:id/update_inventory_sources has been added. This endpoint
functions in the same way that /api/v2/inventory_source/:id/update functions for a single
InventorySource with the exception that it updates all of the inventory sources for the
Inventory.
HTTP GET /api/v2/inventories/:id/update_inventory_sources will list all of the inventory
sources and if they will be updated when a POST to the same endpoint is made. The result of
this request will look like this:
{
results: [
{"inventory_source": 1, "can_update": True},
{"inventory_source": 2, "can_update": False},
]
}
When making a POST to the same endpoint, the response will contain a status as well as the job ID for the update.
POST /api/v2/inventories/:id/update_inventory_sources
{
results: [
{"inventory_update": 20, "inventory_source": 1, "status": "started"},
{"inventory_update": 21, "inventory_source": 2, "status": "Could not start because `can_update` returned False"}
]
}