loopsjinja2salt-projectkannel

How to loop over Kannel config in Jinja2


I have a config file for Kannel (open source WAP and SMS gateway) that has multiple entries that may change over time. The file looks like this:

group = smsc
smsc = smpp
smsc-id = my_smsc
allowed-smsc-id = my_smsc
host = 127.1.2.3
port = 1234
receive-port = 0
smsc-username = User1
smsc-password = FakePassword
transceiver-mode = yes
system-type = "SMPP"
throughput = 50
max-pending-submits = 10
source-addr-ton = 0
source-addr-npi = 0
enquire-link-interval = 30

This block of config can be added numerous times in the same file to open multiple binds to increase throughput.

I need to loop through a sls version of the file using Jinja2 in order to create a conf file on a container, using salt.

But this post is just to gather any ideas of how to loop through such a file? Any help will be greatly appreciated!

Thank YOU!


Solution

  • I eventually solved this issue by putting these values into a pillar file (.sls) and then creating a template with placeholder values that linked to the original file. Here is what the template looked like:

    group="{{ kanneltrx.group }}"
    smsc="{{ kanneltrx.smsc }}"
    smsc-id="{{ kanneltrx.smsc_id }}"
    allowed-smsc-id="{{ kanneltrx.allowed_smsc_id }}"
    host="{{ kanneltrx.host }}"
    port="{{ kanneltrx.port }}"
    receive-port="{{ kanneltrx.receive_port }}"
    smsc-username="{{ kanneltrx.smsc_username }}"
    smsc-password="{{ kanneltrx.smsc_password }}"
    transceiver-mode="{{ kanneltrx.transceiver_mode }}"
    system-type="{{ kanneltrx.system_type }}"
    throughput="{{ kanneltrx.throughput }}"
    max-pending-submits="{{ kanneltrx.max_pending_submits }}"
    source-addr-ton="{{ kanneltrx.source_addr_ton }}"
    source-addr-npi="{{ kanneltrx.source_addr_npi }}"
    enquire-link-interval="{{ kanneltx.enquire_link_interval }}"
    

    The values in the brackets were then the field in the pillar file.

    The only thing missing was how to tell Jinja how many times to loop over the config for the amount of binds needed. For that I added another field called number_of_binds to the pillar and wrote a for statement to loop that many times.

    {% if pillar.kanneltx is defined %}
    {% set kanneltx = salt['pillar.get']('kanneltx') %}
    {% for i in range ( kanneltx.number_of_binds ) %}