Junos Automation (Scripting)
Highlighted
Junos Automation (Scripting)

Loop in Jinja2 template - Ansible

‎06-01-2018 10:04 AM

Hello Experts,

 

Can anyone please guide me how to do a loop in Jinja2 templates. I have sitation like this:

Vaiable definition in host_vars:
------------------------------------------------
vars:

 - fromzone: "untrust"
   tozone: "trust"
   policyname: "ansible"
   sourceaddr: "LOG01"
   destaddr: Internal-NTP-Servers"
   application: "any"
   action: "permit"

 - fromzone: "untrust"
   tozone: "trust"
   policyname: "ansible"
   sourceaddr: "LOG02"
   destaddr: Internal-NTP-Servers"
   application: "any"
   action: "permit"



-------------------------------------------
TEMPLATE:

{% for items in vars.values() %}
 {% for entry in items %}  
  security policies from-zone {{entry}} to-zone {{entry}} {
     policy {{entry}} {
                match {
                    source-address {{entry}};
                    destination-address {{entry}};
                    application {{entry}};
                }
                then {
                    {{entry}};
                }
            }
    }

{% endfor %}
{% endfor %}

I am getting this error when I run this.

fatal: [vic-ccbb-fw-01]: FAILED! => {"changed": false, "msg": "AnsibleError: Unexpected templating type error occurred on ({% for items in vars.values() %}\n {% for entry in items %}  \n  security policies from-zone {{entry}} to-zone {{entry}} {\n     policy {{entry}} {\n                match {\n                    source-address {{entry}};\n                    destination-address {{entry}};\n                    application {{entry}};\n                }\n                then {\n                    {{entry}};\n                }\n            }\n    }\n\n{% endfor %}\n{% endfor %}\n): 'bool' object is not iterable"}
        to retry, use: --limit @/home/ec2-user/Ansible/Vic-CCBB-FW-01/apply_configuration.retry
1 REPLY 1
Highlighted
Junos Automation (Scripting)

Re: Loop in Jinja2 template - Ansible

‎07-17-2018 09:07 PM

not tested, but it should look something like below:

{% for item in vars %}

  security policies from-zone {{item.fromzone}} to-zone {{item.tozone}} {
     policy {{item.policyname}} {
                match {
                    source-address {{item.sourceaddr}};
                    destination-address {{item.destaddr}};
                    application {{item.application}};
                }
                then {
                    {{item.action}};
                }
            }
    }
{% endfor %}