0

I have this vars file:

addvlan:
 - vlan: pruebavlan
   address: 192.168.10.0
   mask: 25
   gateway: ????
   tag: 1917

And I have this JSON which use the vars of above vars:

  "address": "{{address}}",
  "mask": {{mask}},
  "gateway": "{{gateway}}",
  "tag": "{{tag}}",

I need that the gateway will be always the next IP of the address. For example, in the above case, could be 192.168.10.1

Do you know if it's possible or any way to do it?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

2 Answers2

1

Have a look at the ipaddr filter:

gateway: "{{ network_address | ipaddr('1') | ipaddr('address') }}"
techraf
  • 64,883
  • 27
  • 193
  • 198
  • I got error: 'address' is undefined". I forgot to say that my vars are defined into a list.. Is it possible that this is the error? –  Feb 03 '17 at 08:18
1

Solution without ipaddr filter:

{{ gateway.split('.')[:3] | join('.') + '.' + (gateway.split('.')[3] | int + 1) | string }}

But you can't do this:

addvlan:
 - vlan: pruebavlan
   address: 192.168.10.0
   mask: 25
   gateway: "{{ << address manipulations here >> }}"
   tag: 1917

This will give you recursion error, because you try to define keys of addvlan.vlan referencing other keys from same dict.
Do manipulations in your JSON template instead:

  ...
  "address": "{{ address }}",
  "mask": "{{ mask }}",
  "gateway": "{{ address.split('.')[:3] | join('.') + '.' + (address.split('.')[3] | int + 1) | string }}",
  "tag": "{{ tag }}",
  ...
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
  • I got: 'gateway' is undefined". I forgot to say that my vars are defined into a list.. Is it possible that this is the error? –  Feb 03 '17 at 08:19
  • Replace `gateway` with appropriate variable name for you setup – Konstantin Suvorov Feb 03 '17 at 08:24
  • Yes, i did it, i used "listname.gateway" but i got this error: `recursive loop detected in template string: {{ addvlan.gateway.split('.')[:3] | join('.') + '.' + (addvlan.gateway.split('.')[3] | int + 1) | string }}"` –  Feb 03 '17 at 08:26
  • @hectormarina in Ansible/Jinja you can't define variable based on itself. So `gateway: "{{ gateway + 'zzzz' }}"` gives you a recursion. Change to `z_gateway: "{{ gateway + 'zzzz' }}"` to fix this. – Konstantin Suvorov Feb 03 '17 at 08:49
  • I change like you said me: `z_gateway: "{{ addvlan.gateway.split('.')[:3] | join('.') + '.' + (addvlan.gateway.split('.')[3] | int + 1) | string }}"` and i got the same error... –  Feb 03 '17 at 09:09
  • I update the OP with correct vars setup. Take a look, –  Feb 03 '17 at 09:14