Search code examples
pythonjinja2haproxysalt-project

Autogenerate haproxy.cfg using SaltStack


I am using salt 2015.8.1 (Beryllium) and using SaltStacks documenation, have enable haproxy.cfg autogenertion. However I would like to have a side A and side B type layout in my config file. I have tried to accomplish this using the following:

    # Side A
{% for server, addrs in salt['mine.get']('roles:docker', 'network.ip_addrs', expr_form='grain').items() %}
    {% if '*web1*' in server %}
    server {{ server }} {{ addrs[0] }}:443 check inter 2000 rise 2 fall 5
    {% endif %}
{% endfor %}

    # Side B
{% for server, addrs in salt['mine.get']('roles:docker', 'network.ip_addrs', expr_form='grain').items() %}
    {% if '*web2*' in server %}
    server {{ server }} {{ addrs[0] }}:443 check inter 2000 rise 2 fall 5
    {% endif %}
{% endfor %}

The problem is, while this does run AND complete it does not actually generate anything. I have looked around and asked in IRC but have been able to find a solid solution. If anyone could point me in the right direction I would be eternally grateful. The full haproxy.cfg can be found in this gist:

https://gist.github.com/beardedeagle/8b19a34f332ed26ac859

The documenation where this has been pulled from can be found here:

https://docs.saltstack.com/en/latest/topics/mine/

Again, thanks in advance.

* EDIT * I would like to state that I have the following working without issue so mine data is pulling the way it should:

{% for server, addrs in salt['mine.get']('roles:docker', 'network.ip_addrs', expr_form='grain').items() %}
    server {{ server }} {{ addrs[0] }}:443 check inter 2000 rise 2 fall 5
{% endfor %}

Solution

  • You can use compound matching in your loop, so if you don't have another specific grain to separate web1/web2, you can try

    {% for server, addrs in salt['mine.get']('G@roles:docker' and L@web1*.domain.com', 'network.ip_addrs', expr_form='compound').items() %}
    server {{ server }} {{ addrs[0] }}:443 check inter 2000 rise 2 fall 5
    {% endfor %}
    

    Same for web2.