mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 13:52:54 +00:00
Add a way to restrict gathered facts in Ansible:
- Using gather_subset options - By ignoring ohai/chef or facter/puppet facts
This commit is contained in:
committed by
Toshio Kuratomi
parent
2984ffdfac
commit
88772b6003
@@ -156,6 +156,9 @@ DEFAULT_PRIVATE_ROLE_VARS = get_config(p, DEFAULTS, 'private_role_vars', 'ANSIBL
|
||||
DEFAULT_JINJA2_EXTENSIONS = get_config(p, DEFAULTS, 'jinja2_extensions', 'ANSIBLE_JINJA2_EXTENSIONS', None)
|
||||
DEFAULT_EXECUTABLE = get_config(p, DEFAULTS, 'executable', 'ANSIBLE_EXECUTABLE', '/bin/sh')
|
||||
DEFAULT_GATHERING = get_config(p, DEFAULTS, 'gathering', 'ANSIBLE_GATHERING', 'implicit').lower()
|
||||
DEFAULT_GATHER_SUBSET = get_config(p, DEFAULTS, 'gather_subset', 'ANSIBLE_GATHER_SUBSET', 'all').lower()
|
||||
DEFAULT_IGNORE_OHAI = get_config(p, DEFAULTS, 'ignore_ohai', 'ANSIBLE_IGNORE_OHAI', False, boolean=True)
|
||||
DEFAULT_IGNORE_FACTER = get_config(p, DEFAULTS, 'ignore_facter', 'ANSIBLE_IGNORE_FACTER', False, boolean=True)
|
||||
DEFAULT_LOG_PATH = get_config(p, DEFAULTS, 'log_path', 'ANSIBLE_LOG_PATH', '', ispath=True)
|
||||
DEFAULT_FORCE_HANDLERS = get_config(p, DEFAULTS, 'force_handlers', 'ANSIBLE_FORCE_HANDLERS', False, boolean=True)
|
||||
DEFAULT_INVENTORY_IGNORE = get_config(p, DEFAULTS, 'inventory_ignore_extensions', 'ANSIBLE_INVENTORY_IGNORE', ["~", ".orig", ".bak", ".ini", ".cfg", ".retry", ".pyc", ".pyo"], islist=True)
|
||||
|
||||
@@ -151,11 +151,30 @@ class PlayIterator:
|
||||
self._play = play
|
||||
self._blocks = []
|
||||
|
||||
# Default options to gather
|
||||
gather_subset = C.DEFAULT_GATHER_SUBSET
|
||||
ignore_ohai = C.DEFAULT_IGNORE_OHAI
|
||||
ignore_facter = C.DEFAULT_IGNORE_FACTER
|
||||
|
||||
# Retrieve subset to gather
|
||||
if self._play.gather_subset is not None:
|
||||
gather_subset = self._play.gather_subset
|
||||
# ignore ohai
|
||||
if self._play.ignore_ohai is not None:
|
||||
ignore_ohai = self._play.ignore_ohai
|
||||
# ignore puppet facter
|
||||
if self._play.ignore_facter is not None:
|
||||
ignore_facter = self._play.ignore_facter
|
||||
|
||||
setup_block = Block(play=self._play)
|
||||
setup_task = Task(block=setup_block)
|
||||
setup_task.action = 'setup'
|
||||
setup_task.tags = ['always']
|
||||
setup_task.args = {}
|
||||
setup_task.args = {
|
||||
'gather_subset': gather_subset,
|
||||
'ignore_ohai' : ignore_ohai,
|
||||
'ignore_facter': ignore_facter,
|
||||
}
|
||||
setup_task.set_loader(self._play._loader)
|
||||
setup_block.block = [setup_task]
|
||||
|
||||
|
||||
@@ -159,6 +159,9 @@ class Facts(object):
|
||||
{ 'path' : '/usr/local/sbin/pkg', 'name' : 'pkgng' },
|
||||
]
|
||||
|
||||
# Allowed fact subset for gather_subset options
|
||||
ALLOWED_FACT_SUBSET = frozenset([ 'all', 'min', 'network', 'hardware', 'virtual' ])
|
||||
|
||||
def __init__(self, load_on_init=True):
|
||||
|
||||
self.facts = {}
|
||||
@@ -3067,15 +3070,33 @@ def get_file_lines(path):
|
||||
return ret
|
||||
|
||||
def ansible_facts(module):
|
||||
# Retrieve module parameters
|
||||
gather_subset = [ 'all' ]
|
||||
if 'gather_subset' in module.params:
|
||||
gather_subset = module.params['gather_subset']
|
||||
|
||||
# Retrieve all facts elements
|
||||
if 'all' in gather_subset:
|
||||
gather_subset = [ 'min', 'hardware', 'network', 'virtual' ]
|
||||
|
||||
# Check subsets and forbid unallowed name
|
||||
for subset in gather_subset:
|
||||
if subset not in Facts.ALLOWED_FACT_SUBSET:
|
||||
raise TypeError("Bad subset '%s' given to Ansible. gather_subset options allowed: %s" % (subset, ", ".join(Facts.ALLOWED_FACT_SUBSET)))
|
||||
|
||||
facts = {}
|
||||
facts.update(Facts().populate())
|
||||
facts.update(Hardware().populate())
|
||||
facts.update(Network(module).populate())
|
||||
facts.update(Virtual().populate())
|
||||
if 'hardware' in gather_subset:
|
||||
facts.update(Hardware().populate())
|
||||
if 'network' in gather_subset:
|
||||
facts.update(Network(module).populate())
|
||||
if 'virtual' in gather_subset:
|
||||
facts.update(Virtual().populate())
|
||||
facts['gather_subset'] = gather_subset
|
||||
return facts
|
||||
|
||||
# ===========================================
|
||||
|
||||
# TODO: remove this dead code?
|
||||
def get_all_facts(module):
|
||||
|
||||
setup_options = dict(module_setup=True)
|
||||
|
||||
@@ -64,6 +64,9 @@ class Play(Base, Taggable, Become):
|
||||
|
||||
# Connection
|
||||
_gather_facts = FieldAttribute(isa='bool', default=None, always_post_validate=True)
|
||||
_gather_subset = FieldAttribute(isa='string', default=None, always_post_validate=True)
|
||||
_ignore_facter = FieldAttribute(isa='bool', default=None, always_post_validate=True)
|
||||
_ignore_ohai = FieldAttribute(isa='bool', default=None, always_post_validate=True)
|
||||
_hosts = FieldAttribute(isa='list', required=True, listof=string_types, always_post_validate=True)
|
||||
_name = FieldAttribute(isa='string', default='', always_post_validate=True)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user