mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Merge pull request #99 from chjohnst/master
Remote port and error check for old paramiko
This commit is contained in:
commit
debd6fee68
6 changed files with 49 additions and 37 deletions
36
bin/ansible
36
bin/ansible
|
@ -49,30 +49,32 @@ class Cli(object):
|
||||||
''' create an options parser for bin/ansible '''
|
''' create an options parser for bin/ansible '''
|
||||||
|
|
||||||
parser = OptionParser(usage = 'ansible <host-pattern> [options]')
|
parser = OptionParser(usage = 'ansible <host-pattern> [options]')
|
||||||
parser.add_option("-a", "--args", dest="module_args",
|
parser.add_option('-a', '--args', dest='module_args',
|
||||||
help="module arguments", default=C.DEFAULT_MODULE_ARGS)
|
help="module arguments", default=C.DEFAULT_MODULE_ARGS)
|
||||||
parser.add_option("-B", "--background", dest="seconds", type='int', default=0,
|
parser.add_option('-B', '--background', dest='seconds', type='int', default=0,
|
||||||
help="run asynchronously, failing after X seconds")
|
help='run asynchronously, failing after X seconds')
|
||||||
parser.add_option('-f','--forks', dest='forks', default=C.DEFAULT_FORKS, type='int',
|
parser.add_option('-f','--forks', dest='forks', default=C.DEFAULT_FORKS, type='int',
|
||||||
help='number of parallel processes to use')
|
help='number of parallel processes to use')
|
||||||
parser.add_option("-i", "--inventory-file", dest="inventory",
|
parser.add_option('-i', '--inventory-file', dest='inventory',
|
||||||
help="inventory host file", default=C.DEFAULT_HOST_LIST)
|
help='inventory host file', default=C.DEFAULT_HOST_LIST)
|
||||||
parser.add_option("-k", "--ask-pass", default=False, action="store_true",
|
parser.add_option('-k', '--ask-pass', default=False, action='store_true',
|
||||||
help="ask for SSH password")
|
help='ask for SSH password')
|
||||||
parser.add_option("-M", "--module-path", dest="module_path",
|
parser.add_option('-M', '--module-path', dest='module_path',
|
||||||
help="path to module library", default=C.DEFAULT_MODULE_PATH)
|
help="path to module library", default=C.DEFAULT_MODULE_PATH)
|
||||||
parser.add_option("-m", "--module-name", dest="module_name",
|
parser.add_option('-m', '--module-name', dest='module_name',
|
||||||
help="module name to execute", default=C.DEFAULT_MODULE_NAME)
|
help="module name to execute", default=C.DEFAULT_MODULE_NAME)
|
||||||
parser.add_option('-o', '--one-line', dest='one_line', action='store_true',
|
parser.add_option('-o', '--one-line', dest='one_line', action='store_true',
|
||||||
help="condense output")
|
help='condense output')
|
||||||
parser.add_option('-P', '--poll', default=C.DEFAULT_POLL_INTERVAL, type='int',
|
parser.add_option('-P', '--poll', default=C.DEFAULT_POLL_INTERVAL, type='int',
|
||||||
dest='poll_interval', help="set the poll interval if using -B")
|
dest='poll_interval', help='set the poll interval if using -B')
|
||||||
parser.add_option('-t', '--tree', dest='tree', default=None,
|
parser.add_option('-t', '--tree', dest='tree', default=None,
|
||||||
help="log output to this directory")
|
help='log output to this directory')
|
||||||
parser.add_option('-T', '--timeout', default=C.DEFAULT_TIMEOUT, type='int',
|
parser.add_option('-T', '--timeout', default=C.DEFAULT_TIMEOUT, type='int',
|
||||||
dest='timeout', help="set the SSH timeout in seconds")
|
dest='timeout', help='set the SSH timeout in seconds')
|
||||||
parser.add_option('-u', '--user', default=C.DEFAULT_REMOTE_USER,
|
parser.add_option('-u', '--user', default=C.DEFAULT_REMOTE_USER,
|
||||||
dest='remote_user', help='connect as this user')
|
dest='remote_user', help='connect as this user')
|
||||||
|
parser.add_option('-p', '--port', default=C.DEFAULT_REMOTE_PORT, type='int',
|
||||||
|
dest='remote_port', help='set the remote ssh port')
|
||||||
options, args = parser.parse_args()
|
options, args = parser.parse_args()
|
||||||
self.callbacks.options = options
|
self.callbacks.options = options
|
||||||
|
|
||||||
|
@ -101,8 +103,9 @@ class Cli(object):
|
||||||
module_name=options.module_name, module_path=options.module_path,
|
module_name=options.module_name, module_path=options.module_path,
|
||||||
module_args=shlex.split(options.module_args),
|
module_args=shlex.split(options.module_args),
|
||||||
remote_user=options.remote_user, remote_pass=sshpass,
|
remote_user=options.remote_user, remote_pass=sshpass,
|
||||||
host_list=options.inventory, timeout=options.timeout,
|
host_list=options.inventory, timeout=options.timeout,
|
||||||
forks=options.forks, background=options.seconds, pattern=pattern,
|
remote_port=options.remote_port, forks=options.forks,
|
||||||
|
background=options.seconds, pattern=pattern,
|
||||||
callbacks=self.callbacks, verbose=True,
|
callbacks=self.callbacks, verbose=True,
|
||||||
)
|
)
|
||||||
return (runner, runner.run())
|
return (runner, runner.run())
|
||||||
|
@ -116,7 +119,8 @@ class Cli(object):
|
||||||
module_args=[ "jid=%s" % jid ], remote_user=old_runner.remote_user,
|
module_args=[ "jid=%s" % jid ], remote_user=old_runner.remote_user,
|
||||||
remote_pass=old_runner.remote_pass, host_list=hosts,
|
remote_pass=old_runner.remote_pass, host_list=hosts,
|
||||||
timeout=old_runner.timeout, forks=old_runner.forks,
|
timeout=old_runner.timeout, forks=old_runner.forks,
|
||||||
pattern='*', callbacks=self.silent_callbacks, verbose=True,
|
remote_port=old_runner.remote_port, pattern='*',
|
||||||
|
callbacks=self.silent_callbacks, verbose=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
# ----------------------------------------------
|
# ----------------------------------------------
|
||||||
|
|
|
@ -65,10 +65,14 @@ class ParamikoConnection(object):
|
||||||
allow_agent=True,
|
allow_agent=True,
|
||||||
look_for_keys=True,
|
look_for_keys=True,
|
||||||
password=self.runner.remote_pass,
|
password=self.runner.remote_pass,
|
||||||
timeout=self.runner.timeout
|
timeout=self.runner.timeout,
|
||||||
|
port=self.runner.remote_port
|
||||||
)
|
)
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
raise errors.AnsibleConnectionFailed(str(e))
|
if str(e).find("PID check failed") != -1:
|
||||||
|
raise errors.AnsibleError("paramiko version issue, please upgrade paramiko on the overlord")
|
||||||
|
else:
|
||||||
|
raise errors.AnsibleConnectionFailed(str(e))
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def exec_command(self, cmd):
|
def exec_command(self, cmd):
|
||||||
|
|
|
@ -32,5 +32,4 @@ DEFAULT_TIMEOUT = 10
|
||||||
DEFAULT_POLL_INTERVAL = 15
|
DEFAULT_POLL_INTERVAL = 15
|
||||||
DEFAULT_REMOTE_USER = 'root'
|
DEFAULT_REMOTE_USER = 'root'
|
||||||
DEFAULT_REMOTE_PASS = None
|
DEFAULT_REMOTE_PASS = None
|
||||||
|
DEFAULT_REMOTE_PORT = 22
|
||||||
|
|
||||||
|
|
|
@ -54,6 +54,7 @@ class PlayBook(object):
|
||||||
timeout = C.DEFAULT_TIMEOUT,
|
timeout = C.DEFAULT_TIMEOUT,
|
||||||
remote_user = C.DEFAULT_REMOTE_USER,
|
remote_user = C.DEFAULT_REMOTE_USER,
|
||||||
remote_pass = C.DEFAULT_REMOTE_PASS,
|
remote_pass = C.DEFAULT_REMOTE_PASS,
|
||||||
|
remote_port = C.DEFAULT_REMOTE_PORT,
|
||||||
override_hosts = None,
|
override_hosts = None,
|
||||||
verbose = False,
|
verbose = False,
|
||||||
callbacks = None,
|
callbacks = None,
|
||||||
|
@ -69,6 +70,7 @@ class PlayBook(object):
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
self.remote_user = remote_user
|
self.remote_user = remote_user
|
||||||
self.remote_pass = remote_pass
|
self.remote_pass = remote_pass
|
||||||
|
self.remote_port = remote_port
|
||||||
self.verbose = verbose
|
self.verbose = verbose
|
||||||
self.callbacks = callbacks
|
self.callbacks = callbacks
|
||||||
self.runner_callbacks = runner_callbacks
|
self.runner_callbacks = runner_callbacks
|
||||||
|
@ -252,8 +254,8 @@ class PlayBook(object):
|
||||||
|
|
||||||
# *****************************************************
|
# *****************************************************
|
||||||
|
|
||||||
def _run_module(self, pattern, host_list, module, args, remote_user,
|
def _run_module(self, pattern, host_list, module, args, remote_user,
|
||||||
async_seconds, async_poll_interval, only_if):
|
remote_port, async_seconds, async_poll_interval, only_if):
|
||||||
''' run a particular module step in a playbook '''
|
''' run a particular module step in a playbook '''
|
||||||
|
|
||||||
hosts = [ h for h in host_list if (h not in self.stats.failures) and (h not in self.stats.dark)]
|
hosts = [ h for h in host_list if (h not in self.stats.failures) and (h not in self.stats.dark)]
|
||||||
|
@ -262,7 +264,7 @@ class PlayBook(object):
|
||||||
pattern=pattern, groups=self.groups, module_name=module,
|
pattern=pattern, groups=self.groups, module_name=module,
|
||||||
module_args=args, host_list=hosts, forks=self.forks,
|
module_args=args, host_list=hosts, 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=remote_port,
|
||||||
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,
|
||||||
)
|
)
|
||||||
|
@ -275,7 +277,7 @@ class PlayBook(object):
|
||||||
# *****************************************************
|
# *****************************************************
|
||||||
|
|
||||||
def _run_task(self, pattern=None, host_list=None, task=None,
|
def _run_task(self, pattern=None, host_list=None, task=None,
|
||||||
remote_user=None, handlers=None, conditional=False):
|
remote_user=None, remote_port=None, handlers=None, conditional=False):
|
||||||
''' 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
|
||||||
|
@ -303,8 +305,7 @@ class PlayBook(object):
|
||||||
# load up an appropriate ansible runner to
|
# load up an appropriate ansible runner to
|
||||||
# run the task in parallel
|
# run the task in parallel
|
||||||
results = self._run_module(pattern, host_list, module_name,
|
results = self._run_module(pattern, host_list, module_name,
|
||||||
module_args, remote_user,
|
module_args, remote_user, remote_port, async_seconds, async_poll_interval, only_if)
|
||||||
async_seconds, async_poll_interval, only_if)
|
|
||||||
|
|
||||||
self.stats.compute(results)
|
self.stats.compute(results)
|
||||||
|
|
||||||
|
@ -399,7 +400,7 @@ class PlayBook(object):
|
||||||
|
|
||||||
# *****************************************************
|
# *****************************************************
|
||||||
|
|
||||||
def _do_setup_step(self, pattern, vars, user, vars_files=None):
|
def _do_setup_step(self, pattern, vars, user, port, vars_files=None):
|
||||||
''' push variables down to the systems and get variables+facts back up '''
|
''' push variables down to the systems and get variables+facts back up '''
|
||||||
|
|
||||||
# this enables conditional includes like $facter_os.yml and is only done
|
# this enables conditional includes like $facter_os.yml and is only done
|
||||||
|
@ -426,7 +427,7 @@ class PlayBook(object):
|
||||||
pattern=pattern, groups=self.groups, module_name='setup',
|
pattern=pattern, groups=self.groups, module_name='setup',
|
||||||
module_args=push_var_str, host_list=host_list,
|
module_args=push_var_str, host_list=host_list,
|
||||||
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_port=port,
|
||||||
remote_pass=self.remote_pass, setup_cache=SETUP_CACHE,
|
remote_pass=self.remote_pass, setup_cache=SETUP_CACHE,
|
||||||
callbacks=self.runner_callbacks,
|
callbacks=self.runner_callbacks,
|
||||||
).run()
|
).run()
|
||||||
|
@ -459,15 +460,16 @@ class PlayBook(object):
|
||||||
tasks = pg.get('tasks', [])
|
tasks = pg.get('tasks', [])
|
||||||
handlers = pg.get('handlers', [])
|
handlers = pg.get('handlers', [])
|
||||||
user = pg.get('user', C.DEFAULT_REMOTE_USER)
|
user = pg.get('user', C.DEFAULT_REMOTE_USER)
|
||||||
|
port = pg.get('port', C.DEFAULT_REMOTE_PORT)
|
||||||
|
|
||||||
self.callbacks.on_play_start(pattern)
|
self.callbacks.on_play_start(pattern)
|
||||||
|
|
||||||
# push any variables down to the system # and get facts/ohai/other data back up
|
# push any variables down to the system # and get facts/ohai/other data back up
|
||||||
self._do_setup_step(pattern, vars, user, None)
|
self._do_setup_step(pattern, vars, user, port, None)
|
||||||
|
|
||||||
# now with that data, handle contentional variable file imports!
|
# now with that data, handle contentional variable file imports!
|
||||||
if len(vars_files) > 0:
|
if len(vars_files) > 0:
|
||||||
self._do_setup_step(pattern, vars, user, vars_files)
|
self._do_setup_step(pattern, vars, user, port, vars_files)
|
||||||
|
|
||||||
# run all the top level tasks, these get run on every node
|
# run all the top level tasks, these get run on every node
|
||||||
for task in tasks:
|
for task in tasks:
|
||||||
|
@ -476,7 +478,8 @@ class PlayBook(object):
|
||||||
host_list=self.host_list,
|
host_list=self.host_list,
|
||||||
task=task,
|
task=task,
|
||||||
handlers=handlers,
|
handlers=handlers,
|
||||||
remote_user=user
|
remote_user=user,
|
||||||
|
remote_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
|
||||||
|
@ -494,7 +497,8 @@ class PlayBook(object):
|
||||||
handlers=[],
|
handlers=[],
|
||||||
host_list=triggered_by,
|
host_list=triggered_by,
|
||||||
conditional=True,
|
conditional=True,
|
||||||
remote_user=user
|
remote_user=user,
|
||||||
|
remote_port=port
|
||||||
)
|
)
|
||||||
|
|
||||||
# end of execution for this particular pattern. Multiple patterns
|
# end of execution for this particular pattern. Multiple patterns
|
||||||
|
|
|
@ -60,8 +60,8 @@ class Runner(object):
|
||||||
module_name=C.DEFAULT_MODULE_NAME, module_args=C.DEFAULT_MODULE_ARGS,
|
module_name=C.DEFAULT_MODULE_NAME, module_args=C.DEFAULT_MODULE_ARGS,
|
||||||
forks=C.DEFAULT_FORKS, timeout=C.DEFAULT_TIMEOUT, pattern=C.DEFAULT_PATTERN,
|
forks=C.DEFAULT_FORKS, timeout=C.DEFAULT_TIMEOUT, pattern=C.DEFAULT_PATTERN,
|
||||||
remote_user=C.DEFAULT_REMOTE_USER, remote_pass=C.DEFAULT_REMOTE_PASS,
|
remote_user=C.DEFAULT_REMOTE_USER, remote_pass=C.DEFAULT_REMOTE_PASS,
|
||||||
background=0, basedir=None, setup_cache=None, transport='paramiko',
|
remote_port=C.DEFAULT_REMOTE_PORT, background=0, basedir=None, setup_cache=None,
|
||||||
conditional='True', groups={}, callbacks=None, verbose=False):
|
transport='paramiko', conditional='True', groups={}, callbacks=None, verbose=False):
|
||||||
|
|
||||||
if setup_cache is None:
|
if setup_cache is None:
|
||||||
setup_cache = {}
|
setup_cache = {}
|
||||||
|
@ -92,6 +92,7 @@ class Runner(object):
|
||||||
self.verbose = verbose
|
self.verbose = verbose
|
||||||
self.remote_user = remote_user
|
self.remote_user = remote_user
|
||||||
self.remote_pass = remote_pass
|
self.remote_pass = remote_pass
|
||||||
|
self.remote_port = remote_port
|
||||||
self.background = background
|
self.background = background
|
||||||
self.basedir = basedir
|
self.basedir = basedir
|
||||||
|
|
||||||
|
|
|
@ -44,7 +44,8 @@ for x in items:
|
||||||
name = params['name']
|
name = params['name']
|
||||||
state = params.get('state','unknown')
|
state = params.get('state','unknown')
|
||||||
|
|
||||||
if state not in [ 'running', 'stopped', 'restarted' ]:
|
# running and started are the same
|
||||||
|
if state not in [ 'running', 'started', 'stopped', 'restarted' ]:
|
||||||
print "failed=True msg='invalid state'"
|
print "failed=True msg='invalid state'"
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
@ -82,10 +83,9 @@ def _run(cmd):
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.PIPE, shell=True)
|
stderr=subprocess.PIPE, shell=True)
|
||||||
|
|
||||||
|
|
||||||
rc = 0
|
rc = 0
|
||||||
if changed:
|
if changed:
|
||||||
if state == 'started':
|
if state in ('started', 'running'):
|
||||||
rc = _run("/sbin/service %s start" % name)
|
rc = _run("/sbin/service %s start" % name)
|
||||||
elif state == 'stopped':
|
elif state == 'stopped':
|
||||||
rc = _run("/sbin/service %s stop" % name)
|
rc = _run("/sbin/service %s stop" % name)
|
||||||
|
|
Loading…
Reference in a new issue