Splitting variable not working in Ansible

MMA picture MMA · Apr 5, 2017 · Viewed 12.2k times · Source

I am trying to split the variable based on delimiter. How can I achieve it?

  some_module: {{item}}.split('@')[1]
  with_items:
     - git@someversionxxx
     - gradle@someversionxxx

I get following error:

list object' has no attribute 'split ansible

I want to consider only first part of variable i.e. before '@'

Answer

techraf picture techraf · Apr 5, 2017
some_module: "{{ item.split('@')[0] }}"
  • {{ ... }} is used to indicate Jinja2 expressions and everything you have is a Jinja2 expression
  • with YAML syntax in Ansible you must quote a string if it starts with { (unless it was a JSON object, here it's not)
  • the first element of the split result will have an index of 0