saltstack自动化运维系列⑩SaltStack二次开发初探
1、当salt运行在公网或者网络环境较差的条件下,需要配置timeout时间
vim /etc/salt/master
timeout: 60
2、salt-minion的单机模式:
修改配置文件:
# vim /etc/salt/minion
file_client: local
# /etc/init.d/salt-minion stop
本地命令测试
# salt-call --local test.ping
local:
True
# salt-call --local cmd.run 'df -Th'
[INFO ] Executing command 'df -Th' in directory '/root'
local:
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg_node2-root ext4 29G 1.1G 27G 4% /
tmpfs tmpfs 935M 0 935M 0% /dev/shm
/dev/sda1 ext4 485M 39M 421M 9% /boot
/dev/mapper/vg_node2-data ext4 29G 296M 28G 2% /data
/dev/mapper/vg_node2-usr ext4 9.7G 2.1G 7.1G 23% /usr
/dev/mapper/vg_node2-web ext4 25G 172M 23G 1% /web
# yum install python-setproctitle
该模块安装后可以通过ps更方便查看salt相关进程
ps aux | grep salt
3、简单二次开发
# mkdir /srv/salt/base/_grains
vim /srv/salt/base/_grains/my_grains.py
#!/usr/bin/env python def my_grains():
'''
my custom grains
'''
grains = {'mysite':'www.chinasoft.com','say':'life is short,i use python'}
return grains
# salt '*' saltutil.sync_grains
mini1:
node2.chinasoft.com:
- grains.my_grains
# salt '*' grains.item mysite
node2.chinasoft.com:
----------
mysite:
www.chinasoft.com
mini1:
----------
mysite:
www.chinasoft.com
[root@mini1 ~]# salt '*' grains.item say
mini1:
----------
say:
life is short,i use python
node2.chinasoft.com:
----------
say:
life is short,i use python
编写模块
# mkdir /srv/salt/base/_modules
# vim /srv/salt/base/_modules/my_disk.py
def list():
cmd = 'df -Th'
ret = __salt__['cmd.run'](cmd)
return ret
应用模块
# salt '*' saltutil.sync_modules
使用查询:
# salt '*' my_disk.list
node2.chinasoft.com:
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg_node2-root ext4 29G 1.1G 27G 4% /
tmpfs tmpfs 935M 12K 935M 1% /dev/shm
/dev/sda1 ext4 485M 39M 421M 9% /boot
/dev/mapper/vg_node2-data ext4 29G 301M 28G 2% /data
/dev/mapper/vg_node2-usr ext4 9.7G 2.1G 7.1G 23% /usr
/dev/mapper/vg_node2-web ext4 25G 172M 23G 1% /web
mini1:
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg0-root ext4 25G 1.6G 23G 7% /
tmpfs tmpfs 495M 16K 495M 1% /dev/shm
/dev/sda1 ext4 291M 39M 238M 14% /boot
/dev/mapper/vg0-usr ext4 20G 3.0G 16G 17% /usr
/dev/mapper/vg0-var ext4 9.7G 1.1G 8.2G 11% /var
4、拷贝文件到所有minion客户端
# salt '*' webmin-1.831-1.noarch.rpm /data/webmin-1.831-1.noarch.rpm