1

I search the best method to replace last number of an IP with Jinja2.

I need to replace the last octet of the IP:

{% set ip = "192.168.1.1" %}
{% set points == ip.split('.') %}
{{ points | last | replace (points | last , "0") }}

Result: 0
Desired result: 192.168.1.0

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
CH06
  • 139
  • 2
  • 14

1 Answers1

1

You can use Python's slicing in order to get all the list elements except the last one with a negative index, then

  1. either concatenate a list containing your new element and finally join
    {{ ("192.168.1.1".split(".")[:-1] + ["0"]) | join(".") }}
    
  2. or join and then concatenate with a string with your new element
    {{ "192.168.1.1".split(".")[:-1] | join(".") + ".0" }}
    
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83