Skip to content

Commit

Permalink
Support the include_host_filters (#1347)
Browse files Browse the repository at this point in the history
* support the include_host_filters

* Update plugins/inventory/azure_rm.py

Co-authored-by: Fred-sun <37327967+Fred-sun@users.noreply.github.com>

* Update plugins/inventory/azure_rm.py

Co-authored-by: Fred-sun <37327967+Fred-sun@users.noreply.github.com>

* Update plugins/inventory/azure_rm.py

Co-authored-by: Fred-sun <37327967+Fred-sun@users.noreply.github.com>

* Update plugins/inventory/azure_rm.py

Co-authored-by: Fred-sun <37327967+Fred-sun@users.noreply.github.com>

* fix the format errors

---------

Co-authored-by: Fred-sun <37327967+Fred-sun@users.noreply.github.com>
  • Loading branch information
xu001186 and Fred-sun authored Dec 6, 2023
1 parent 5dec17d commit a867b33
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
5 changes: 5 additions & 0 deletions plugins/doc_fragments/azure_rm.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class ModuleDocFragment(object):
expression in the list is evaluated for each host; when the expression is true, the host is excluded
from the inventory.
default: []
include_host_filters:
description: Include hosts from the inventory with a list of Jinja2 conditional expressions. Each
expression in the list is evaluated for each host; when the expression is true, the host is included
in the inventory, all hosts are includes in the inventory by default.
default: [true]
batch_fetch:
description: To improve performance, results are fetched using an unsupported batch API. Disabling
C(batch_fetch) uses a much slower serial fetch, resulting in many more round-trips. Generally only
Expand Down
24 changes: 21 additions & 3 deletions plugins/inventory/azure_rm.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@
- tags['tagkey2'] is defined and tags['tagkey2'] == 'tagkey2'
# excludes hosts that are powered off
- powerstate != 'running'
# includes a host to the inventory when any of these expressions is true, can refer to any vars defined on the host
include_host_filters:
# includes hosts that in the eastus region and power on
- location in ['eastus'] and powerstate == 'running'
# includes hosts in the eastus region and power on OR includes hosts in the eastus2 region and tagkey is tagkey
- location in ['eastus'] and powerstate == 'running'
- location in ['eastus2'] and tags['tagkey'] is defined and tags['tagkey'] == 'tagkey'
'''

# FUTURE: do we need a set of sane default filters, separate from the user-defineable ones?
Expand Down Expand Up @@ -217,6 +225,8 @@ def parse(self, inventory, loader, path, cache=True):

self._filters = self.get_option('exclude_host_filters') + self.get_option('default_host_filters')

self._include_filters = self.get_option('include_host_filters')

try:
self._credential_setup()
self._get_hosts()
Expand Down Expand Up @@ -299,7 +309,9 @@ def _get_hosts(self):
for h in self._hosts:
# FUTURE: track hostnames to warn if a hostname is repeated (can happen for legacy and for composed inventory_hostname)
inventory_hostname = self._get_hostname(h, hostnames=constructable_hostnames, strict=constructable_config_strict)
if self._filter_host(inventory_hostname, h.hostvars):
if self._filter_exclude_host(inventory_hostname, h.hostvars):
continue
if not self._filter_include_host(inventory_hostname, h.hostvars):
continue
self.inventory.add_host(inventory_hostname)
# FUTURE: configurable default IP list? can already do this via hostvar_expressions
Expand All @@ -315,10 +327,10 @@ def _get_hosts(self):
self._add_host_to_keyed_groups(constructable_config_keyed_groups, h.hostvars, inventory_hostname, strict=constructable_config_strict)

# FUTURE: fix underlying inventory stuff to allow us to quickly access known groupvars from reconciled host
def _filter_host(self, inventory_hostname, hostvars):
def _filter_host(self, filter, inventory_hostname, hostvars):
self.templar.available_variables = hostvars

for condition in self._filters:
for condition in filter:
# FUTURE: should warn/fail if conditional doesn't return True or False
conditional = "{{% if {0} %}} True {{% else %}} False {{% endif %}}".format(condition)
try:
Expand All @@ -331,6 +343,12 @@ def _filter_host(self, inventory_hostname, hostvars):

return False

def _filter_include_host(self, inventory_hostname, hostvars):
return self._filter_host(self._include_filters, inventory_hostname, hostvars)

def _filter_exclude_host(self, inventory_hostname, hostvars):
return self._filter_host(self._filters, inventory_hostname, hostvars)

def _get_hostname(self, host, hostnames=None, strict=False):
hostname = None
errors = []
Expand Down

0 comments on commit a867b33

Please sign in to comment.