MOtto
MOtto

Reputation: 13

set sallt-stack custom grains using salt modules within python

I want to assign one custom grain ("__hW_raid_active") to store information if hardware (1)-or software RAID (0) is used on a minion and set it accordingly. Minions with software RAID have the directory (/proc/mdstat for mdadm). So I would use salt module file.directory.exists, which gives a boolean as return value and can be used in an if-statement.

This is the python-script I try to make it work with in /srv/salt/_grains

!/usr/bin Python
import salt.modules.file
# this is to make the module available
__salt__ = {
    'dir_exists':salt.modules.file.directory_exists
}
# Since I had errors saying module '__salt__' does not exist
# Now errors are gone but no effect on the grains list

# function:
def raiddevcheck():

    # Instantiate grains dictionary
    grains = {}

    # check it sofware RAID is on minion (we use mdadm)
    if __salt__['dir_exists']('/proc/mdstat'):
        grains["__hw_raid_active"] = 0
    else:
        grains["__hw_raid_active"] = 1

    return grains

if __name__ == '__main__':
    raiddevcheck()
salt 'minion' grains.ls

No grain named: __hw_raid_active

No errors nor from master (in debug mode_ -l debug), the minion or in /var/log/salt/master

All I see is an almost empty file "grains" just contataining {} on the minion I tested it (has hardware RAID).

Appreciate verry much any helpfull idea or am I totally on the wrong spot with the whole “custom-grain idea” managed in one central spot (_grains on master)? Do I have to copy the python script to the minons, if so where? I am pretty new to salt-stack still.

Cheers Marco

Upvotes: 1

Views: 1090

Answers (2)

OrangeDog
OrangeDog

Reputation: 38777

I had errors saying module '__salt__' does not exist

Because you were running it directly instead of via the salt loader?

If you want it to work in both, then this should work:

def raiddevcheck():
    # Instantiate grains dictionary
    grains = {}

    # check if sofware RAID is on minion (we use mdadm)
    if __salt__["file.directory_exists"]("/proc/mdstat"):
        grains["__hw_raid_active"] = 0
    else:
        grains["__hw_raid_active"] = 1

    return grains

if __name__ == "__main__":
    import salt.modules.file
    __salt__ = {
        "file.directory_exists": salt.modules.file.directory_exists
    }
    raiddevcheck()

Or use the os module instead:

import os.path

def raiddevcheck():
    # Instantiate grains dictionary
    grains = {}

    # check if sofware RAID is on minion (we use mdadm)
    if os.path.is_dir("/proc/mdstat"):
        grains["__hw_raid_active"] = 0
    else:
        grains["__hw_raid_active"] = 1

    return grains

if __name__ == "__main__":
    raiddevcheck()

Then make sure you've synced and refreshed the grains:

salt '*' saltutil.sync_grains refresh=true

Upvotes: 0

MOtto
MOtto

Reputation: 13

This is the solution:

  1. get the right python linkt in the shebang (same as salt-master) - not sure but can't hurt (did not test it)
  2. Just simple references of the method (no fancy __salt__ applied)
!/usr/bin/python3 python
import salt.modules.file

def raiddevcheck():

    # Instantiate grains directory
    grains = {}
    # check it sofware RAID is on minion (we use mdadm)
    if salt.modules.file.directory_exists('/proc/mdstat'):
       grains["__hw_raid_active"] = 0
    else:
       grains["__hw_raid_active"] = 1

    # time stamp container
    grains["__raid_last_notify"]  = 0
    grains["__smart_last_notification"] =  0

    return grains

if __name__ == '__main__':
    raiddevcheck()

Upvotes: -1

Related Questions