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
This commit is contained in:
Michael DeHaan 2012-04-26 19:56:10 -04:00
parent a0ac936a55
commit b9982fc17b
6 changed files with 44 additions and 41 deletions

View file

@ -35,6 +35,8 @@ def main(args):
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,
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)
@ -51,6 +53,7 @@ def main(args):
override_hosts = None
if options.override_hosts:
override_hosts = options.override_hosts.split(",")
extra_vars = utils.parse_kv(options.extra_vars)
# run all playbooks specified on the command line
for playbook in args:
@ -74,7 +77,8 @@ def main(args):
timeout=options.timeout,
transport=options.connection,
sudo=options.sudo,
sudo_pass=sudopass
sudo_pass=sudopass,
extra_vars=extra_vars
)
try:

View file

@ -1,22 +1,13 @@
'\" t
.\" Title: ansible-playbook
.\" Author: [see the "AUTHOR" section]
.\" Generator: DocBook XSL Stylesheets v1.76.1 <http://docbook.sf.net/>
.\" Date: 04/17/2012
.\" Generator: DocBook XSL Stylesheets v1.75.2 <http://docbook.sf.net/>
.\" Date: 04/26/2012
.\" Manual: System administration commands
.\" Source: Ansible 0.0.2
.\" Source: Ansible 0.4
.\" Language: English
.\"
.TH "ANSIBLE\-PLAYBOOK" "1" "04/17/2012" "Ansible 0\&.0\&.2" "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 '
.TH "ANSIBLE\-PLAYBOOK" "1" "04/26/2012" "Ansible 0\&.4" "System administration commands"
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
@ -63,6 +54,11 @@ to load modules from\&. The default is
\fI/usr/share/ansible\fR\&.
.RE
.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
.RS 4
Level of parallelism\&.

View file

@ -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'.
*-e* 'VARS', *--extra-vars=*'VARS'::
Extra variables to inject into a playbook, in key=value key=value format.
*-f* 'NUM', *--forks=*'NUM'::

View file

@ -1,22 +1,13 @@
'\" t
.\" Title: ansible
.\" Author: [see the "AUTHOR" section]
.\" Generator: DocBook XSL Stylesheets v1.76.1 <http://docbook.sf.net/>
.\" Date: 04/17/2012
.\" Generator: DocBook XSL Stylesheets v1.75.2 <http://docbook.sf.net/>
.\" Date: 04/26/2012
.\" Manual: System administration commands
.\" Source: Ansible 0.0.2
.\" Source: Ansible 0.4
.\" Language: English
.\"
.TH "ANSIBLE" "1" "04/17/2012" "Ansible 0\&.0\&.2" "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 '
.TH "ANSIBLE" "1" "04/26/2012" "Ansible 0\&.4" "System administration commands"
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
@ -34,7 +25,7 @@ ansible \- run a command somewhere else
ansible <host\-pattern> [\-f forks] [\-m module_name] [\-a args]
.SH "DESCRIPTION"
.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"
.PP
\fBhost\-pattern\fR
@ -72,7 +63,7 @@ to load modules from\&. The default is
\fI/usr/share/ansible\fR\&.
.RE
.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
The
\fIARGUMENTS\fR

View file

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

View file

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