Saturday, 12 November 2016

Getting started with Saltstack part 3 (Writing custom modules)

Apart from the plethora of execution modules already available, Salt gives us the tools to write our own custom modules as well. Salt uses python under the hood to provide all the system administration functionality features distributed in the form of execution modules. We can leverage these existing salt modules by calling them in our custom made module.  When Salt loads all of the execution modules, it creates a dictionary with references to each execution module function. This dictionary is available as __salt__.

I've written one such custom module to illustrate this feature & will describe it's usage in this article.

Salt looks in directory path /srv/salt/_modules for custom modules to sync & use. These custom modules are essentially python scripts.

Let's get started by creating the directory path:

mkdir /srv/salt/_modules
cd /srv/salt/_modules

Now we write our script named atest.py

[root@cserver _modules]# cat atest.py
def reserv():
    '''
    Test module to reload a service
    CLI Example:
    .. code-block:: bash
    salt '*' test_comm.reserv
    '''
    source = '/etc/ssh/ssh_config'
    destination= '/tmp/'
    copy_file = __salt__['rsync.rsync'] (source, destination)
    ser_name = 'sshd'
    test_comm = __salt__['service.reload'](ser_name)
    return copy_file
    return test_comm


The above script will do two things. It'll copy the ssh_config file to the /tmp directory & reload the ssh service across the minions on which it is being executed.
The script is using the rsync & service modules.
The ''' are used to enclose text that will serve as documentation if someone runs sys.doc against the custom module.

After writing the script we need to sync it across the minions by typing saltutils.sync_all;

[root@cserver _modules]# salt '*' saltutil.sync_all
firstminion:
    ----------
    beacons:
    engines:
    grains:
    log_handlers:
    modules:
        - modules.atest
    output:
    proxymodules:
    renderers:
    returners:
    sdb:
    states:
    utils:
secondminion:
    ----------
    beacons:
    engines:
    grains:
    log_handlers:
    modules:
        - modules.atest
    output:
    proxymodules:
    renderers:
    returners:
    sdb:
    states:
    utils:


We need to run this command while we are in the /srv/salt/_modules directory.

After the sync is complete we can test the module by running it across minions:

[root@cserver _modules]# salt '*' atest.reserv
secondminion:
    ----------
    pid:
        16678
    retcode:
        0
    stderr:
    stdout:
        sending incremental file list
        ssh_config

        sent 1132 bytes  received 31 bytes  2326.00 bytes/sec
        total size is 2123  speedup is 1.83
firstminion:
    ----------
    pid:
        51866
    retcode:
        0
    stderr:
    stdout:
        sending incremental file list
        ssh_config

        sent 1132 bytes  received 31 bytes  2326.00 bytes/sec
        total size is 2123  speedup is 1.83


& it works!

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...