Python API简单使用
第一条命令
/usr/bin/salt默认使用的接口是LocalClient,该接口只能在salt master上使用
>>> import salt.client
>>> local = salt.client.LocalClient()
>>> local.cmd('*','cmd.run',['hostname'])
{'slave': 'slave'}
逐条返回结果
>>> ret = local.cmd_iter('*','test.ping')
>>> for i in ret:
... print i
...
{'slave': {'retcode': 0, 'ret': True}}
检查minion服务器信息
>>> local.cmd('*','cmd.run',['hostname'])
{'slave': 'slave'}
>>> local.cmd('*','cmd.run',['ifconfig'])
{'slave': 'ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.43.234 netmask 255.255.255.0 broadcast 192.168.43.255
ether 00:0c:29:92:1c:db txqueuelen 1000 (Ethernet)
RX packets 57041 bytes 70741421 (67.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 39561 bytes 4756727 (4.5 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1 (Local Loopback)
RX packets 4 bytes 340 (340.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4 bytes 340 (340.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0'}
>>> local.cmd('*','cmd.run',['crontab -l'])
{'slave': 'no crontab for root'}
>>> local.cmd('*','cmd.run',['df -h'])
{'slave': 'Filesystem Size Used Avail Use% Mounted on
/dev/mapper/cl-root 17G 1.5G 16G 9% /
devtmpfs 982M 0 982M 0% /dev
tmpfs 993M 12K 993M 1% /dev/shm
tmpfs 993M 8.5M 984M 1% /run
tmpfs 993M 0 993M 0% /sys/fs/cgroup
/dev/sda1 1014M 121M 894M 12% /boot
tmpfs 199M 0 199M 0% /run/user/0'}
控制minion的服务
>>> local.cmd('*','service.stop',['nginx'])
{'slave': True}
>>> local.cmd('*','service.status',['nginx'])
{'slave': False}
>>> local.cmd('*','service.start',['nginx'])
{'slave': True}
>>> local.cmd('*','service.status',['nginx'])
{'slave': True}