From 24226646fc43198d7c20f9590248b7189a4c8b96 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Sun, 28 Jun 2015 01:00:32 -0400 Subject: [PATCH] When loading the play hosts list, enforce some consistency Fixes #9580 --- lib/ansible/playbook/play.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py index 093a4e1d472..c3d9aea06ba 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