Answers: <p>I'm not sure that you can really set breakpoints in Jinja templates. You certainly can't in Saltstack without modifying the Saltstack code, and imagine similar is true with Ansible. But, if what you are trying to do is test/debug your jinja2 template, consider trying out the <a href="http://jinja.quantprogramming.com/" rel="nofollow noreferrer">Jinja Live Parser</a>.</p>
  <p>Also consider formatting your jinja a little neater like:</p>
  <pre><code>{%- if azure_anf_pool is defined %} 
    {%- for pool in azure_anf_pool if break | default(False) == False %}
      {%- if pool.azure_anf_volume is defined %} 
        {%- for volume in pool.azure_anf_volume %} 
          {%- if &quot;CIFS&quot; in volume.volume_protocol %} 
            {%- set break = True %} 
            {%- set join_ad = true %} 
  {{- join_ad + &quot;\n&quot; }} 
          {%- else %} 
            {% set join_ad = &quot;false&quot; -%} 
  {{- join_ad + &quot;\n&quot; }} 
          {%- endif %} 
        {%- endfor %}
      {%- endif %}
    {%- endfor %}
  {%- endif %}
  </code></pre>
  <p>You may already have some nesting/formatting in your original post that may have been stripped if you don't know/didn't use Stack Exchange's <a href="https://meta.stackexchange.com/help/formatting">markdown formatting</a>.</p>
  <p>But even then, note the <code>-</code> character at the beginning of the lines - this will help you prevent line returns and whitespace that you do not want in your jinja.</p>
  <p>I suspect you are <em>really</em> after is something along the lines of:</p>
  <pre><code>{%- if azure_anf_pool is defined %}
    {%- for pools in azure_anf_pool %}
      {%- for pool in pools.values() %}
        {%- if pool.azure_anf_volume is defined %}
          {%- if pool.azure_anf_volume.volume_protocol == &quot;CIFS&quot; %}
            {%- set join_ad = True %}
          {%- else %}
            {%- set join_ad = False %}
          {%- endif %}
  {{- pools.keys()[0] + &quot;: &quot; + join_ad|string + &quot;\n&quot;}}
        {%- endif %}
      {%- endfor %}
    {%- endfor %}
  {% endif %}
  </code></pre>
  <p>Using YAML input like:</p>
  <pre><code>azure_anf_pool:
    - xpng-anf-Pool9:
        pool_name: &quot;xpng-anf-Pool9&quot;
        pool_service_level: &quot;Standard&quot; 
        azure_anf_volume:
          volume_name: &quot;xpng-anf-Vol90&quot;
          volume_protocol: &quot;CIFS&quot;
    - foo:
        pool_name: &quot;xpng-anf-Pool10&quot; 
        pool_service_level: &quot;Standard&quot; 
        azure_anf_volume:
            volume_name: &quot;xpng-anf-Vol100&quot;
            volume_protocol: &quot;NFSv3&quot;
  </code></pre>
  <p>This template renders:</p>
  <pre><code>xpng-anf-Pool9:·True
  xpng-anf-Pool10:·False
  </code></pre>
  <p>Some helpful docs in arriving at this point are:</p>
  <ul>
  <li><p><a href="https://jinja.palletsprojects.com/en/3.1.x/templates/#whitespace-control" rel="nofollow noreferrer">Jinja Whitespace Control</a></p>
  </li>
  <li><p><a href="https://jinja.palletsprojects.com/en/3.1.x/templates/#filters" rel="nofollow noreferrer">Jinja Filters</a></p>
  </li>
  <li><p><a href="https://www.w3schools.com/python/ref_dictionary_values.asp" rel="nofollow noreferrer">The Python Dictionary values() method</a></p>
  </li>
  <li><p><a href="https://www.w3schools.com/python/ref_dictionary_keys.asp" rel="nofollow noreferrer">The Python Dictionary keys() Method</a></p>
  </li>
  <li><p><a href="https://www.w3schools.com/python/python_lists.asp" rel="nofollow noreferrer">Python Lists</a></p>
  </li>
  <li><p><a href="https://realpython.com/python-dicts/" rel="nofollow noreferrer">Python Dictionaries</a></p>
  <p>(Try simply: <code>{{ azure_anf_pool }}</code> in the jinja live parser and then run that through an <a href="https://mothereff.in/html-entities" rel="nofollow noreferrer">HTML entity encoder/decoder</a> [results are encoded, so you need to decode them] to get a readable rendering of the list you have constructed with your YAML. This may help you visualize what's going on a bit better.)</p>
  </li>
  </ul>
  <p>If you can understand and fully grasp the above concepts, it should help you understand what you might be doing wrong. Hope this helps!</p>
  
Answers: CC BY-SA 4.0
Answers: 2024-05-20T19:14:58.350Z
Answers: 2024-05-20T19:37:25.270Z
Answers: 2024-05-20T19:37:25.270Z
Answers: James Shewey
Body: <h2>Below is My Playbook.</h2>
  <p>azure_anf_pool:</p>
  <ul>
  <li>pool_name: &quot;xpng-anf-Pool9&quot;
  pool_service_level: &quot;Standard&quot;
  azure_anf_volume:
  <ul>
  <li>volume_name: &quot;xpng-anf-Vol90&quot;
  volume_protocol: &quot;CIFS&quot;</li>
  </ul>
  </li>
  <li>pool_name: &quot;xpng-anf-Pool10&quot;
  pool_service_level: &quot;Standard&quot;
  azure_anf_volume:
  <ul>
  <li>volume_name: &quot;xpng-anf-Vol100&quot;
  volume_protocol: &quot;NFSv3&quot;</li>
  </ul>
  </li>
  <li>pool_name: &quot;xpng-anf-Pool11&quot;
  pool_service_level: &quot;Standard&quot;
  azure_anf_volume:
  <ul>
  <li>volume_name: &quot;xpng-anf-Vol110&quot;
  volume_protocol: &quot;NFSv3 &amp; CIFS&quot;</li>
  </ul>
  </li>
  <li>pool_name: &quot;xpng-anf-Pool12&quot;
  pool_service_level: &quot;Standard&quot;
  azure_anf_volume:
  <ul>
  <li>volume_name: &quot;xpng-anf-Vol120&quot;
  volume_protocol: &quot;NFSv4.1 &amp; CIFS&quot;</li>
  </ul>
  </li>
  </ul>
  <h2>Below is My jinja2 code.</h2>
  <p>{% if azure_anf_pool is defined %}
  {% for pool in azure_anf_pool  if break | default(False) == False %}
  {% if pool.azure_anf_volume is defined %}
  {% for volume in pool.azure_anf_volume %}
  {% if &quot;CIFS&quot; in volume.volume_protocol %}
  {% set break = True %}
  {% set join_ad = true %}
  {{ join_ad }}
  {% else %}
  {% set join_ad = false %}
  {{ join_ad }}
  {% endif %}
  {% endfor %}
  {% endif %}
  {% endfor %}
  {% endif %}</p>
  <p>What i need is as soon as I found CIFS in volume.volume_protocol it stop loop there and set join_ad true.</p>
  
Tags: <jinja>
Title: Break and continue in Jinja2
UserDisplayName: user45294
ThreadText: # Break and continue in Jinja2
  
  (Asked by: username_0 on 2024-05-20)
  
  ## Below is My Playbook.
  
  azure\_anf\_pool:
  
  * pool\_name: "xpng-anf-Pool9"
    pool\_service\_level: "Standard"
    azure\_anf\_volume:
    + volume\_name: "xpng-anf-Vol90"
      volume\_protocol: "CIFS"
  * pool\_name: "xpng-anf-Pool10"
    pool\_service\_level: "Standard"
    azure\_anf\_volume:
    + volume\_name: "xpng-anf-Vol100"
      volume\_protocol: "NFSv3"
  * pool\_name: "xpng-anf-Pool11"
    pool\_service\_level: "Standard"
    azure\_anf\_volume:
    + volume\_name: "xpng-anf-Vol110"
      volume\_protocol: "NFSv3 & CIFS"
  * pool\_name: "xpng-anf-Pool12"
    pool\_service\_level: "Standard"
    azure\_anf\_volume:
    + volume\_name: "xpng-anf-Vol120"
      volume\_protocol: "NFSv4.1 & CIFS"
  
  ## Below is My jinja2 code.
  
  {% if azure\_anf\_pool is defined %}
  {% for pool in azure\_anf\_pool if break | default(False) == False %}
  {% if pool.azure\_anf\_volume is defined %}
  {% for volume in pool.azure\_anf\_volume %}
  {% if "CIFS" in volume.volume\_protocol %}
  {% set break = True %}
  {% set join\_ad = true %}
  {{ join\_ad }}
  {% else %}
  {% set join\_ad = false %}
  {{ join\_ad }}
  {% endif %}
  {% endfor %}
  {% endif %}
  {% endfor %}
  {% endif %}
  
  What i need is as soon as I found CIFS in volume.volume\_protocol it stop loop there and set join\_ad true.
  
  **Tags:** jinja
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-05-20. Score: 0
  
  I'm not sure that you can really set breakpoints in Jinja templates. You certainly can't in Saltstack without modifying the Saltstack code, and imagine similar is true with Ansible. But, if what you are trying to do is test/debug your jinja2 template, consider trying out the [Jinja Live Parser](http://jinja.quantprogramming.com/).
  
  Also consider formatting your jinja a little neater like:
  
  ```
  {%- if azure_anf_pool is defined %} 
    {%- for pool in azure_anf_pool if break | default(False) == False %}
      {%- if pool.azure_anf_volume is defined %} 
        {%- for volume in pool.azure_anf_volume %} 
          {%- if "CIFS" in volume.volume_protocol %} 
            {%- set break = True %} 
            {%- set join_ad = true %} 
  {{- join_ad + "\n" }} 
          {%- else %} 
            {% set join_ad = "false" -%} 
  {{- join_ad + "\n" }} 
          {%- endif %} 
        {%- endfor %}
      {%- endif %}
    {%- endfor %}
  {%- endif %}
  
  ```
  
  You may already have some nesting/formatting in your original post that may have been stripped if you don't know/didn't use Stack Exchange's [markdown formatting](https://meta.stackexchange.com/help/formatting).
  
  But even then, note the `-` character at the beginning of the lines - this will help you prevent line returns and whitespace that you do not want in your jinja.
  
  I suspect you are *really* after is something along the lines of:
  
  ```
  {%- if azure_anf_pool is defined %}
    {%- for pools in azure_anf_pool %}
      {%- for pool in pools.values() %}
        {%- if pool.azure_anf_volume is defined %}
          {%- if pool.azure_anf_volume.volume_protocol == "CIFS" %}
            {%- set join_ad = True %}
          {%- else %}
            {%- set join_ad = False %}
          {%- endif %}
  {{- pools.keys()[0] + ": " + join_ad|string + "\n"}}
        {%- endif %}
      {%- endfor %}
    {%- endfor %}
  {% endif %}
  
  ```
  
  Using YAML input like:
  
  ```
  azure_anf_pool:
    - xpng-anf-Pool9:
        pool_name: "xpng-anf-Pool9"
        pool_service_level: "Standard" 
        azure_anf_volume:
          volume_name: "xpng-anf-Vol90"
          volume_protocol: "CIFS"
    - foo:
        pool_name: "xpng-anf-Pool10" 
        pool_service_level: "Standard" 
        azure_anf_volume:
            volume_name: "xpng-anf-Vol100"
            volume_protocol: "NFSv3"
  
  ```
  
  This template renders:
  
  ```
  xpng-anf-Pool9:·True
  xpng-anf-Pool10:·False
  
  ```
  
  Some helpful docs in arriving at this point are:
  
  * [Jinja Whitespace Control](https://jinja.palletsprojects.com/en/3.1.x/templates/#whitespace-control)
  * [Jinja Filters](https://jinja.palletsprojects.com/en/3.1.x/templates/#filters)
  * [The Python Dictionary values() method](https://www.w3schools.com/python/ref_dictionary_values.asp)
  * [The Python Dictionary keys() Method](https://www.w3schools.com/python/ref_dictionary_keys.asp)
  * [Python Lists](https://www.w3schools.com/python/python_lists.asp)
  * [Python Dictionaries](https://realpython.com/python-dicts/)
  
    (Try simply: `{{ azure_anf_pool }}` in the jinja live parser and then run that through an [HTML entity encoder/decoder](https://mothereff.in/html-entities) [results are encoded, so you need to decode them] to get a readable rendering of the list you have constructed with your YAML. This may help you visualize what's going on a bit better.)
  
  If you can understand and fully grasp the above concepts, it should help you understand what you might be doing wrong. Hope this helps!
  
  
  ---
  
  