Dynamically get grains for minions that match a (grain) role

414 views
Skip to first unread message

Nikolas Karavias

unread,
Jul 10, 2015, 12:01:04 PM7/10/15
to salt-...@googlegroups.com
I'm trying to do something really simple, but apparently it's not working out. I have a list of roles on my minions (passed via pillar) and I want to be able to query the grains of all minions that match a role.

My pillar top.sls:

base:
  '*':
    - default

  'roles:zookeeper':
    - mine
    - match: grain
    - zookeeper

All zookeeper nodes have the following minion config:

master: master02
grains:
  roles:
    - zookeeper

The "mine" pillar state looks like this:

mine_functions:
  test.ping: []
  grains.items: []
  pillar.items: []
  network.interfaces: []


Inside a file i want to generate a string for all minions that match 'roles:zookeeper' grain:

{%- set data = salt['mine.get']('roles:zookeeper', 'grains.items', 'grain') %}
{%- for node in data -%}
{{  node }}
{% endfor %}

The file after salt runs looks like this:

zoo01
zoo02..
... the rest of my minions that match 'roles:zookeeper'


  1. Am I overcomplicating things? This sounds like overkill for just wanting to dynamically get grains (at least the id) for minions that match a role...
  2. How do I get the output to be inline (zoo01,zoo02....) instead of having \n on each item getting printed?




Stephen Spencer

unread,
Jul 11, 2015, 4:49:22 PM7/11/15
to salt-...@googlegroups.com

What version of salt are you using?

-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.

Nikolas Karavias

unread,
Jul 11, 2015, 5:59:53 PM7/11/15
to salt-...@googlegroups.com
2015.5.2 

Paul Reinking

unread,
Jul 13, 2015, 1:31:09 AM7/13/15
to salt-...@googlegroups.com
Hi Nikolas,

I think I get what your are trying to do and this snippet list all zookeepers that below to the same customer and environment on my systems.

# List all Zookeeper nodes within this cluster
{% for server, addrs in salt['mine.get']('G@customer:' ~ grains.customer ~ ' and G@environment:' ~ grains.environment ~ ' and G@roles:zok and G@cluster:' ~ grains.cluster, 'ip_addrs', expr_form='compound')|dictsort %}
server.{{loop.index}}={{addrs[0]}}:2888:3888
{% endfor %}

Paul

Op 11-07-15 om 23:59 schreef Nikolas Karavias:
--

 

Kind regards,

Paul Reinking
 
CONFIDENTIALITY NOTE: The information transmitted, including attachments, is intended only for the person(s) or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and destroy any copies of this information.

Nikolas Karavias

unread,
Jul 28, 2015, 8:32:37 AM7/28/15
to Salt-users, pa...@reinking-online.nl
How would you get that to work by returning a list of strings? It seems that I can't iterate over a list and insert the items into another string.

In my previous example:

{%- set data = salt['mine.get']('roles:zookeeper', 'grains.items', 'grain') %}
{%- for node in data -%}
zookeepers={{  node }}
{% endfor %}

I want to be able to get zookeepers=node1,node2,node3 instead of 

zookeepers=node1
zookeepers=node2
zookeepers=node3

Nikolas Karavias

unread,
Jul 28, 2015, 9:42:22 AM7/28/15
to salt-...@googlegroups.com
A little jinja RTFM worked...

 {%- set proxy= [] %}
 {%- for host, hostinfo in salt['mine.get']('G@roles:some_role and G@datacenter:env', 'network.interfaces','compound').items() %}
 {%- do proxy.append(host) %}
 {%- endfor %}
 proxy={{ proxy }}


--
You received this message because you are subscribed to a topic in the Google Groups "Salt-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/salt-users/SZvewwgJ2Aw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to salt-users+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages