Reinstate --extra-vars, which can do things in playbooks like:

ansible-playbook release-my-app.yml --extra-vars="version=123"

And make $version available in the playbook without re-editing the file
pull/266/head
Michael DeHaan 13 years ago
parent a0ac936a55
commit b9982fc17b

@ -35,6 +35,8 @@ def main(args):
parser = utils.base_parser(constants=C, usage=usage, connect_opts=True, runas_opts=True) parser = utils.base_parser(constants=C, usage=usage, connect_opts=True, runas_opts=True)
parser.add_option('-O', '--override-hosts', dest="override_hosts", default=None, parser.add_option('-O', '--override-hosts', dest="override_hosts", default=None,
help="run playbook against these hosts regardless of inventory settings") help="run playbook against these hosts regardless of inventory settings")
parser.add_option('-e', '--extra-vars', dest="extra_vars", default=None,
help="set additional key=value variables from the CLI")
options, args = parser.parse_args(args) options, args = parser.parse_args(args)
@ -51,6 +53,7 @@ def main(args):
override_hosts = None override_hosts = None
if options.override_hosts: if options.override_hosts:
override_hosts = options.override_hosts.split(",") override_hosts = options.override_hosts.split(",")
extra_vars = utils.parse_kv(options.extra_vars)
# run all playbooks specified on the command line # run all playbooks specified on the command line
for playbook in args: for playbook in args:
@ -74,7 +77,8 @@ def main(args):
timeout=options.timeout, timeout=options.timeout,
transport=options.connection, transport=options.connection,
sudo=options.sudo, sudo=options.sudo,
sudo_pass=sudopass sudo_pass=sudopass,
extra_vars=extra_vars
) )
try: try:

@ -1,22 +1,13 @@
'\" t '\" t
.\" Title: ansible-playbook .\" Title: ansible-playbook
.\" 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.75.2 <http://docbook.sf.net/>
.\" Date: 04/17/2012 .\" Date: 04/26/2012
.\" Manual: System administration commands .\" Manual: System administration commands
.\" Source: Ansible 0.0.2 .\" Source: Ansible 0.4
.\" Language: English .\" Language: English
.\" .\"
.TH "ANSIBLE\-PLAYBOOK" "1" "04/17/2012" "Ansible 0\&.0\&.2" "System administration commands" .TH "ANSIBLE\-PLAYBOOK" "1" "04/26/2012" "Ansible 0\&.4" "System administration commands"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\" ----------------------------------------------------------------- .\" -----------------------------------------------------------------
.\" * set default formatting .\" * set default formatting
.\" ----------------------------------------------------------------- .\" -----------------------------------------------------------------
@ -63,6 +54,11 @@ to load modules from\&. The default is
\fI/usr/share/ansible\fR\&. \fI/usr/share/ansible\fR\&.
.RE .RE
.PP .PP
\fB\-e\fR \fIVARS\fR, \fB\-\-extra\-vars=\fR\fIVARS\fR
.RS 4
Extra variables to inject into a playbook, in key=value key=value format\&.
.RE
.PP
\fB\-f\fR \fINUM\fR, \fB\-\-forks=\fR\fINUM\fR \fB\-f\fR \fINUM\fR, \fB\-\-forks=\fR\fINUM\fR
.RS 4 .RS 4
Level of parallelism\&. Level of parallelism\&.

@ -48,6 +48,9 @@ The 'PATH' to the inventory hosts file, which defaults to '/etc/ansible/hosts'.
The 'DIRECTORY' to load modules from. The default is '/usr/share/ansible'. The 'DIRECTORY' to load modules from. The default is '/usr/share/ansible'.
*-e* 'VARS', *--extra-vars=*'VARS'::
Extra variables to inject into a playbook, in key=value key=value format.
*-f* 'NUM', *--forks=*'NUM':: *-f* 'NUM', *--forks=*'NUM'::

@ -1,22 +1,13 @@
'\" t '\" t
.\" 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.75.2 <http://docbook.sf.net/>
.\" Date: 04/17/2012 .\" Date: 04/26/2012
.\" Manual: System administration commands .\" Manual: System administration commands
.\" Source: Ansible 0.0.2 .\" Source: Ansible 0.4
.\" Language: English .\" Language: English
.\" .\"
.TH "ANSIBLE" "1" "04/17/2012" "Ansible 0\&.0\&.2" "System administration commands" .TH "ANSIBLE" "1" "04/26/2012" "Ansible 0\&.4" "System administration commands"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\" ----------------------------------------------------------------- .\" -----------------------------------------------------------------
.\" * set default formatting .\" * set default formatting
.\" ----------------------------------------------------------------- .\" -----------------------------------------------------------------
@ -34,7 +25,7 @@ ansible \- run a command somewhere else
ansible <host\-pattern> [\-f forks] [\-m module_name] [\-a args] ansible <host\-pattern> [\-f forks] [\-m module_name] [\-a args]
.SH "DESCRIPTION" .SH "DESCRIPTION"
.sp .sp
\fBAnsible\fR is an extra\-simple tool/framework/API for doing \*(Aqremote things\*(Aq over SSH\&. \fBAnsible\fR is an extra\-simple tool/framework/API for doing \'remote things\' over SSH\&.
.SH "ARGUMENTS" .SH "ARGUMENTS"
.PP .PP
\fBhost\-pattern\fR \fBhost\-pattern\fR
@ -72,7 +63,7 @@ to load modules from\&. The default is
\fI/usr/share/ansible\fR\&. \fI/usr/share/ansible\fR\&.
.RE .RE
.PP .PP
\fB\-a\fR \*(Aq\fIARGUMENTS\fR\*(Aq, \fB\-\-args=\fR\*(Aq\fIARGUMENTS\fR\*(Aq \fB\-a\fR \'\fIARGUMENTS\fR\', \fB\-\-args=\fR\'\fIARGUMENTS\fR\'
.RS 4 .RS 4
The The
\fIARGUMENTS\fR \fIARGUMENTS\fR

@ -62,7 +62,8 @@ class PlayBook(object):
callbacks = None, callbacks = None,
runner_callbacks = None, runner_callbacks = None,
stats = None, stats = None,
sudo = False): sudo = False,
extra_vars = None):
""" """
playbook: path to a playbook file playbook: path to a playbook file
@ -85,6 +86,9 @@ class PlayBook(object):
if playbook is None or callbacks is None or runner_callbacks is None or stats is None: if playbook is None or callbacks is None or runner_callbacks is None or stats is None:
raise Exception('missing required arguments') raise Exception('missing required arguments')
if extra_vars is None:
extra_vars = {}
self.module_path = module_path self.module_path = module_path
self.forks = forks self.forks = forks
self.timeout = timeout self.timeout = timeout
@ -99,6 +103,7 @@ class PlayBook(object):
self.stats = stats self.stats = stats
self.sudo = sudo self.sudo = sudo
self.sudo_pass = sudo_pass self.sudo_pass = sudo_pass
self.extra_vars = extra_vars
self.basedir = os.path.dirname(playbook) self.basedir = os.path.dirname(playbook)
self.playbook = self._parse_playbook(playbook) self.playbook = self._parse_playbook(playbook)
@ -114,29 +119,32 @@ class PlayBook(object):
def _get_vars(self, play, dirname): def _get_vars(self, play, dirname):
''' load the vars section from a play ''' ''' load the vars section from a play '''
if play.get('vars') is None: if play.get('vars') is None:
play['vars'] = {} play['vars'] = {}
vars = play['vars'] if type(play['vars']) not in [dict, list]:
if type(vars) not in [dict, list]:
raise errors.AnsibleError("'vars' section must contain only key/value pairs") raise errors.AnsibleError("'vars' section must contain only key/value pairs")
vars = {}
# translate a list of vars into a dict # translate a list of vars into a dict
if type(vars) == list: if type(play['vars']) == list:
varlist = vars for item in play['vars']:
vars = {}
for item in varlist:
k, v = item.items()[0] k, v = item.items()[0]
vars[k] = v vars[k] = v
play['vars'] = vars else:
vars = play['vars']
vars_prompt = play.get('vars_prompt', {}) vars_prompt = play.get('vars_prompt', {})
if type(vars_prompt) != dict: if type(vars_prompt) != dict:
raise errors.AnsibleError("'vars_prompt' section must contain only key/value pairs") raise errors.AnsibleError("'vars_prompt' section must contain only key/value pairs")
for vname in vars_prompt: for vname in vars_prompt:
# TODO: make this prompt one line and consider double entry
print vars_prompt[vname] print vars_prompt[vname]
# FIXME - need some way to know that this prompt should be getpass or raw_input
vars[vname] = self.callbacks.on_vars_prompt(vname) vars[vname] = self.callbacks.on_vars_prompt(vname)
return vars
results = self.extra_vars.copy()
results.update(vars)
return results
# ***************************************************** # *****************************************************

@ -141,7 +141,7 @@ class TestPlaybook(unittest.TestCase):
runner_callbacks = self.test_callbacks runner_callbacks = self.test_callbacks
) )
result = self.playbook.run() result = self.playbook.run()
print utils.bigjson(dict(events=EVENTS)) # print utils.bigjson(dict(events=EVENTS))
return result return result
def test_one(self): def test_one(self):
@ -166,5 +166,6 @@ class TestPlaybook(unittest.TestCase):
# make sure the template module took options from the vars section # make sure the template module took options from the vars section
data = file('/tmp/ansible_test_data_template.out').read() data = file('/tmp/ansible_test_data_template.out').read()
print data
assert data.find("ears") != -1, "template success" assert data.find("ears") != -1, "template success"

Loading…
Cancel
Save