Re-add vars to Base and standardize var processing

Fixes #11779
pull/11887/head
James Cammarata 9 years ago
parent ebfd99e307
commit e7d0c9f820

@ -36,9 +36,8 @@ from ansible.parsing import DataLoader
from ansible.playbook.attribute import Attribute, FieldAttribute from ansible.playbook.attribute import Attribute, FieldAttribute
from ansible.template import Templar from ansible.template import Templar
from ansible.utils.boolean import boolean from ansible.utils.boolean import boolean
from ansible.utils.debug import debug from ansible.utils.debug import debug
from ansible.utils.vars import combine_vars
from ansible.template import template from ansible.template import template
class Base: class Base:
@ -48,6 +47,9 @@ class Base:
_port = FieldAttribute(isa='int') _port = FieldAttribute(isa='int')
_remote_user = FieldAttribute(isa='string') _remote_user = FieldAttribute(isa='string')
# variables
_vars = FieldAttribute(isa='dict', default=dict())
# flags and misc. settings # flags and misc. settings
_environment = FieldAttribute(isa='list', default=[]) _environment = FieldAttribute(isa='list', default=[])
_no_log = FieldAttribute(isa='bool', default=False) _no_log = FieldAttribute(isa='bool', default=False)
@ -351,6 +353,30 @@ class Base:
# restore the UUID field # restore the UUID field
setattr(self, '_uuid', data.get('uuid')) setattr(self, '_uuid', data.get('uuid'))
def _load_vars(self, attr, ds):
'''
Vars in a play can be specified either as a dictionary directly, or
as a list of dictionaries. If the later, this method will turn the
list into a single dictionary.
'''
try:
if isinstance(ds, dict):
return ds
elif isinstance(ds, list):
all_vars = dict()
for item in ds:
if not isinstance(item, dict):
raise ValueError
all_vars = combine_vars(all_vars, item)
return all_vars
elif ds is None:
return {}
else:
raise ValueError
except ValueError:
raise AnsibleParserError("Vars in a %s must be specified as a dictionary, or a list of dictionaries" % self.__class__.__name__, obj=ds)
def _extend_value(self, value, new_value): def _extend_value(self, value, new_value):
''' '''
Will extend the value given with new_value (and will turn both Will extend the value given with new_value (and will turn both

@ -53,7 +53,7 @@ class Block(Base, Become, Conditional, Taggable):
of a role or task include which does, so return those if present. of a role or task include which does, so return those if present.
''' '''
all_vars = dict() all_vars = self.vars.copy()
if self._role: if self._role:
all_vars.update(self._role.get_vars(self._dep_chain)) all_vars.update(self._role.get_vars(self._dep_chain))

@ -32,8 +32,6 @@ from ansible.playbook.role import Role
from ansible.playbook.taggable import Taggable from ansible.playbook.taggable import Taggable
from ansible.playbook.task import Task from ansible.playbook.task import Task
from ansible.utils.vars import combine_vars
__all__ = ['Play'] __all__ = ['Play']
@ -64,7 +62,6 @@ class Play(Base, Taggable, Become):
_name = FieldAttribute(isa='string', default='') _name = FieldAttribute(isa='string', default='')
# Variable Attributes # Variable Attributes
_vars = FieldAttribute(isa='dict', default=dict())
_vars_files = FieldAttribute(isa='list', default=[]) _vars_files = FieldAttribute(isa='list', default=[])
_vars_prompt = FieldAttribute(isa='list', default=[]) _vars_prompt = FieldAttribute(isa='list', default=[])
_vault_password = FieldAttribute(isa='string') _vault_password = FieldAttribute(isa='string')
@ -150,30 +147,6 @@ class Play(Base, Taggable, Become):
return ds return ds
def _load_vars(self, attr, ds):
'''
Vars in a play can be specified either as a dictionary directly, or
as a list of dictionaries. If the later, this method will turn the
list into a single dictionary.
'''
try:
if isinstance(ds, dict):
return ds
elif isinstance(ds, list):
all_vars = dict()
for item in ds:
if not isinstance(item, dict):
raise ValueError
all_vars = combine_vars(all_vars, item)
return all_vars
elif ds is None:
return {}
else:
raise ValueError
except ValueError:
raise AnsibleParserError("Vars in a playbook must be specified as a dictionary, or a list of dictionaries", obj=ds)
def _load_tasks(self, attr, ds): def _load_tasks(self, attr, ds):
''' '''
Loads a list of blocks from a list which may be mixed tasks/blocks. Loads a list of blocks from a list which may be mixed tasks/blocks.

@ -78,7 +78,6 @@ class Task(Base, Conditional, Taggable, Become):
_retries = FieldAttribute(isa='int', default=1) _retries = FieldAttribute(isa='int', default=1)
_run_once = FieldAttribute(isa='bool') _run_once = FieldAttribute(isa='bool')
_until = FieldAttribute(isa='list') # ? _until = FieldAttribute(isa='list') # ?
_vars = FieldAttribute(isa='dict', default=dict())
def __init__(self, block=None, role=None, task_include=None): def __init__(self, block=None, role=None, task_include=None):
''' constructors a task, without the Task.load classmethod, it will be pretty blank ''' ''' constructors a task, without the Task.load classmethod, it will be pretty blank '''
@ -166,9 +165,9 @@ class Task(Base, Conditional, Taggable, Become):
# be adding things to them below (special handling for includes). # be adding things to them below (special handling for includes).
# When that deprecated feature is removed, this can be too. # When that deprecated feature is removed, this can be too.
if 'vars' in ds: if 'vars' in ds:
if not isinstance(ds['vars'], dict): # _load_vars is defined in Base, and is used to load a dictionary
raise AnsibleError("vars specified on a task must be a dictionary (got a %s)" % type(ds['vars']), obj=new_ds) # or list of dictionaries in a standard way
new_ds['vars'] = ds.pop('vars') new_ds['vars'] = self._load_vars(None, ds.pop('vars'))
else: else:
new_ds['vars'] = dict() new_ds['vars'] = dict()

@ -412,7 +412,7 @@ class StrategyBase:
# set the vars for this task from those specified as params to the include # set the vars for this task from those specified as params to the include
for b in block_list: for b in block_list:
b.vars = included_file._args.copy() b.vars.update(included_file._args.copy())
return block_list return block_list

Loading…
Cancel
Save