需要有关for循环的帮助

时间:2018-05-15 20:53:44

标签: ansible jinja2

我想知道是否有一种方法可以让下面的代码每次列出时{{routemapnumber}}值增加5。所以第一个{{routemapnumber}}值将是5然后另一个将是10等。我认为for循环可以工作但我有语法问题。

route-map RDS_FILTER permit {{routemapnumber}}
 match ip address {{item.aclnumber}}
 set ip next-hop verify-availability 10.4.21.157 1 track {{item.slanumber}}
 set ip next-hop 192.168.2.58
route-map RDS_FILTER permit {{routemapnumber}}
 match ip address 109
 set ip next-hop verify-availability 10.4.21.157 1 track 1
 set ip next-hop 192.168.2.58
route-map RDS_FILTER permit {{routemapnumber}}
 match ip address 108
route-map RDS_FILTER permit {{routemapnumber}}
match ip address 107
 set ip next-hop 192.168.0.2
route-map RDS_FILTER permit {{routemapnumber}

1 个答案:

答案 0 :(得分:0)

如果没有很多行,我会使用硬编码增量,例如:

route-map RDS_FILTER permit {{routemapnumber}}
 match ip address
 set ip next-hop verify-availability 10.4.21.157 1 track
 set ip next-hop 192.168.2.58
route-map RDS_FILTER permit {{routemapnumber + 10}}
 match ip address 109
 set ip next-hop verify-availability 10.4.21.157 1 track 1
 set ip next-hop 192.168.2.58
route-map RDS_FILTER permit {{routemapnumber + 15}}
 match ip address 108
route-map RDS_FILTER permit {{routemapnumber + 20}}
match ip address 107
 set ip next-hop 192.168.0.2
route-map RDS_FILTER permit {{routemapnumber + 25}}

如果这对你来说不是一个好的选择,你可以在模板之前添加一些数学逻辑,使用set语句增加变量,然后用它打印:

{% set routemapnumber = routemapnumber + 5 %}

完整示例:

route-map RDS_FILTER permit {{routemapnumber}}
 match ip address
 set ip next-hop verify-availability 10.4.21.157 1 track
 set ip next-hop 192.168.2.58
route-map RDS_FILTER permit {% set routemapnumber = routemapnumber + 5 %}{{routemapnumber}}
 match ip address 109
 set ip next-hop verify-availability 10.4.21.157 1 track 1
 set ip next-hop 192.168.2.58
route-map RDS_FILTER permit {% set routemapnumber = routemapnumber + 5 %}{{routemapnumber}}
 match ip address 108
route-map RDS_FILTER permit {% set routemapnumber = routemapnumber + 5 %}{{routemapnumber}}
match ip address 107
 set ip next-hop 192.168.0.2
route-map RDS_FILTER permit {% set routemapnumber = routemapnumber + 5 %}{{routemapnumber}}