Filter plugins

36 views
Skip to first unread message

Valentin Fadeev

unread,
Jul 7, 2015, 8:43:31 AM7/7/15
to ansible...@googlegroups.com
Recently I had to write templates containing somewhat long filter pipelines. Part of the logic involved lookuping current output of the pipeline in a dictionary (hostvars) and then extracting the value of a field of the resulting dictionary. In order to keep things decoupled I wrote two almost trivial plugins. Here an example:

 - name: find another host to join to form a cluster (e.g. Consul)
   set_fact
:
     host_to_join
: >
     
{{
       groups
['my_group'] | difference([inventory_hostname])
                         
| list
                         
| random
                         
| find_in(hostvars)
                         
| get_key('ec2_private_ip_address')
     
}}

Just wondering whether there are other existing ways of achieving the same result, with/without plugins, as surely someone must have faced a similar task.

Thanks
Valentin

Brian Coca

unread,
Jul 7, 2015, 11:16:49 AM7/7/15
to ansible...@googlegroups.com
something like:

- name: find another host to join to form a cluster (e.g. Consul)
set_fact:
host_to_join: >
{{
(groups['my_group'] | difference([inventory_hostname])
| random
| intersection(hostvars.keys())
).'ec2_private_ip_address'
> --
> 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 ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/0b9c36de-08e3-41df-a21c-1d2e8df19e1b%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca
Reply all
Reply to author
Forward
0 new messages