Settings

Theme

Winning at Ansible: How to manipulate items in a list

blog.caranna.works

2 points by carlosnunez 10 years ago · 2 comments

Reader

alexandrerond 10 years ago

I don't understand this.

If you need to iterate over the items you can just use "with_items" or "with_subelements" (http://docs.ansible.com/ansible/playbooks_loops.html#looping...) and the "{{ item }}_12345"

If you need a list for a template you can always loop it in the template and access the sublements there and template them as you wish.

One of the strong points of Ansible is that it can be read very easily. The moment you do:

"{{ important_files | map(attribute='file_name') | list | map('regex_replace','(.*)','\\1_{{ file_suffix }}') | list }}"

you throw it away.

  • carlosnunezOP 10 years ago

    The problem that I found with that approach for this particular use case (maybe using files wasn't a great example, since it has its own module and files can be templated) is that the variable itself is a list of dicts. I found that using with_subelements or with_items for this didn't reduce the complexity much since I still needed to resort to Jinja gymnastics to get what I needed and then needed to worry about taking that output and casting it to a list anyway.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection