How to use saltstack to manage different config file for different minions?

I have two minions with httpd server managed under saltstack. VirtualHost based on ports need to be configured for them separately. So /etc/httpd/conf.d/httpd-vhost.conf is watched like this:

httpd:
  pkg.installed: []
  service.running:
    - require:
      - pkg: httpd
    - watch:
      - file: /etc/httpd/conf.d/httpd-vhosts.conf

/etc/httpd/conf.d/httpd-vhosts.conf:
  file.managed:
    - source: salt://webserver/httpd-vhosts.conf

The problem is that the two minions have their own server name and httpd-vhost.conf should be different like ServerName www.example1.com and www.example2.com. Saltstack grains module just work for .sls file not managed file. So any advice to make it work?


Just add - template: jinja to your file.managed and you can use grains in configuration files.

/etc/httpd/conf.d/httpd-vhosts.conf:
  file.managed:
    - source: salt://webserver/httpd-vhosts.conf
    - template: jinja

You can use grains in the sourced file like so:

{% if grains['id'] == 'dev' -%}
ServerName dev.example.com
{% else %}
ServerName example.com
{% endif -%}

It is this very feature that sold me on SaltStack.


You could create to state files for each server where the only difference is the source line. Of course each source has the proper config. Then in top.sls you specify the correct state file for each of the web server.