# Ansible Jinja Template Comparison

**URL:** <https://forum.ansible.com/t/ansible-jinja-template-comparison/28595>\
**Category:** Ansible Project\
**Created:** [September 17, 2018, 10:40pm UTC](https://forum.ansible.com/t/ansible-jinja-template-comparison/28595 "2018-09-17T22:40:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krishna\_N](https://avatars.discourse-cdn.com/v4/letter/k/3bc359/32.png) [@Krishna\_N](https://forum.ansible.com/u/Krishna_N)\
**Post date:** [September 17, 2018, 10:40pm UTC](https://forum.ansible.com/t/ansible-jinja-template-comparison/28595/1 "2018-09-17T22:40:33Z")

</div>

Greetings!

How to compare when items are not in jinja template loop?

```auto
    with_items: "{{ runconfser.stdout_lines[0] }}"
    when: (item not in {src: 'ntprequired.j2'} and (item!=""))
    ios_config:
      lines:
        - "no {{ item }}"

```

```auto
I am using roles.

```

```auto
**/templates/ntprequired.j2** :

```

```auto
{% for ntp_srv in ntp_servers %}
ntp server {{ ntp_srv }}
{% endfor %}

```

---

<div class="post-metadata">

**Author:** ![Jonathon\_Woods](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@Jonathon\_Woods](https://forum.ansible.com/u/Jonathon_Woods)\
**Post date:** [September 18, 2018, 10:22am UTC](https://forum.ansible.com/t/ansible-jinja-template-comparison/28595/2 "2018-09-18T10:22:16Z")

</div>

I’m not sure how to make it work how you have laid out. But I would do the check in the jinja template anyway. Get your current ntp lines. In the jinja template run.

{% for ntp\_server in ntp\_config\_get %}  
{% if ntp\_line not in ntp\_servers %}  
no ntp server {{ ntp\_server }}  
{% ending %}  
{% for ntp\_srv in ntp\_servers %}  
ntp server {{ ntp\_srv }}  
{% endfor %}

If you got the whole line of current ntp servers, you’ll have to parse the ips with regex, I’m certain I’ve done something very similar to this before. Let me know if you still have issues after trying this.

---

<div class="post-metadata">

**Author:** ![Krishna\_N](https://avatars.discourse-cdn.com/v4/letter/k/3bc359/32.png) [@Krishna\_N](https://forum.ansible.com/u/Krishna_N)\
**Post date:** [September 18, 2018, 6:54pm UTC](https://forum.ansible.com/t/ansible-jinja-template-comparison/28595/3 "2018-09-18T18:54:17Z")

</div>

Hi Jason,  
Thanks for replying. That’s definitely another way of doing it. However, this is what I am trying.

1. Get the ntp server config from “sh run”.
2. Store that to a list
3. Jinja template generates the required config. I am passing the ntp\_server IPs via -e (extra variables).
4. Add the config from 3, compare 3 and 4 and remove the rest.

# tasks file for ansible-ios-ntp

---

<div class="post-metadata">

**Author:** ![Krishna\_N](https://avatars.discourse-cdn.com/v4/letter/k/3bc359/32.png) [@Krishna\_N](https://forum.ansible.com/u/Krishna_N)\
**Post date:** [September 26, 2018, 11:20pm UTC](https://forum.ansible.com/t/ansible-jinja-template-comparison/28595/4 "2018-09-26T23:20:26Z")

</div>

Any help is highly appreciated!
