Search code examples
amazon-web-servicessalt-project

How do I allow a salt stack formula to run on only certain operating system versions?


I'm writing a SaltStack formula for installation of AWS Application Discovery Service.

The install script requires certain OS versions - how do I account for that in my formula?

The map.jinja in the redis formula does the following - is this the correct way?

{% set os_map = salt['grains.filter_by']({
    'Debian': {
        'pkg_name': 'redis-server',
        'svc_name': 'redis-server',
        'cfg_name': '/etc/redis/redis.conf',
        'cfg_version': salt['grains.filter_by']({
            'wheezy': '2.4',
            'jessie': '2.8',
            'default': '2.8'
}, grain='oscodename'), 

OS Requirements:

  • Ubuntu 14
  • Amazon Linux 2012.03 or 2015.03
  • Centos 6 or 7
  • Redhat 6 or 7

Solution

  • The mapping is needed when you have different values on different OSes, take a look at the following example from the documentation, map.jinja part in your case you may use the following for example:

    {% set mysql = salt['grains.filter_by']({
        'Debian': {
            'server': 'mysql-server',
            'client': 'mysql-client',
            'service': 'mysql',
            'config': '/etc/mysql/my.cnf',
            'python': 'python-mysqldb',
        },
        'RedHat': {
            'server': 'mysql-server',
            'client': 'mysql',
            'service': 'mysqld',
            'config': '/etc/my.cnf',
            'python': 'MySQL-python',
        },
        'Gentoo': {
            'server': 'dev-db/mysql',
            'client': 'dev-db/mysql',
            'service': 'mysql',
            'config': '/etc/mysql/my.cnf',
            'python': 'dev-python/mysql-python',
        },
    }) %}
    

    The above code will use the values of server, client, service, etc according to the OS they will be running on Note the I have removed the following merge=salt['pillar.get']('mysql:lookup')

    Another example when you need to start apache service on Ubuntu named apache2 and on Centos named httpd so you need a lookup table to handle different paths and services name.

    I didn't have a chance before to try a formula on a random OS other than OSes defined under map.jinja but i think it will be broken.

    In order to prevent the formula from running on Unwanted OS you have to wrap your code between if statement for example one of your requirement is CentOS 6 or CentOS 7 so what About CentOS 5? You can do the following:

    # This will be matched with CentOS/Redhat 6 or higher
    {% if grains.os_family == 'RedHat' and grains.osmajorrelease >= '6' %}
    # do something
    {% endif %}
    

    And you can use elif to add more conditions, take a look to get more information about using jinja in states And DON'T Forget to check the grains of your minions in order to add conditions in a correct way to make it work as expected.