Thanks that's a good idea. I'm struggling to come up with a good name for the filter though. I'll think about it a bit more but best place to start almost seems to be extending with_subelements so the second parameter can be a list (which would at least mean I don't have to come up with a new filter name. I guess I could make something for internal use only, but would rather create something that I could contribute back to ansible.
Jon On Monday, July 3, 2017 at 10:22:57 AM UTC+1, Pshem Kowalczyk wrote: > > Hi, > > If you feel up to it - write a filter plugin. They're quite easy to > create. Have a look here for an example: > http://www.dasblinkenlichten.com/creating-ansible-filter-plugins/ > I use them when I have limited control over the data structure I have to > deal with. Even if data manipulation can be done in 'pure' Ansible it ends > up being quite convoluted (and usually spread across multiple files, since > 'include' is the only way to have loop inside a loop whilst retaining full > control over the data). > > kind regards > Pshem > > > On Mon, 3 Jul 2017 at 19:34 'J Hawkesworth' via Ansible Project < > [email protected] <javascript:>> wrote: > >> Thank you for this. It might well be easier for me to reformat the >> source variable. >> >> The thing I want to avoid most of all is having to maintain two separate >> variables so the ability to transform from one to another at playbook >> runtime is the thing I need the most. >> >> Jon >> >> >> On Sunday, July 2, 2017 at 7:03:32 PM UTC+1, Daniel JD wrote: >>> >>> I couldnt get it to work with you variable. It is just bad formatted. >>> >>> If you could format it like this it would be easy: >>> >>> components_by_group: >>> - name: web >>> components: >>> - frontend_app >>> - admin_app >>> - name: database >>> components: >>> - db_tools >>> - flyway >>> >>> tasks: >>> - name: Change the format of the Group >>> set_fact: >>> group_by_components: "{{ group_by_components | default(dict()) >>> | combine({ item.1 : item.0.name }) }}" >>> with_subelements: >>> - "{{ components_by_group }}" >>> - components >>> >>> Outputs: >>> >>> ok: [localhost] => { >>> "group_by_components": { >>> "admin_app": "web", >>> "db_tools": "database", >>> "flyway": "database", >>> "frontend_app": "web" >>> } >>> } >>> >>> >>> Am Mittwoch, 28. Juni 2017 17:17:41 UTC+2 schrieb J Hawkesworth: >>>> >>>> Hello, >>>> >>>> So I have a var dictionary which stores component groups and components >>>> >>>> components_by_group: >>>> web: >>>> - frontend_app >>>> - admin_app >>>> database: >>>> - db_tools >>>> - flyway >>>> >>>> I'd like to invert this so that I can use the component to lookup the >>>> group >>>> >>>> group_by_component: >>>> frontend_app: web >>>> admin_app: web >>>> db_tools: database >>>> flyway: database >>>> >>>> >>>> I can make a list of the groups easily enough... >>>> >>>> - name: make a list of the groups >>>> set_fact: >>>> component_groups: "{{ component_groups|default([]) + [ >>>> item.key ] }}" >>>> with_dict: "{{ components_by_group }}" >>>> >>>> >>>> How can I make a dict that looks like group_by_component above? >>>> >>>> Many thanks, >>>> >>>> Jon >>>> >>> -- >> You received this message because you are subscribed to the Google Groups >> "Ansible Project" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected] <javascript:>. >> To post to this group, send email to [email protected] >> <javascript:>. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/ansible-project/87f66af2-d2f2-436b-b797-3bc8e0307cd7%40googlegroups.com >> >> <https://groups.google.com/d/msgid/ansible-project/87f66af2-d2f2-436b-b797-3bc8e0307cd7%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/0b5e56a0-f63f-4bc0-bba1-31958b5c3943%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
