Ansible Jinja Template Comparison

22 views
Skip to first unread message

Krishna N

unread,
Sep 17, 2018, 6:40:33 PM9/17/18
to Ansible Project

Greetings!

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

    with_items: "{{ runconfser.stdout_lines[0] }}"
    when: (item not in {src: 'ntprequired.j2'} and (item!=""))
    ios_config:
      lines:
        - "no {{ item }}"
I am using roles.
/templates/ntprequired.j2:
{% for ntp_srv in ntp_servers %}
ntp server {{ ntp_srv }}
{% endfor %}

Jonathon Woods

unread,
Sep 18, 2018, 6:22:39 AM9/18/18
to ansible...@googlegroups.com
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. 



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1d64f07a-6be8-483f-8dd7-343ea7ab73dd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Jonathon Woods

Krishna N

unread,
Sep 18, 2018, 2:54:40 PM9/18/18
to ansible...@googlegroups.com, jswo...@gmail.com
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
---
- name: Current Edge servers before
ios_command:
commands:
- sh run | include ntp server
register: runconfser

- debug:
var: runconfser

# NTP SECTION - START

- name: Set NTP servers
ios_config:
src: ntprequired.j2
notify: Save Config

- name: Remove the rest NTP Servers
with_items: "{{ runconfser.stdout_lines[0] }}"
when: (item not in {src: 'ntprequired.j2'} and (item!=""))
ios_config:
lines:
- "no {{ item }}"


You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/7kKzc8j7cks/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

Krishna N

unread,
Sep 26, 2018, 7:20:47 PM9/26/18
to ansible...@googlegroups.com, Jonathon Woods
Any help is highly appreciated!
Reply all
Reply to author
Forward
0 new messages