Hi again.   Thanks .. I got so hung up on using with_dict I didn't consider 
taking another tack.

I'm reconsidering the decision to not to use  hash_behaviour=merge as that 
solves my problem + would also come in handy elsewhere, like combining 
default sysctl parameter changes with additional application & environment 
requirements.    If I don't then it would be an interesting exercise for me 
to write a custom module to merge dictionaries, or even add a new ansible 
filter.   

Cheers,  J

On Wednesday, 11 March 2015 16:01:25 UTC, Jonathan Davila wrote:
>
> so I've got a working sample for you. This is the exact test I did.
>
> ---
> - name: Template Test For Route File
>   connection: local
>   hosts: localhost
>   vars:
>    host_routes:
>      route-oob:
>         device: bond.123
>         routes:
>           10.118.0.0/16: '10.118.255.254'
>      route-int:
>         device: bond.456
>         routes:
>           172.168.0.0/23: '172.168.1.254'
>    standard_routes:
>      route-oob:
>         device: bond.123
>         routes:
>           10.198.0.0/24: '10.118.255.254'
>   tasks:
>     - template: src=t.j2 dest=./templated
>
>
>  
> *and my jinja template file looks like:*
>
> {% if host_routes %}
> {% for k in host_routes %}
> {% for route in host_routes[k]['routes'] %} {{ route }} via {{host_routes[
> k]['routes'][route]}} dev {{ host_routes[k]['device'] }}
> {% endfor %}
> {% endfor %}
> {% endif %}
> {% if standard_routes %}
> {% for k in standard_routes %}
> {% for route in standard_routes[k]['routes'] %} {{ route }} via {{
> standard_routes[k]['routes'][route]}} dev {{ standard_routes[k]['device'] 
> }}
> {% endfor %}
> {% endfor %}
> {% endif %}
>
> *which should then output to a file called templated in your CWD*
>  172.168.0.0/23 via 172.168.1.254 dev bond.456
>  10.118.0.0/16 via 10.118.255.254 dev bond.123
>  10.198.0.0/24 via 10.118.255.254 dev bond.123
>
>
> Actually don't need the with_dict loop here at all.
>
> Hopefully that helps, let us know otherwise.
>
> On Tuesday, March 10, 2015 at 9:23:49 AM UTC-4, John McNulty wrote:
>>
>>
>> Hi,
>>
>> So here's the scenario - I'm given a data structure that when converted 
>> to yaml looks like this (names & numbers invented).
>>
>> host_routes:
>>   route-oob:
>>     device: bond.123
>>     routes:
>>       10.118.0.0/16: '10.118.255.254'
>>   route-int:
>>     device: bond.456
>>     routes:
>>       172.168.0.0/23: '172.168.1.254'
>>
>> I can use that with a template to create the network-script route files 
>> needed, that's easy enough.   But in real life there can be up to several 
>> OOB routes that this host will need depending on which datacentre / hall / 
>> security zone it resides in. e.g. 
>>
>> standard_routes:
>>   route-oob:
>>     device: bond.123
>>     routes:
>>       10.198.0.0/24: '10.118.255.254'
>>
>> I'd like to add these additional routes to the appropriate group_var file 
>> and then merge them together when creating the route files.   I can't get 
>> this to work though.  I've tried all the following:
>>
>> with_dict: host_routes|union(standard_routes)
>>
>> with_dict: union(host_routes, standard_routes)
>>
>> with_dict: host_routes + standard_routes
>>
>>
>> and get the same error every time ..
>>
>> fatal: [localhost] => with_dict expects a dict
>>
>>
>> Is there another filter or some other way of doing this?   I don't want 
>> to change the default hash_behaviour to merge just for this one use case as 
>> that has much wider implications.
>>
>> Thanks,  John
>>
>>
>>

-- 
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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/2ad17396-9255-4e54-8891-fde2e2ad3ec3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to