Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the include_host_filters #1347

Merged
merged 6 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 24 additions & 3 deletions plugins/inventory/azure_rm.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@
- 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
xu001186 marked this conversation as resolved.
Show resolved Hide resolved
- 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'

xu001186 marked this conversation as resolved.
Show resolved Hide resolved

xu001186 marked this conversation as resolved.
Show resolved Hide resolved
'''

# FUTURE: do we need a set of sane default filters, separate from the user-defineable ones?
Expand Down Expand Up @@ -215,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 @@ -297,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 @@ -313,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 @@ -329,6 +343,13 @@ 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