开始编写一个Salt模块

编写模块

简述

以ubuntu为例 salt 全部模块文件置于如下目录中:

/usr/share/pyshared/salt/

编写一个自定义的模块

在已定义的 file_roots 中,创建一个 _modules/ 目录,我们要扩展的外部模块就编写在这个目录下,(具体位置参见配置文件: /etc/salt/master)

描述一下这个模块的功能:检查 /etc/hostname /etc/resolv.conf 这两个文件的 attr 属性

/srv/salt/_modules/getattr.py

import os
import popen2

def run():
        cmd = "lsattr /etc/hostname /etc/resolv.conf"
        child=os.popen(cmd)
        data = child.read()
        return data

将模块分发到目标机器

salt '*' saltutil.sync_modules

命令执行完毕后,可以看到getattr.py被分发到minions主机的如下位置

/var/cache/salt/master/roots/hash/base/_modules/getattr.py.hash.md5
/var/cache/salt/minion/files/base/_modules/getattr.py
/var/cache/salt/minion/extmods/modules/getattr.py

执行自己编写的模块

salt '*' getattr.run

命令执行成功后返回信息如下:

salt-master:
    -------------e- /etc/hostname
    -------------e- /etc/resolv.conf

参考

A guide on how to write Salt modules

你可能感兴趣的:(开始编写一个Salt模块)