4

I'm rewriting some modules from puppet to saltstack.

In puppet we can use node to specific this part is for this machine, for example:

node /william\d+.aws.dev/ {
  # some codes here.. 
}

But in saltstack, it's not that much elegant:

{% if grains['fqdn'] == 'william.aws.dev' %}
  # some codes here..
{% endif %}

and regular expression is not supported.

Is there any way to rewrite it in saltstack using less code?


Note I don't want to use top.sls to define which node using which sls. because it would make the top.sls file too large to maintain.

I just want to define a simple two line in top.sls:

'*':
  - node.*

everytime I add some node in salt, I just need to create a new file under the node directory.

Felix Frank
  • 8,125
  • 1
  • 23
  • 30
Ethan Xu
  • 143
  • 1
  • 7

1 Answers1

1

I would still recommend assigning SLSes to nodes in your topfile, simply because the way you have it set up right now, every node gets served a copy of every single SLS under node/ directory, with the logic of which states to run encoded in the SLSes themselves. This seems a bit messy to me, considering that you can do all of this logic and regex related matching in your top.sls.

everytime I add some node in salt, I just need to create a new file under the node directory.

OK, you can get this functionality by using only top.sls while still keeping it at a manageable size. It seems that your intent is to have each node get its own SLS file, and to assign nodes to the SLS matching their fqdn. If that is the case, then take a look at this example top.sls, tweaked from the salt grains documentation

{% set grain_fqdn = salt['grains.get']('fqdn', '') %}

base:
  'fqdn:{{ grain_fqdn }}':
      - match: grain  
      - node.{{ grain_fqdn }}

This will ensure that the node with grain fqdn = williams.aws.dev get's assigned the node.williams.aws.dev.sls state file. Note your match here can use any sort of regex or compound matching that salt allows: http://docs.saltstack.com/en/latest/topics/targeting/

talwai
  • 59
  • 5
  • but if there is not sls name "node.williams.aws.dev.sls" would cause error. because I don't need all nodes have sls file. – Ethan Xu Feb 02 '15 at 05:53