1# This file is part of Ansible 2# 3# Ansible is free software: you can redistribute it and/or modify 4# it under the terms of the GNU General Public License as published by 5# the Free Software Foundation, either version 3 of the License, or 6# (at your option) any later version. 7# 8# Ansible is distributed in the hope that it will be useful, 9# but WITHOUT ANY WARRANTY; without even the implied warranty of 10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11# GNU General Public License for more details. 12# 13# You should have received a copy of the GNU General Public License 14# along with Ansible. If not, see <http://www.gnu.org/licenses/>. 15 16from __future__ import (absolute_import, division, print_function) 17__metaclass__ = type 18 19import json 20 21from ansible.module_utils.facts.namespace import PrefixFactNamespace 22 23from ansible.module_utils.facts.collector import BaseFactCollector 24 25 26class FacterFactCollector(BaseFactCollector): 27 name = 'facter' 28 _fact_ids = set(['facter']) 29 30 def __init__(self, collectors=None, namespace=None): 31 namespace = PrefixFactNamespace(namespace_name='facter', 32 prefix='facter_') 33 super(FacterFactCollector, self).__init__(collectors=collectors, 34 namespace=namespace) 35 36 def find_facter(self, module): 37 facter_path = module.get_bin_path('facter', opt_dirs=['/opt/puppetlabs/bin']) 38 cfacter_path = module.get_bin_path('cfacter', opt_dirs=['/opt/puppetlabs/bin']) 39 40 # Prefer to use cfacter if available 41 if cfacter_path is not None: 42 facter_path = cfacter_path 43 44 return facter_path 45 46 def run_facter(self, module, facter_path): 47 # if facter is installed, and we can use --json because 48 # ruby-json is ALSO installed, include facter data in the JSON 49 rc, out, err = module.run_command(facter_path + " --puppet --json") 50 return rc, out, err 51 52 def get_facter_output(self, module): 53 facter_path = self.find_facter(module) 54 if not facter_path: 55 return None 56 57 rc, out, err = self.run_facter(module, facter_path) 58 59 if rc != 0: 60 return None 61 62 return out 63 64 def collect(self, module=None, collected_facts=None): 65 # Note that this mirrors previous facter behavior, where there isnt 66 # a 'ansible_facter' key in the main fact dict, but instead, 'facter_whatever' 67 # items are added to the main dict. 68 facter_dict = {} 69 70 if not module: 71 return facter_dict 72 73 facter_output = self.get_facter_output(module) 74 75 # TODO: if we fail, should we add a empty facter key or nothing? 76 if facter_output is None: 77 return facter_dict 78 79 try: 80 facter_dict = json.loads(facter_output) 81 except Exception: 82 # FIXME: maybe raise a FactCollectorError with some info attrs? 83 pass 84 85 return facter_dict 86