Merge pull request #132 from sfromm/localconnection

Make use of LocalConnection explicit
This commit is contained in:
Michael DeHaan 2012-04-12 17:40:03 -07:00
commit a5df306aa3
8 changed files with 50 additions and 17 deletions

View file

@ -48,7 +48,7 @@ class Cli(object):
''' create an options parser for bin/ansible '''
parser = utils.base_parser(constants=C, runas_opts=True, async_opts=True,
output_opts=True, usage='%prog <host-pattern> [options]')
output_opts=True, connect_opts=True, usage='%prog <host-pattern> [options]')
parser.add_option('-a', '--args', dest='module_args',
help="module arguments", default=C.DEFAULT_MODULE_ARGS)
parser.add_option('-m', '--module-name', dest='module_name',
@ -86,7 +86,7 @@ class Cli(object):
remote_port=options.remote_port, forks=options.forks,
background=options.seconds, pattern=pattern,
callbacks=self.callbacks, sudo=options.sudo, verbose=True,
debug=options.debug
transport=options.connection, debug=options.debug
)
return (runner, runner.run())

View file

@ -2,12 +2,12 @@
.\" Title: ansible
.\" Author: [see the "AUTHOR" section]
.\" Generator: DocBook XSL Stylesheets v1.76.1 <http://docbook.sf.net/>
.\" Date: 04/03/2012
.\" Date: 04/10/2012
.\" Manual: System administration commands
.\" Source: Ansible 0.0.2
.\" Language: English
.\"
.TH "ANSIBLE" "1" "04/03/2012" "Ansible 0\&.0\&.2" "System administration commands"
.TH "ANSIBLE" "1" "04/10/2012" "Ansible 0\&.0\&.2" "System administration commands"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
@ -122,6 +122,14 @@ Use this remote
\fIUSERNAME\fR
instead of root\&.
.RE
.PP
\fB\-c\fR \fICONNECTION\fR, \fB\-\-connection=\fR\fICONNECTION\fR
.RS 4
Connection type to use\&. Possible options are
\fIparamiko\fR
and
\fIlocal\fR\&.
.RE
.SH "INVENTORY"
.sp
Ansible stores the hosts it can potentially operate on in an inventory file\&. The syntax is one host per line\&. Groups headers are allowed and are included on their own line, enclosed in square brackets\&.

View file

@ -96,6 +96,10 @@ Poll a background job every 'NUM' seconds. Requires *-B*.
Use this remote 'USERNAME' instead of root.
*-c* 'CONNECTION', *--connection=*'CONNECTION'::
Connection type to use. Possible options are 'paramiko' and 'local'.
INVENTORY
---------

View file

@ -47,7 +47,7 @@ class Connection(object):
def connect(self, host):
conn = None
if self.transport == 'local' or self._LOCALHOSTRE.search(host):
if self.transport == 'local' and self._LOCALHOSTRE.search(host):
conn = LocalConnection(self.runner, host)
elif self.transport == 'paramiko':
conn = ParamikoConnection(self.runner, host)

View file

@ -33,3 +33,5 @@ DEFAULT_POLL_INTERVAL = 15
DEFAULT_REMOTE_USER = 'root'
DEFAULT_REMOTE_PASS = None
DEFAULT_REMOTE_PORT = 22
DEFAULT_TRANSPORT = 'paramiko'
DEFAULT_TRANSPORT_OPTS = ['local', 'paramiko']

View file

@ -55,6 +55,7 @@ class PlayBook(object):
remote_user = C.DEFAULT_REMOTE_USER,
remote_pass = C.DEFAULT_REMOTE_PASS,
remote_port = C.DEFAULT_REMOTE_PORT,
transport = C.DEFAULT_TRANSPORT,
override_hosts = None,
extra_vars = None,
debug = False,
@ -73,6 +74,7 @@ class PlayBook(object):
self.remote_user = remote_user
self.remote_pass = remote_pass
self.remote_port = remote_port
self.transport = transport
self.debug = debug
self.verbose = verbose
self.callbacks = callbacks
@ -272,7 +274,7 @@ class PlayBook(object):
# *****************************************************
def _run_module(self, pattern, host_list, module, args, vars, remote_user,
async_seconds, async_poll_interval, only_if, sudo):
async_seconds, async_poll_interval, only_if, sudo, transport):
''' 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)]
@ -285,7 +287,8 @@ class PlayBook(object):
remote_port=self.remote_port, module_vars=vars,
setup_cache=SETUP_CACHE, basedir=self.basedir,
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,
transport=transport
)
if async_seconds == 0:
@ -296,7 +299,7 @@ class PlayBook(object):
# *****************************************************
def _run_task(self, pattern=None, host_list=None, task=None,
remote_user=None, handlers=None, conditional=False, sudo=False):
remote_user=None, handlers=None, conditional=False, sudo=False, transport=None):
''' run a single task in the playbook and recursively run any subtasks. '''
# load the module name and parameters from the task entry
@ -328,7 +331,7 @@ class PlayBook(object):
# run the task in parallel
results = self._run_module(pattern, host_list, module_name,
module_args, module_vars, remote_user, async_seconds,
async_poll_interval, only_if, sudo)
async_poll_interval, only_if, sudo, transport)
self.stats.compute(results)
@ -423,7 +426,7 @@ class PlayBook(object):
# *****************************************************
def _do_setup_step(self, pattern, vars, user, port, sudo, vars_files=None):
def _do_setup_step(self, pattern, vars, user, port, sudo, transport, vars_files=None):
''' push variables down to the systems and get variables+facts back up '''
# this enables conditional includes like $facter_os.yml and is only done
@ -447,6 +450,7 @@ class PlayBook(object):
remote_pass=self.remote_pass, remote_port=self.remote_port,
setup_cache=SETUP_CACHE,
callbacks=self.runner_callbacks, sudo=sudo, debug=self.debug,
transport=transport,
).run()
self.stats.compute(setup_results, setup=True)
@ -486,15 +490,16 @@ class PlayBook(object):
user = pg.get('user', self.remote_user)
port = pg.get('port', self.remote_port)
sudo = pg.get('sudo', False)
transport = pg.get('connection', self.transport)
self.callbacks.on_play_start(pattern)
# push any variables down to the system # and get facts/ohai/other data back up
self._do_setup_step(pattern, vars, user, port, sudo, None)
self._do_setup_step(pattern, vars, user, port, sudo, transport, None)
# now with that data, handle contentional variable file imports!
if len(vars_files) > 0:
self._do_setup_step(pattern, vars, user, port, sudo, vars_files)
self._do_setup_step(pattern, vars, user, port, sudo, transport, vars_files)
# run all the top level tasks, these get run on every node
for task in tasks:
@ -504,7 +509,8 @@ class PlayBook(object):
task=task,
handlers=handlers,
remote_user=user,
sudo=sudo
sudo=sudo,
transport=transport
)
# handlers only run on certain nodes, they are flagged by _flag_handlers
@ -523,7 +529,8 @@ class PlayBook(object):
host_list=triggered_by,
conditional=True,
remote_user=user,
sudo=sudo
sudo=sudo,
transport=transport
)
# end of execution for this particular pattern. Multiple patterns

View file

@ -22,6 +22,7 @@ import fnmatch
import multiprocessing
import signal
import os
import pwd
import Queue
import random
import traceback
@ -73,7 +74,7 @@ class Runner(object):
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_port=C.DEFAULT_REMOTE_PORT, background=0, basedir=None, setup_cache=None,
transport='paramiko', conditional='True', groups={}, callbacks=None, verbose=False,
transport=C.DEFAULT_TRANSPORT, conditional='True', groups={}, callbacks=None, verbose=False,
debug=False, sudo=False, extra_vars=None, module_vars=None):
if setup_cache is None:
@ -86,7 +87,9 @@ class Runner(object):
self.callbacks = callbacks
self.generated_jid = str(random.randint(0, 999999999999))
self.connector = ansible.connection.Connection(self, transport)
self.transport = transport
self.connector = ansible.connection.Connection(self, self.transport)
if type(host_list) == str:
self.host_list, self.groups = self.parse_hosts(host_list)
@ -113,6 +116,9 @@ class Runner(object):
self.basedir = basedir
self.sudo = sudo
euid = pwd.getpwuid(os.geteuid())[0]
if self.transport == 'local' and self.remote_user != euid:
raise Exception("User mismatch: expected %s, but is %s" % (self.remote_user, euid))
if type(self.module_args) != str and type(self.module_args) != dict:
raise Exception("module_args must be a string or dict: %s" % self.module_args)

View file

@ -279,7 +279,7 @@ class SortedOptParser(optparse.OptionParser):
self.option_list.sort(key=methodcaller('get_opt_string'))
return optparse.OptionParser.format_help(self, formatter=None)
def base_parser(constants=C, usage="", output_opts=False, runas_opts=False, async_opts=False):
def base_parser(constants=C, usage="", output_opts=False, runas_opts=False, async_opts=False, connect_opts=False):
''' create an options parser for any ansible script '''
parser = SortedOptParser(usage)
@ -310,6 +310,12 @@ def base_parser(constants=C, usage="", output_opts=False, runas_opts=False, asyn
parser.add_option('-u', '--user', default=constants.DEFAULT_REMOTE_USER,
dest='remote_user', help='connect as this user')
if connect_opts:
parser.add_option('-c', '--connection', dest='connection',
choices=C.DEFAULT_TRANSPORT_OPTS,
default=C.DEFAULT_TRANSPORT,
help="connection type to use")
if async_opts:
parser.add_option('-P', '--poll', default=constants.DEFAULT_POLL_INTERVAL, type='int',
dest='poll_interval', help='set the poll interval if using -B')