Fixes to make ports DWIM.

pull/154/merge
Michael DeHaan 13 years ago
parent 957867e088
commit 08468dcb0c

@ -9,6 +9,7 @@
- hosts: all - hosts: all
user: root user: root
port: 3000
# could have also have done: # could have also have done:
# user: mdehaan # user: mdehaan

@ -45,12 +45,12 @@ class Connection(object):
self.runner = runner self.runner = runner
self.transport = transport self.transport = transport
def connect(self, host): def connect(self, host, port=None):
conn = None conn = None
if self.transport == 'local' and self._LOCALHOSTRE.search(host): if self.transport == 'local' and self._LOCALHOSTRE.search(host):
conn = LocalConnection(self.runner, host) conn = LocalConnection(self.runner, host, None)
elif self.transport == 'paramiko': elif self.transport == 'paramiko':
conn = ParamikoConnection(self.runner, host) conn = ParamikoConnection(self.runner, host, port)
if conn is None: if conn is None:
raise Exception("unsupported connection type") raise Exception("unsupported connection type")
return conn.connect() return conn.connect()
@ -64,10 +64,13 @@ class Connection(object):
class ParamikoConnection(object): class ParamikoConnection(object):
''' SSH based connections with Paramiko ''' ''' SSH based connections with Paramiko '''
def __init__(self, runner, host): def __init__(self, runner, host, port=None):
self.ssh = None self.ssh = None
self.runner = runner self.runner = runner
self.host = host self.host = host
self.port = port
if port is None:
self.port = self.runner.remote_port
def _get_conn(self): def _get_conn(self):
ssh = paramiko.SSHClient() ssh = paramiko.SSHClient()
@ -75,9 +78,13 @@ class ParamikoConnection(object):
try: try:
ssh.connect( ssh.connect(
self.host, username=self.runner.remote_user, self.host,
allow_agent=True, look_for_keys=True, password=self.runner.remote_pass, username=self.runner.remote_user,
timeout=self.runner.timeout, port=self.runner.remote_port allow_agent=True,
look_for_keys=True,
password=self.runner.remote_pass,
timeout=self.runner.timeout,
port=self.port
) )
except Exception, e: except Exception, e:
if str(e).find("PID check failed") != -1: if str(e).find("PID check failed") != -1:
@ -183,7 +190,7 @@ class LocalConnection(object):
self.runner = runner self.runner = runner
self.host = host self.host = host
def connect(self): def connect(self, port=None):
''' connect to the local host; nothing to do here ''' ''' connect to the local host; nothing to do here '''
return self return self

@ -304,18 +304,21 @@ class PlayBook(object):
# ***************************************************** # *****************************************************
def _run_module(self, pattern, module, args, vars, remote_user, def _run_module(self, pattern, module, args, vars, remote_user,
async_seconds, async_poll_interval, only_if, sudo, transport): async_seconds, async_poll_interval, only_if, sudo, transport, port):
''' run a particular module step in a playbook ''' ''' run a particular module step in a playbook '''
hosts = [ h for h in self.inventory.list_hosts() if (h not in self.stats.failures) and (h not in self.stats.dark)] hosts = [ h for h in self.inventory.list_hosts() if (h not in self.stats.failures) and (h not in self.stats.dark)]
self.inventory.restrict_to(hosts) self.inventory.restrict_to(hosts)
if port is None:
port=self.remote_port
runner = ansible.runner.Runner( runner = ansible.runner.Runner(
pattern=pattern, inventory=self.inventory, module_name=module, pattern=pattern, inventory=self.inventory, module_name=module,
module_args=args, forks=self.forks, module_args=args, forks=self.forks,
remote_pass=self.remote_pass, module_path=self.module_path, remote_pass=self.remote_pass, module_path=self.module_path,
timeout=self.timeout, remote_user=remote_user, timeout=self.timeout, remote_user=remote_user,
remote_port=self.remote_port, module_vars=vars, remote_port=port, module_vars=vars,
setup_cache=SETUP_CACHE, basedir=self.basedir, setup_cache=SETUP_CACHE, basedir=self.basedir,
conditional=only_if, callbacks=self.runner_callbacks, conditional=only_if, callbacks=self.runner_callbacks,
extra_vars=self.extra_vars, debug=self.debug, sudo=sudo, extra_vars=self.extra_vars, debug=self.debug, sudo=sudo,
@ -333,7 +336,7 @@ class PlayBook(object):
# ***************************************************** # *****************************************************
def _run_task(self, pattern=None, task=None, def _run_task(self, pattern=None, task=None,
remote_user=None, handlers=None, conditional=False, sudo=False, transport=None): remote_user=None, handlers=None, conditional=False, sudo=False, transport=None, port=None):
''' run a single task in the playbook and recursively run any subtasks. ''' ''' run a single task in the playbook and recursively run any subtasks. '''
# load the module name and parameters from the task entry # load the module name and parameters from the task entry
@ -365,7 +368,7 @@ class PlayBook(object):
# run the task in parallel # run the task in parallel
results = self._run_module(pattern, module_name, results = self._run_module(pattern, module_name,
module_args, module_vars, remote_user, async_seconds, module_args, module_vars, remote_user, async_seconds,
async_poll_interval, only_if, sudo, transport) async_poll_interval, only_if, sudo, transport, port)
self.stats.compute(results) self.stats.compute(results)
@ -483,7 +486,7 @@ class PlayBook(object):
module_args=vars, inventory=self.inventory, module_args=vars, inventory=self.inventory,
forks=self.forks, module_path=self.module_path, forks=self.forks, module_path=self.module_path,
timeout=self.timeout, remote_user=user, timeout=self.timeout, remote_user=user,
remote_pass=self.remote_pass, remote_port=self.remote_port, remote_pass=self.remote_pass, remote_port=port,
setup_cache=SETUP_CACHE, setup_cache=SETUP_CACHE,
callbacks=self.runner_callbacks, sudo=sudo, debug=self.debug, callbacks=self.runner_callbacks, sudo=sudo, debug=self.debug,
transport=transport, sudo_pass=self.sudo_pass, is_playbook=True transport=transport, sudo_pass=self.sudo_pass, is_playbook=True
@ -546,7 +549,8 @@ class PlayBook(object):
handlers=handlers, handlers=handlers,
remote_user=user, remote_user=user,
sudo=sudo, sudo=sudo,
transport=transport transport=transport,
port=port
) )
# handlers only run on certain nodes, they are flagged by _flag_handlers # handlers only run on certain nodes, they are flagged by _flag_handlers
@ -566,7 +570,8 @@ class PlayBook(object):
conditional=True, conditional=True,
remote_user=user, remote_user=user,
sudo=sudo, sudo=sudo,
transport=transport transport=transport,
port=port
) )
self.inventory.lift_restriction() self.inventory.lift_restriction()

@ -68,16 +68,32 @@ def _executor_hook(job_queue, result_queue):
class Runner(object): class Runner(object):
def __init__(self, host_list=C.DEFAULT_HOST_LIST, module_path=C.DEFAULT_MODULE_PATH, def __init__(self,
module_name=C.DEFAULT_MODULE_NAME, module_args=C.DEFAULT_MODULE_ARGS, host_list=C.DEFAULT_HOST_LIST,
forks=C.DEFAULT_FORKS, timeout=C.DEFAULT_TIMEOUT, pattern=C.DEFAULT_PATTERN, module_path=C.DEFAULT_MODULE_PATH,
remote_user=C.DEFAULT_REMOTE_USER, remote_pass=C.DEFAULT_REMOTE_PASS, module_name=C.DEFAULT_MODULE_NAME,
sudo_pass=C.DEFAULT_SUDO_PASS, remote_port=C.DEFAULT_REMOTE_PORT, background=0, module_args=C.DEFAULT_MODULE_ARGS,
basedir=None, setup_cache=None, transport=C.DEFAULT_TRANSPORT, forks=C.DEFAULT_FORKS,
conditional='True', groups={}, callbacks=None, verbose=False, timeout=C.DEFAULT_TIMEOUT,
debug=False, sudo=False, extra_vars=None, pattern=C.DEFAULT_PATTERN,
module_vars=None, is_playbook=False, inventory=None): remote_user=C.DEFAULT_REMOTE_USER,
remote_pass=C.DEFAULT_REMOTE_PASS,
remote_port=C.DEFAULT_REMOTE_PORT,
sudo_pass=C.DEFAULT_SUDO_PASS,
background=0,
basedir=None,
setup_cache=None,
transport=C.DEFAULT_TRANSPORT,
conditional='True',
groups={},
callbacks=None,
verbose=False,
debug=False,
sudo=False,
extra_vars=None,
module_vars=None,
is_playbook=False,
inventory=None):
if setup_cache is None: if setup_cache is None:
setup_cache = {} setup_cache = {}
if basedir is None: if basedir is None:
@ -132,25 +148,16 @@ class Runner(object):
@classmethod @classmethod
def parse_hosts(cls, host_list, override_hosts=None, extra_vars=None): def parse_hosts(cls, host_list, override_hosts=None, extra_vars=None):
''' parse the host inventory file, returns (hosts, groups) ''' ''' parse the host inventory file, returns (hosts, groups) '''
if override_hosts is None: if override_hosts is None:
inventory = ansible.inventory.Inventory(host_list, extra_vars) inventory = ansible.inventory.Inventory(host_list, extra_vars)
else: else:
inventory = ansible.inventory.Inventory(override_hosts) inventory = ansible.inventory.Inventory(override_hosts, extra_vars)
return inventory.host_list, inventory.groups return inventory.host_list, inventory.groups
# ***************************************************** # *****************************************************
def _connect(self, host):
''' connects to a host, returns (is_successful, connection_object OR traceback_string) '''
try:
return [ True, self.connector.connect(host) ]
except errors.AnsibleConnectionFailed, e:
return [ False, "FAILED: %s" % str(e) ]
# *****************************************************
def _return_from_module(self, conn, host, result, err, executed=None): def _return_from_module(self, conn, host, result, err, executed=None):
''' helper function to handle JSON parsing of results ''' ''' helper function to handle JSON parsing of results '''
@ -510,10 +517,15 @@ class Runner(object):
def _executor_internal(self, host): def _executor_internal(self, host):
''' callback executed in parallel for each host. returns (hostname, connected_ok, extra) ''' ''' callback executed in parallel for each host. returns (hostname, connected_ok, extra) '''
ok, conn = self._connect(host) host_variables = self.inventory.get_variables(host, self.extra_vars)
if not ok: port = host_variables.get('ansible_ssh_port', self.remote_port)
return [ host, False, conn , None]
conn = None
try:
conn = self.connector.connect(host, port)
except errors.AnsibleConnectionFailed, e:
return [ host, False, "FAILED: %s" % str(e), None ]
cache = self.setup_cache.get(host, {}) cache = self.setup_cache.get(host, {})
module_name = utils.template(self.module_name, cache) module_name = utils.template(self.module_name, cache)

Loading…
Cancel
Save