From 637d3070dc1cd7ad801635df77b1b5d1fb530cd3 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Thu, 29 Aug 2013 17:21:28 -0500 Subject: [PATCH] Allow default variables to be overridden by inventory variables --- lib/ansible/playbook/__init__.py | 4 ++-- lib/ansible/playbook/play.py | 9 +++------ lib/ansible/playbook/task.py | 7 ++++--- lib/ansible/runner/__init__.py | 4 ++++ 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/ansible/playbook/__init__.py b/lib/ansible/playbook/__init__.py index 3f9130e1539..b8bc24b059a 100644 --- a/lib/ansible/playbook/__init__.py +++ b/lib/ansible/playbook/__init__.py @@ -307,7 +307,7 @@ class PlayBook(object): remote_pass=self.remote_pass, module_path=self.module_path, timeout=self.timeout, remote_user=task.play.remote_user, remote_port=task.play.remote_port, module_vars=task.module_vars, - private_key_file=self.private_key_file, + default_vars=task.default_vars, private_key_file=self.private_key_file, setup_cache=self.SETUP_CACHE, basedir=task.play.basedir, conditional=task.only_if, callbacks=self.runner_callbacks, sudo=task.sudo, sudo_user=task.sudo_user, @@ -447,7 +447,7 @@ class PlayBook(object): remote_pass=self.remote_pass, remote_port=play.remote_port, private_key_file=self.private_key_file, setup_cache=self.SETUP_CACHE, callbacks=self.runner_callbacks, sudo=play.sudo, sudo_user=play.sudo_user, transport=play.transport, sudo_pass=self.sudo_pass, is_playbook=True, module_vars=play.vars, - check=self.check, diff=self.diff + default_vars=play.default_vars, check=self.check, diff=self.diff ).run() self.stats.compute(setup_results, setup=True) diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py index d204980b9da..68563d655ae 100644 --- a/lib/ansible/playbook/play.py +++ b/lib/ansible/playbook/play.py @@ -28,7 +28,7 @@ import os class Play(object): __slots__ = [ - 'hosts', 'name', 'vars', 'vars_prompt', 'vars_files', + 'hosts', 'name', 'vars', 'default_vars', 'vars_prompt', 'vars_files', 'handlers', 'remote_user', 'remote_port', 'sudo', 'sudo_user', 'transport', 'playbook', 'tags', 'gather_facts', 'serial', '_ds', '_handlers', '_tasks', @@ -70,7 +70,7 @@ class Play(object): self.tags = [] ds = self._load_roles(self.roles, ds) - self.vars_files = ds.get('vars_files', []) + self.vars_files = ds.get('vars_files', []) self._update_vars_files_for_host(None) @@ -295,10 +295,7 @@ class Play(object): ds['handlers'] = new_handlers ds['vars_files'] = new_vars_files - defaults = self._load_role_defaults(defaults_files) - # merge default vars with self.vars, with vars taking precedence. - if defaults: - self.vars = utils.combine_vars(defaults, self.vars) + self.default_vars = self._load_role_defaults(defaults_files) return ds diff --git a/lib/ansible/playbook/task.py b/lib/ansible/playbook/task.py index a4f4911fba7..6ff409eab00 100644 --- a/lib/ansible/playbook/task.py +++ b/lib/ansible/playbook/task.py @@ -24,7 +24,7 @@ class Task(object): __slots__ = [ 'name', 'meta', 'action', 'only_if', 'when', 'async_seconds', 'async_poll_interval', - 'notify', 'module_name', 'module_args', 'module_vars', + 'notify', 'module_name', 'module_args', 'module_vars', 'default_vars', 'play', 'notified_by', 'tags', 'register', 'delegate_to', 'first_available_file', 'ignore_errors', 'local_action', 'transport', 'sudo', 'sudo_user', 'sudo_pass', @@ -100,8 +100,9 @@ class Task(object): elif not x in Task.VALID_KEYS: raise errors.AnsibleError("%s is not a legal parameter in an Ansible task or handler" % x) - self.module_vars = module_vars - self.play = play + self.module_vars = module_vars + self.play = play + self.default_vars = play.default_vars # load various attributes self.name = ds.get('name', None) diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index 45e6cb8d07a..63e99c7a947 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -131,6 +131,7 @@ class Runner(object): sudo=False, # whether to run sudo or not sudo_user=C.DEFAULT_SUDO_USER, # ex: 'root' module_vars=None, # a playbooks internals thing + default_vars=None, # ditto is_playbook=False, # running from playbook or not? inventory=None, # reference to Inventory object subset=None, # subset pattern @@ -159,6 +160,7 @@ class Runner(object): self.inventory = utils.default(inventory, lambda: ansible.inventory.Inventory(host_list)) self.module_vars = utils.default(module_vars, lambda: {}) + self.default_vars = utils.default(default_vars, lambda: {}) self.always_run = None self.connector = connection.Connection(self) self.conditional = conditional @@ -405,6 +407,7 @@ class Runner(object): port = self.remote_port inject = {} + inject = utils.combine_vars(inject, self.default_vars) inject = utils.combine_vars(inject, host_variables) inject = utils.combine_vars(inject, self.module_vars) inject = utils.combine_vars(inject, self.setup_cache[host]) @@ -413,6 +416,7 @@ class Runner(object): inject['group_names'] = host_variables.get('group_names', []) inject['groups'] = self.inventory.groups_list() inject['vars'] = self.module_vars + inject['defaults'] = self.default_vars inject['environment'] = self.environment if self.inventory.basedir() is not None: