Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra vars (squashed) #94

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions bin/ansible-playbook
Expand Up @@ -37,6 +37,8 @@ def main(args):
help='set the number of forks to start up')
parser.add_option("-i", "--inventory-file", dest="inventory",
help="inventory host file", default=C.DEFAULT_HOST_LIST)
parser.add_option('-e', '--extra-vars', dest='extra_vars',
help='arguments to pass to the inventory script')
parser.add_option("-k", "--ask-pass", default=False, action="store_true",
help="ask for SSH password")
parser.add_option("-M", "--module-path", dest="module_path",
Expand Down Expand Up @@ -71,6 +73,7 @@ def main(args):
pb = ansible.playbook.PlayBook(
playbook=playbook,
host_list=options.inventory,
extra_vars=options.extra_vars,
module_path=options.module_path,
forks=options.forks,
verbose=True,
Expand Down
21 changes: 15 additions & 6 deletions lib/ansible/playbook.py
Expand Up @@ -56,6 +56,7 @@ def __init__(self,
remote_pass = C.DEFAULT_REMOTE_PASS,
remote_port = C.DEFAULT_REMOTE_PORT,
override_hosts = None,
extra_vars = None,
verbose = False,
callbacks = None,
runner_callbacks = None,
Expand All @@ -75,13 +76,14 @@ def __init__(self,
self.callbacks = callbacks
self.runner_callbacks = runner_callbacks
self.override_hosts = override_hosts
self.extra_vars = extra_vars
self.stats = stats

self.basedir = os.path.dirname(playbook)
self.playbook = self._parse_playbook(playbook)

self.host_list, self.groups = ansible.runner.Runner.parse_hosts(
host_list, override_hosts=self.override_hosts)
host_list, override_hosts=self.override_hosts, extra_vars=self.extra_vars)

# *****************************************************

Expand Down Expand Up @@ -267,8 +269,8 @@ def _run_module(self, pattern, host_list, module, args, remote_user,
timeout=self.timeout, remote_user=remote_user,
remote_port=self.remote_port,
setup_cache=SETUP_CACHE, basedir=self.basedir,
conditional=only_if, callbacks=self.runner_callbacks,
sudo=sudo
conditional=only_if, callbacks=self.runner_callbacks,
extra_vars=self.extra_vars, sudo=sudo
)

if async_seconds == 0:
Expand All @@ -286,14 +288,14 @@ def _run_task(self, pattern=None, host_list=None, task=None,
name = task.get('name', None)
action = task.get('action', None)
if action is None:
raise errors.AnsibleError("action is required for each item in tasks")
raise errors.AnsibleError("action is required for each item in tasks: offending task is %s" % name if name else "unknown")
if name is None:
name = action
only_if = task.get('only_if', 'True')
async_seconds = int(task.get('async', 0)) # not async by default
async_poll_interval = int(task.get('poll', 10)) # default poll = 10 seconds

tokens = shlex.split(action)
tokens = shlex.split(action, posix=True)
module_name = tokens[0]
module_args = tokens[1:]

Expand Down Expand Up @@ -371,7 +373,7 @@ def _do_conditional_imports(self, vars_files, host_list):
raise errors.AnsibleError("vars_files must be a list")
for host in host_list:
cache_vars = SETUP_CACHE.get(host,{})
SETUP_CACHE[host] = {}
#SETUP_CACHE[host] = {}
for filename in vars_files:
if type(filename) == list:
# loop over all filenames, loading the first one, and failing if # none found
Expand Down Expand Up @@ -445,6 +447,13 @@ def _do_setup_step(self, pattern, vars, user, port, sudo, vars_files=None):
for (host, result) in setup_ok.iteritems():
SETUP_CACHE[host] = result

if self.extra_vars:
extra_vars = utils.parse_kv(shlex.split(self.extra_vars))
for h in self.host_list:
try:
SETUP_CACHE[h].update(extra_vars)
except:
SETUP_CACHE[h] = extra_vars
return host_list

# *****************************************************
Expand Down
21 changes: 15 additions & 6 deletions lib/ansible/runner.py
Expand Up @@ -74,7 +74,7 @@ def __init__(self, host_list=C.DEFAULT_HOST_LIST, module_path=C.DEFAULT_MODULE_P
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,
sudo=False):
sudo=False, extra_vars=None):

if setup_cache is None:
setup_cache = {}
Expand All @@ -101,6 +101,7 @@ def __init__(self, host_list=C.DEFAULT_HOST_LIST, module_path=C.DEFAULT_MODULE_P
self.forks = int(forks)
self.pattern = pattern
self.module_args = module_args
self.extra_vars = extra_vars
self.timeout = timeout
self.verbose = verbose
self.remote_user = remote_user
Expand Down Expand Up @@ -143,14 +144,17 @@ def parse_hosts_from_regular_file(cls, host_list):
# *****************************************************

@classmethod
def parse_hosts_from_script(cls, host_list):
def parse_hosts_from_script(cls, host_list, extra_vars):
''' evaluate a script that returns list of hosts by groups '''

results = []
groups = dict(ungrouped=[])
host_list = os.path.abspath(host_list)
cls._external_variable_script = host_list
cmd = subprocess.Popen([host_list], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
cmd = [host_list, '--list']
if extra_vars:
cmd.extend(['--extra-vars', extra_vars])
cmd = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
out, err = cmd.communicate()
try:
groups = utils.json_loads(out)
Expand All @@ -165,7 +169,7 @@ def parse_hosts_from_script(cls, host_list):
# *****************************************************

@classmethod
def parse_hosts(cls, host_list, override_hosts=None):
def parse_hosts(cls, host_list, override_hosts=None, extra_vars=None):
''' parse the host inventory file, returns (hosts, groups) '''

if override_hosts is not None:
Expand All @@ -183,7 +187,7 @@ def parse_hosts(cls, host_list, override_hosts=None):
if not os.access(host_list, os.X_OK):
return Runner.parse_hosts_from_regular_file(host_list)
else:
return Runner.parse_hosts_from_script(host_list)
return Runner.parse_hosts_from_script(host_list, extra_vars)

# *****************************************************

Expand Down Expand Up @@ -275,7 +279,12 @@ def _add_variables_from_script(self, conn, inject):
''' support per system variabes from external variable scripts, see web docs '''

host = conn.host
cmd = subprocess.Popen([Runner._external_variable_script, host],

cmd = [Runner._external_variable_script, '--host', host]
if self.extra_vars:
cmd.extend(['--extra-vars', self.extra_vars])

cmd = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=False
Expand Down