Merge pull request #3247 from ansible/facts-subset-and-cleanup
Facts subset and cleanup
This commit is contained in:
commit
a8841e6834
1 changed files with 26 additions and 55 deletions
|
@ -24,6 +24,19 @@ module: setup
|
|||
version_added: historical
|
||||
short_description: Gathers facts about remote hosts
|
||||
options:
|
||||
gather_subset:
|
||||
version_added: "2.1"
|
||||
description:
|
||||
- "if supplied, restrict the additional facts collected to the given subset.
|
||||
Possible values: all, hardware, network, virtual, ohai, and
|
||||
facter Can specify a list of values to specify a larger subset.
|
||||
Values can also be used with an initial C(!) to specify that
|
||||
that specific subset should not be collected. For instance:
|
||||
!hardware, !network, !virtual, !ohai, !facter. Note that a few
|
||||
facts are always collected. Use the filter parameter if you do
|
||||
not want to display those."
|
||||
required: false
|
||||
default: 'all'
|
||||
filter:
|
||||
version_added: "1.1"
|
||||
description:
|
||||
|
@ -80,77 +93,35 @@ ansible all -m setup -a 'filter=facter_*'
|
|||
# Display only facts about certain interfaces.
|
||||
ansible all -m setup -a 'filter=ansible_eth[0-2]'
|
||||
|
||||
# Restrict additional gathered facts to network and virtual.
|
||||
ansible all -m setup -a 'gather_subset=network,virtual'
|
||||
|
||||
# Do not call puppet facter or ohai even if present.
|
||||
ansible all -m setup -a 'gather_subset=!facter,!ohai'
|
||||
|
||||
# Only collect the minimum amount of facts:
|
||||
ansible all -m setup -a 'gather_subset=!all'
|
||||
|
||||
# Display facts from Windows hosts with custom facts stored in C(C:\\custom_facts).
|
||||
ansible windows -m setup -a "fact_path='c:\\custom_facts'"
|
||||
"""
|
||||
|
||||
|
||||
def run_setup(module):
|
||||
|
||||
setup_options = dict(module_setup=True)
|
||||
facts = ansible_facts(module)
|
||||
|
||||
for (k, v) in facts.items():
|
||||
setup_options["ansible_%s" % k.replace('-', '_')] = v
|
||||
|
||||
# Look for the path to the facter and ohai binary and set
|
||||
# the variable to that path.
|
||||
facter_path = module.get_bin_path('facter', opt_dirs=['/opt/puppetlabs/bin'])
|
||||
ohai_path = module.get_bin_path('ohai')
|
||||
|
||||
# if facter is installed, and we can use --json because
|
||||
# ruby-json is ALSO installed, include facter data in the JSON
|
||||
if facter_path is not None:
|
||||
rc, out, err = module.run_command(facter_path + " --puppet --json")
|
||||
facter = True
|
||||
try:
|
||||
facter_ds = json.loads(out)
|
||||
except:
|
||||
facter = False
|
||||
if facter:
|
||||
for (k,v) in facter_ds.items():
|
||||
setup_options["facter_%s" % k] = v
|
||||
|
||||
# ditto for ohai
|
||||
if ohai_path is not None:
|
||||
rc, out, err = module.run_command(ohai_path)
|
||||
ohai = True
|
||||
try:
|
||||
ohai_ds = json.loads(out)
|
||||
except:
|
||||
ohai = False
|
||||
if ohai:
|
||||
for (k,v) in ohai_ds.items():
|
||||
k2 = "ohai_%s" % k.replace('-', '_')
|
||||
setup_options[k2] = v
|
||||
|
||||
setup_result = { 'ansible_facts': {} }
|
||||
|
||||
for (k,v) in setup_options.items():
|
||||
if module.params['filter'] == '*' or fnmatch.fnmatch(k, module.params['filter']):
|
||||
setup_result['ansible_facts'][k] = v
|
||||
|
||||
# hack to keep --verbose from showing all the setup module results
|
||||
setup_result['_ansible_verbose_override'] = True
|
||||
|
||||
return setup_result
|
||||
|
||||
def main():
|
||||
global module
|
||||
module = AnsibleModule(
|
||||
argument_spec = dict(
|
||||
gather_subset=dict(default=["all"], required=False, type='list'),
|
||||
filter=dict(default="*", required=False),
|
||||
fact_path=dict(default='/etc/ansible/facts.d', required=False),
|
||||
),
|
||||
supports_check_mode = True,
|
||||
)
|
||||
data = run_setup(module)
|
||||
data = get_all_facts(module)
|
||||
module.exit_json(**data)
|
||||
|
||||
# import module snippets
|
||||
|
||||
from ansible.module_utils.basic import *
|
||||
|
||||
from ansible.module_utils.facts import *
|
||||
|
||||
main()
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
Loading…
Reference in a new issue