Skip to content

Filters

Jake Callahan edited this page Apr 10, 2023 · 2 revisions

Filters

Note: Broker's filters will be radically changing in Broker 0.3

Inventory Filters

Broker's inventory filters are based on what is stored in its local inventory file. Therefore, only properties in that file are filterable. Nested properties are annotated with a . notation. For example, a top-level property hostname can be accessed by itself. However, a nested property of _broker_args called version would be accessed by _broker_args.version.

Results Filters

This type of filter is used against the output of some of Broker's commands where a list of results is passed in. The filter will be matched against each item in the list and only those matching the filter will be returned.

(0.2.0) Filter Syntax

Filters take the form "(property)(condition)(value)". Filters have several possible conditions:

  • < means "in" or that the filter value exists within the actual value
  • = means "equals"
  • { means "starts with"
  • } means "ends with"

Furthermore, putting a ! before the condition inverts the filter. So != means "not equals" and !< means "not in".

You can also chain multiple filters together by separating them with a comma. These are additive AND filters where each filter condition must match.

Example filters

Inventory Filters

--filter 'hostname<test' The string test should exist somewhere in the hostname value

--filter '_broker_args.template{deploy-sat' The template should start with the string "deploy-sat"

--filter 'name<test,_broker_args.provider!=RHV' The host's name should have "test" in it and the provider should not equal "RHV".

Results Filters

--results-filter 'res<test' Each result in the list must contain the string "test"

'--results-filter 'res}example'` Each result in the list must end with the string "example"

(0.3.0) Filter Syntax

Filters are any valid python expression that acts upon a filterable object. A filterable object is a list typically noted with either @inv or @res. Broker will replace that notation, in the filter expression, with a valid list whose contents are context-dependent.

Example Filters

Inventory Filters

--filter '"test" in @inv.hosname' The string "test" exists somewhere in the hostname value

--filter '@inv[-1]' Use the last host in the inventory

--filter '@inv[3:7]' Pick the 3rd through 6th hosts in the inventory

--filter '@inv._broker_args.template.startswith("deploy-sat")' The template should start with the string "deploy-sat"

You can also chain multiple filters together, feeding the results of the previous filter into the next, using the pipe | symbol. This allows you to perform multiple layers of filtering in a single expression.

--filter '"test" in @inv.name | @inv._broker_args.provider != "RHEV"' The host's name should have test in it and the provider should not equal RHEV.

Results Filters

--results-filter '"test" in @res' The string "test" exists in the results item

--results-filter '@res[-1]' Return just the last item from the results list

Note: Due to shell expansion, it is recommended to wrap a filter in single quotes.