Skip to content

Instantly share code, notes, and snippets.

@dspruell
Created June 10, 2016 00:23
Show Gist options
  • Save dspruell/d12506b7332789f726ba487636b36c74 to your computer and use it in GitHub Desktop.
Save dspruell/d12506b7332789f726ba487636b36c74 to your computer and use it in GitHub Desktop.
Filter idea - quote_each - surround array members in quotes
def quote_each(data, type='double'):
'''
Take a list of strings and output the list with each item surrounded by
quotation marks. Defaults to using double quotes ("item"), may be specified
instead to use single quotes ('item').
'''
QUOTE_TYPES = {
'double': '"',
'single': "'",
}
if not isinstance(data, list):
raise errors.AnsibleFilterError("|failed expects first param is a list")
if not all(isinstance(x, basestring) for x in data):
raise errors.AnsibleFilterError("|failed expects first param is a list"
" of strings")
retval = [(QUOTE_TYPES[type] + s + QUOTE_TYPES[type]) for s in data]
return retval
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment