Some help needed using salt.mine for haproxy template

63 views
Skip to first unread message

Paul Reinking

unread,
Jun 27, 2015, 4:46:23 AM6/27/15
to salt-...@googlegroups.com
Hi,

I'm building a template for haproxy and wanted to iterate over addresses
based on a grains match

{% for server, addrs in salt['mine.get']('G@roles:sql and G@cluster:01',
'sql_ip_addrs', expr_form='compound')|dictsort %}
server {{ server }} {{ addrs[0] }}:3306 weight 10 check port 3306
inter 10s maxconn 40
{% endfor %}

I would like to replace `G@cluster:01` with something like
`G@cluster:grains[cluster]` so it automatically matches the cluster id
that I'm rolling out.
Unfortunately I have not yet found the correct syntax and would like to
ask some help for this.

Also it would be nice if I could somehow loop over all items except the
last one?

paul

Stephen Spencer

unread,
Jun 28, 2015, 12:34:11 AM6/28/15
to salt-...@googlegroups.com

This should do the trick:

'G@cluster:{0}'.format(grains.cluster)
(pythonic)

or

'G@cluster:' ~ grains.cluster
(jinjaic)

-S

--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paul Reinking

unread,
Jun 28, 2015, 3:46:15 AM6/28/15
to salt-...@googlegroups.com
Hi Steven,

Many thanks for your help and below a snippet of the code I created.
I managed as well to skip the last entry in the dictionary by referring
to the last line of the sorted dictionary with [-1]

{% set nodes = salt['mine.get']('G@customer:'~grains.customer and
'G@environment:'~grains.environment and 'G@roles:sql and G@cluster:01',
'repl_ip_addrs', expr_form='compound')|dictsort %}
{% for server, addrs in nodes %}
{% if server != nodes[-1][0] %}
server {{ server }} {{ addrs[0] }}:3306 weight 10 check port 3306
inter 10s maxconn 40
{% endif %}
{% endfor %}

Paul

Paul Reinking

unread,
Jun 28, 2015, 5:34:18 AM6/28/15
to salt-...@googlegroups.com
Hi Steven,
.

The syntax is working but the comparison is always true, regardless of
what the grains.environment might be

Op 28-06-15 om 09:46 schreef Paul Reinking:

Paul Reinking

unread,
Jun 28, 2015, 7:16:05 AM6/28/15
to salt-...@googlegroups.com
Hi,

With the help of Steven In managed to get the following working

{% for server, addrs in salt['mine.get']('G@environment:production and
G@cluster:'~grains.cluster, 'ip_addrs', expr_form='compound')|dictsort %}

Its seems however that the compound expression fails in the next
statement as you only get the result of the last comparison and the
`and` is ignored

{% for server, addrs in
salt['mine.get']('G@environment:'~grains.environment and
G@cluster:'~grains.cluster, 'ip_addrs', expr_form='compound')|dictsort %}

Is there a way to work around this?

Paul

Op 27-06-15 om 10:46 schreef Paul Reinking:

Stephen Spencer

unread,
Jun 28, 2015, 11:04:14 AM6/28/15
to salt-...@googlegroups.com
Try expressing the last target thusly:

 'G@environment:' ~ grains.environment ~ ' and G@cluster:' ~ grains.cluster

The tilde is just a string concatenator in jinja, so you need to quote whatever isn't a variable. I like the pythonic variation for that very reason--it would become:

'G@environment:{0} and G@cluster:{1}'.format(grains.environment, grains.cluster)

You'll need to be mindful of the spaces as well. If you haven't discovered it yet, a good (ghetto) way of debugging jinja logic is to run your jinja statements via salt-call on one of the target minions setting the log level to debug from the command line

e.g.:

given the state file /srv/salt/test/test.sls

{% set things = salt.mine.get(
      'I@subcomponent:stats and I@location:dfw or I@location:ord and I@environment:' ~•
          pillar.environment,•
      'external_interface',•
      expr_form='compound') %}

# things {{things}}
# {{things.keys()}}

Then from the minion:

salt-call -l debug state.sls test.test

<2-3 dozen debug statements>
[DEBUG   ] Rendered data from file: /var/cache/salt/minion/files/base/test/test.sls:

# things {'stats-01.log.some.000.lab.rackspace.dfw': {'src': '166.78.63.174/24', 'host': '166.78.63.174/24', 'cluster_id': '000', 'dev': 'eth0'}}
# ['stats-01.log.some.000.lab.rackspace.dfw']

[DEBUG   ] Results of YAML rendering:
{}

-S

--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
You know, I used to think it was awful that life was so unfair. Then I thought, wouldn't it be much worse if life were fair, and all the terrible things that happen to us come because we actually deserve them? So, now I take great comfort in the general hostility and unfairness of the universe.

Paul Reinking

unread,
Jun 29, 2015, 2:47:40 AM6/29/15
to salt-...@googlegroups.com
Hi Steven,

Thanks, now I understand how you came to the complete strings and the end-result is :

Op 28-06-15 om 17:04 schreef Stephen Spencer:
Reply all
Reply to author
Forward
0 new messages