From 961bee00d5760b7d9a24b97c8d4804e46c142a79 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Tue, 18 Aug 2015 20:02:03 -0400 Subject: [PATCH] centralized the definition of 'localhost' --- lib/ansible/constants.py | 1 + lib/ansible/inventory/__init__.py | 13 ++++++------- lib/ansible/plugins/action/synchronize.py | 12 ++++++------ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/ansible/constants.py b/lib/ansible/constants.py index 8b78500c393..290274c13e8 100644 --- a/lib/ansible/constants.py +++ b/lib/ansible/constants.py @@ -245,3 +245,4 @@ VAULT_VERSION_MIN = 1.0 VAULT_VERSION_MAX = 1.0 MAX_FILE_SIZE_FOR_DIFF = 1*1024*1024 TREE_DIR = None +LOCALHOST = frozenset(['127.0.0.1', 'localhost', '::1']) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index 5a06f5275f7..d25409b0ae4 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -49,7 +49,6 @@ class Inventory(object): # 'parser', '_vars_per_host', '_vars_per_group', '_hosts_cache', '_groups_list', # '_pattern_cache', '_vault_password', '_vars_plugins', '_playbook_basedir'] - LOCALHOST_ALIASES = frozenset(('localhost', '127.0.0.1', '::1')) def __init__(self, loader, variable_manager, host_list=C.DEFAULT_HOST_LIST): # the host file file, or script path, or list of hosts @@ -370,7 +369,7 @@ class Inventory(object): for host in matching_hosts: __append_host_to_results(host) - if pattern in self.LOCALHOST_ALIASES and len(results) == 0: + if pattern in C.LOCALHOST and len(results) == 0: new_host = self._create_implicit_localhost(pattern) results.append(new_host) return results @@ -404,15 +403,15 @@ class Inventory(object): def get_host(self, hostname): if hostname not in self._hosts_cache: self._hosts_cache[hostname] = self._get_host(hostname) - if hostname in self.LOCALHOST_ALIASES: - for host in self.LOCALHOST_ALIASES.difference((hostname,)): + if hostname in C.LOCALHOST: + for host in C.LOCALHOST.difference((hostname,)): self._hosts_cache[host] = self._hosts_cache[hostname] return self._hosts_cache[hostname] def _get_host(self, hostname): - if hostname in self.LOCALHOST_ALIASES: + if hostname in C.LOCALHOST: for host in self.get_group('all').get_hosts(): - if host.name in self.LOCALHOST_ALIASES: + if host.name in C.LOCALHOST: return host return self._create_implicit_localhost(hostname) matching_host = None @@ -511,7 +510,7 @@ class Inventory(object): """ return a list of hostnames for a pattern """ result = [ h for h in self.get_hosts(pattern) ] - if len(result) == 0 and pattern in self.LOCALHOST_ALIASES: + if len(result) == 0 and pattern in C.LOCALHOST: result = [pattern] return result diff --git a/lib/ansible/plugins/action/synchronize.py b/lib/ansible/plugins/action/synchronize.py index 26deb9ecc45..79de84238d7 100644 --- a/lib/ansible/plugins/action/synchronize.py +++ b/lib/ansible/plugins/action/synchronize.py @@ -23,7 +23,7 @@ import os.path from ansible.plugins.action import ActionBase from ansible.plugins import connection_loader from ansible.utils.boolean import boolean -from ansible import constants +from ansible import constants as C class ActionModule(ActionBase): @@ -46,7 +46,7 @@ class ActionModule(ActionBase): def _process_origin(self, host, path, user): - if host not in ('127.0.0.1', 'localhost', '::1'): + if host not in C.LOCALHOST: if user: return '%s@%s:%s' % (user, host, path) else: @@ -58,7 +58,7 @@ class ActionModule(ActionBase): def _process_remote(self, host, path, user): transport = self._play_context.connection - if host not in ('127.0.0.1', 'localhost', '::1') or transport != "local": + if host not in C.LOCALHOST or transport != "local": if user: return '%s@%s:%s' % (user, host, path) else: @@ -115,11 +115,11 @@ class ActionModule(ActionBase): src_host = '127.0.0.1' dest_host = task_vars.get('ansible_ssh_host') or task_vars.get('inventory_hostname') - dest_is_local = dest_host in ('127.0.0.1', 'localhost', '::1') + dest_is_local = dest_host in C.LOCALHOST # CHECK FOR NON-DEFAULT SSH PORT if self._task.args.get('dest_port', None) is None: - inv_port = task_vars.get('ansible_ssh_port', None) or constants.DEFAULT_REMOTE_PORT + inv_port = task_vars.get('ansible_ssh_port', None) or C.DEFAULT_REMOTE_PORT if inv_port is not None: self._task.args['dest_port'] = inv_port @@ -218,7 +218,7 @@ class ActionModule(ActionBase): self._task.args['rsync_path'] = '"%s"' % rsync_path if use_ssh_args: - self._task.args['ssh_args'] = constants.ANSIBLE_SSH_ARGS + self._task.args['ssh_args'] = C.ANSIBLE_SSH_ARGS # run the module and store the result result = self._execute_module('synchronize', task_vars=task_vars)