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 ''' ''' create an options parser for bin/ansible '''
parser = utils.base_parser(constants=C, runas_opts=True, async_opts=True, 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', 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('-m', '--module-name', dest='module_name', parser.add_option('-m', '--module-name', dest='module_name',
@ -86,7 +86,7 @@ class Cli(object):
remote_port=options.remote_port, forks=options.forks, remote_port=options.remote_port, forks=options.forks,
background=options.seconds, pattern=pattern, background=options.seconds, pattern=pattern,
callbacks=self.callbacks, sudo=options.sudo, verbose=True, callbacks=self.callbacks, sudo=options.sudo, verbose=True,
debug=options.debug transport=options.connection, debug=options.debug
) )
return (runner, runner.run()) return (runner, runner.run())

View file

@ -2,12 +2,12 @@
.\" Title: ansible .\" Title: ansible
.\" Author: [see the "AUTHOR" section] .\" Author: [see the "AUTHOR" section]
.\" Generator: DocBook XSL Stylesheets v1.76.1 <http://docbook.sf.net/> .\" Generator: DocBook XSL Stylesheets v1.76.1 <http://docbook.sf.net/>
.\" Date: 04/03/2012 .\" Date: 04/10/2012
.\" Manual: System administration commands .\" Manual: System administration commands
.\" Source: Ansible 0.0.2 .\" Source: Ansible 0.0.2
.\" Language: English .\" 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 .\" * Define some portability stuff
.\" ----------------------------------------------------------------- .\" -----------------------------------------------------------------
@ -122,6 +122,14 @@ Use this remote
\fIUSERNAME\fR \fIUSERNAME\fR
instead of root\&. instead of root\&.
.RE .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" .SH "INVENTORY"
.sp .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\&. 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. Use this remote 'USERNAME' instead of root.
*-c* 'CONNECTION', *--connection=*'CONNECTION'::
Connection type to use. Possible options are 'paramiko' and 'local'.
INVENTORY INVENTORY
--------- ---------

View file

@ -47,7 +47,7 @@ class Connection(object):
def connect(self, host): def connect(self, host):
conn = None 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) conn = LocalConnection(self.runner, host)
elif self.transport == 'paramiko': elif self.transport == 'paramiko':
conn = ParamikoConnection(self.runner, host) conn = ParamikoConnection(self.runner, host)

View file

@ -33,3 +33,5 @@ DEFAULT_POLL_INTERVAL = 15
DEFAULT_REMOTE_USER = 'root' DEFAULT_REMOTE_USER = 'root'
DEFAULT_REMOTE_PASS = None DEFAULT_REMOTE_PASS = None
DEFAULT_REMOTE_PORT = 22 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_user = C.DEFAULT_REMOTE_USER,
remote_pass = C.DEFAULT_REMOTE_PASS, remote_pass = C.DEFAULT_REMOTE_PASS,
remote_port = C.DEFAULT_REMOTE_PORT, remote_port = C.DEFAULT_REMOTE_PORT,
transport = C.DEFAULT_TRANSPORT,
override_hosts = None, override_hosts = None,
extra_vars = None, extra_vars = None,
debug = False, debug = False,
@ -73,6 +74,7 @@ class PlayBook(object):
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.remote_port = remote_port
self.transport = transport
self.debug = debug self.debug = debug
self.verbose = verbose self.verbose = verbose
self.callbacks = callbacks self.callbacks = callbacks
@ -272,7 +274,7 @@ class PlayBook(object):
# ***************************************************** # *****************************************************
def _run_module(self, pattern, host_list, module, args, vars, remote_user, 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 ''' ''' 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)]
@ -285,7 +287,8 @@ class PlayBook(object):
remote_port=self.remote_port, module_vars=vars, remote_port=self.remote_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,
transport=transport
) )
if async_seconds == 0: if async_seconds == 0:
@ -296,7 +299,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, 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. ''' ''' 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
@ -328,7 +331,7 @@ class PlayBook(object):
# 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, module_vars, remote_user, async_seconds, 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) 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 ''' ''' 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
@ -447,6 +450,7 @@ class PlayBook(object):
remote_pass=self.remote_pass, remote_port=self.remote_port, remote_pass=self.remote_pass, remote_port=self.remote_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,
).run() ).run()
self.stats.compute(setup_results, setup=True) self.stats.compute(setup_results, setup=True)
@ -486,15 +490,16 @@ class PlayBook(object):
user = pg.get('user', self.remote_user) user = pg.get('user', self.remote_user)
port = pg.get('port', self.remote_port) port = pg.get('port', self.remote_port)
sudo = pg.get('sudo', False) sudo = pg.get('sudo', False)
transport = pg.get('connection', self.transport)
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, port, sudo, None) self._do_setup_step(pattern, vars, user, port, sudo, transport, 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, 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 # run all the top level tasks, these get run on every node
for task in tasks: for task in tasks:
@ -504,7 +509,8 @@ class PlayBook(object):
task=task, task=task,
handlers=handlers, handlers=handlers,
remote_user=user, remote_user=user,
sudo=sudo sudo=sudo,
transport=transport
) )
# handlers only run on certain nodes, they are flagged by _flag_handlers # handlers only run on certain nodes, they are flagged by _flag_handlers
@ -523,7 +529,8 @@ class PlayBook(object):
host_list=triggered_by, host_list=triggered_by,
conditional=True, conditional=True,
remote_user=user, remote_user=user,
sudo=sudo sudo=sudo,
transport=transport
) )
# end of execution for this particular pattern. Multiple patterns # end of execution for this particular pattern. Multiple patterns

View file

@ -22,6 +22,7 @@ import fnmatch
import multiprocessing import multiprocessing
import signal import signal
import os import os
import pwd
import Queue import Queue
import random import random
import traceback import traceback
@ -73,7 +74,7 @@ class Runner(object):
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,
remote_port=C.DEFAULT_REMOTE_PORT, background=0, basedir=None, setup_cache=None, 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): debug=False, sudo=False, extra_vars=None, module_vars=None):
if setup_cache is None: if setup_cache is None:
@ -86,7 +87,9 @@ class Runner(object):
self.callbacks = callbacks self.callbacks = callbacks
self.generated_jid = str(random.randint(0, 999999999999)) 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: if type(host_list) == str:
self.host_list, self.groups = self.parse_hosts(host_list) self.host_list, self.groups = self.parse_hosts(host_list)
@ -113,6 +116,9 @@ class Runner(object):
self.basedir = basedir self.basedir = basedir
self.sudo = sudo 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: 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) 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')) self.option_list.sort(key=methodcaller('get_opt_string'))
return optparse.OptionParser.format_help(self, formatter=None) 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 ''' ''' create an options parser for any ansible script '''
parser = SortedOptParser(usage) 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, parser.add_option('-u', '--user', default=constants.DEFAULT_REMOTE_USER,
dest='remote_user', help='connect as this 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: if async_opts:
parser.add_option('-P', '--poll', default=constants.DEFAULT_POLL_INTERVAL, type='int', parser.add_option('-P', '--poll', default=constants.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')