Code Help - iterate through roles/grains and deploy file if matching grain exists

705 views
Skip to first unread message

Brent Nikolaus

unread,
Jan 23, 2015, 1:31:40 AM1/23/15
to salt-...@googlegroups.com
Hi,

I am trying to iterate through a custom grain that I has multiple values associated with it. I am trying to get my code to loop through all the roles, If one of the roles is rabbitmq, then deploy the file, thus not deploying the file they don't match. The grain is called roles and its implemented in the /etc/salt/minion config like below.

 53   roles:
 54     - rabbitmq
 55     - sensu-master
 56     - salt-master
 57   env: devops

This article is a good read on managing roles and comes close to my use case, except I don't use the value's being iterated over just perform the task if if value exists.
=> https://thiggy.wordpress.com/2013/09/19/role-based-state-management-with-saltstack/

If anyone could tell me what I am doing wrong or provide useful insight, perhaps I will buy you beer at SaltConf. ;)

My Code
 21 {% for role in salt['grains.get']('roles', []) %}
 22 {% if role == 'rabbitmq' %}
 23
 24 /etc/diamond/collectors.salt/RabbitMQCollector.conf
 25   file.managed:
 26     - source: salt://metrics/diamond/files/diamond/collectors/RabbitMQCollector.conf
 27
 28 {% endif %}
 29 {% endfor %}

Error Message
cilrtdms0001.sys.cigna.com:
    Data failed to compile:
----------
    Rendering SLS 'base:metrics.diamond.collectors' failed: Unknown yaml render error; line 27

---
[...]




/etc/diamond/collectors.salt/RabbitMQCollector.conf
  file.managed:    <======================
    - source: salt://metrics/diamond/files/diamond/collectors/RabbitMQCollector.conf




---


Any help would appreciated.

Dmitry Golubenko

unread,
Jan 23, 2015, 2:37:17 AM1/23/15
to salt-...@googlegroups.com
В Чтв, 22/01/2015 в 22:31 -0800, Brent Nikolaus пишет:
> Hi,
>
> I am trying to iterate through a custom grain that I has multiple
> values associated with it. I am trying to get my code to loop through
> all the roles, If one of the roles is rabbitmq, then deploy the file,
> thus not deploying the file they don't match. The grain is called
> roles and its implemented in the /etc/salt/minion config like below.
>
> 53 roles:
> 54 - rabbitmq
> 55 - sensu-master
> 56 - salt-master
> 57 env: devops
>
> This article is a good read on managing roles and comes close to my
> use case, except I don't use the value's being iterated over just
> perform the task if if value exists.
try something like this:
{% if 'rabbitmq' in grains['roles'] %}
24 /etc/diamond/collectors.salt/RabbitMQCollector.conf
25 file.managed:
26 - source:
salt://metrics/diamond/files/diamond/collectors/RabbitMQCollector.conf
{% endif %}

not very clean way when you have 10+ roles, but sometime useful to match
minions in topfile, e.g.
{% if 'apache' in grains['roles'] %}
- apache
{% endif %}
{% if 'nginx' in grains['roles'] %}
- nginx
{% endif %}
> --
> 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.



Nicholas Capo

unread,
Jan 23, 2015, 10:11:41 AM1/23/15
to salt-...@googlegroups.com

/etc/diamond/collectors.salt/RabbitMQCollector.conf
  file.managed:    <======================
    - source: salt://metrics/diamond/files/diamond/collectors/RabbitMQCollector.conf

I think you are missing a colon after your id.

/etc/diamond/collectors.salt/RabbitMQCollector.conf: <<-------- here
  file.managed:

I would also recommend putting this logic in the top file. As our states have grown the complexity keeps increasing. It also make it easier to test (I use a vagrant vm for testing and development of states), since you can just apply it directly without setting up any grains first, on the other hand you have to be careful when calling state.sls.

Nicholas



> For more options, visit https://groups.google.com/d/optout.



--
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+unsubscribe@googlegroups.com.

Brent Nikolaus

unread,
Jan 23, 2015, 10:13:49 PM1/23/15
to salt-...@googlegroups.com
I did managed to fix the missing colon and the logic seemed to work fine. I appreciate the extra set of eyes. :)

Thanks Everyone for the assistance.
- Brent

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/jZWBwTAlkBk/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