diff options
author | James Cammarata <jimi@sngx.net> | 2015-06-28 01:00:32 -0400 |
---|---|---|
committer | James Cammarata <jimi@sngx.net> | 2015-06-28 01:00:32 -0400 |
commit | 24226646fc43198d7c20f9590248b7189a4c8b96 (patch) | |
tree | 14159cbe3a24b2cd4f918f0f8cd259460f940031 | |
parent | 9d9cd0c42ca9a401f299f8cb805aafe3c0817b9e (diff) | |
download | ansible-24226646fc43198d7c20f9590248b7189a4c8b96.tar.gz |
When loading the play hosts list, enforce some consistency
Fixes #9580
-rw-r--r-- | lib/ansible/playbook/play.py | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py index 093a4e1d47..c3d9aea06b 100644 --- a/lib/ansible/playbook/play.py +++ b/lib/ansible/playbook/play.py @@ -19,6 +19,8 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type +from six import string_types + from ansible.errors import AnsibleError, AnsibleParserError from ansible.playbook.attribute import Attribute, FieldAttribute @@ -57,7 +59,7 @@ class Play(Base, Taggable, Become): # Connection _gather_facts = FieldAttribute(isa='string', default='smart') - _hosts = FieldAttribute(isa='list', default=[], required=True) + _hosts = FieldAttribute(isa='list', default=[], required=True, listof=string_types) _name = FieldAttribute(isa='string', default='') # Variable Attributes @@ -121,6 +123,28 @@ class Play(Base, Taggable, Become): return super(Play, self).preprocess_data(ds) + def _load_hosts(self, attr, ds): + ''' + Loads the hosts from the given datastructure, which might be a list + or a simple string. We also switch integers in this list back to strings, + as the YAML parser will turn things that look like numbers into numbers. + ''' + + if isinstance(ds, (string_types, int)): + ds = [ ds ] + + if not isinstance(ds, list): + raise AnsibleParserError("'hosts' must be specified as a list or a single pattern", obj=ds) + + # YAML parsing of things that look like numbers may have + # resulted in integers showing up in the list, so convert + # them back to strings to prevent problems + for idx,item in enumerate(ds): + if isinstance(item, int): + ds[idx] = "%s" % item + + return ds + def _load_vars(self, attr, ds): ''' Vars in a play can be specified either as a dictionary directly, or |