From 4dfa40f18e8c4b815a43593baa9bc98d0d61a09a Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Sat, 15 Mar 2014 16:19:28 -0400 Subject: [PATCH] added gathering control to ansible, defaults to 'smart' --- examples/ansible.cfg | 5 +++++ lib/ansible/constants.py | 1 + lib/ansible/playbook/__init__.py | 10 +++++++--- lib/ansible/playbook/play.py | 2 +- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/examples/ansible.cfg b/examples/ansible.cfg index 7a46caf8c0c..1a592d3e355 100644 --- a/examples/ansible.cfg +++ b/examples/ansible.cfg @@ -24,6 +24,11 @@ transport = smart remote_port = 22 module_lang = C +# controls implicit fact gathering (always, never or smart). +# smart gathers only if not currently in memory. +# does NOT affect explicit 'gather_facts' entries. +gathering = smart + # additional paths to search for roles in, colon separated #roles_path = /etc/ansible/roles diff --git a/lib/ansible/constants.py b/lib/ansible/constants.py index f9cd208c4ad..ade825d17f2 100644 --- a/lib/ansible/constants.py +++ b/lib/ansible/constants.py @@ -134,6 +134,7 @@ DEFAULT_SU = get_config(p, DEFAULTS, 'su', 'ANSIBLE_SU', False, boolean=True) DEFAULT_SU_FLAGS = get_config(p, DEFAULTS, 'su_flags', 'ANSIBLE_SU_FLAGS', '') DEFAULT_SU_USER = get_config(p, DEFAULTS, 'su_user', 'ANSIBLE_SU_USER', 'root') DEFAULT_ASK_SU_PASS = get_config(p, DEFAULTS, 'ask_su_pass', 'ANSIBLE_ASK_SU_PASS', False, boolean=True) +DEFAULT_GATHERING = get_config(p, DEFAULTS, 'gathering', 'ANSIBLE_GATHERING', 'smart').lower() DEFAULT_ACTION_PLUGIN_PATH = get_config(p, DEFAULTS, 'action_plugins', 'ANSIBLE_ACTION_PLUGINS', '/usr/share/ansible_plugins/action_plugins') DEFAULT_CALLBACK_PLUGIN_PATH = get_config(p, DEFAULTS, 'callback_plugins', 'ANSIBLE_CALLBACK_PLUGINS', '/usr/share/ansible_plugins/callback_plugins') diff --git a/lib/ansible/playbook/__init__.py b/lib/ansible/playbook/__init__.py index 918b9341717..88e054c3c8f 100644 --- a/lib/ansible/playbook/__init__.py +++ b/lib/ansible/playbook/__init__.py @@ -479,11 +479,15 @@ class PlayBook(object): def _do_setup_step(self, play): ''' get facts from the remote system ''' - if play.gather_facts is False: - return {} - host_list = self._trim_unavailable_hosts(play._play_hosts) + if play.gather_facts is None and C.DEFAULT_GATHERING == 'smart': + host_list = [h for h in host_list if h not in self.SETUP_CACHE or 'module_setup' not in self.SETUP_CACHE[h]] + if len(host_list) == 0: + return {} + elif play.gather_facts is False or (play.gather_facts is None and C.DEFAULT_GATHERING == 'never'): + return {} + self.callbacks.on_setup() self.inventory.restrict_to(host_list) diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py index 9195c5f2b66..e3e5fefcfda 100644 --- a/lib/ansible/playbook/play.py +++ b/lib/ansible/playbook/play.py @@ -117,7 +117,7 @@ class Play(object): self.sudo = ds.get('sudo', self.playbook.sudo) self.sudo_user = ds.get('sudo_user', self.playbook.sudo_user) self.transport = ds.get('connection', self.playbook.transport) - self.gather_facts = ds.get('gather_facts', True) + self.gather_facts = ds.get('gather_facts', None) self.remote_port = self.remote_port self.any_errors_fatal = utils.boolean(ds.get('any_errors_fatal', 'false')) self.accelerate = utils.boolean(ds.get('accelerate', 'false'))