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\n inet 192.168.43.234 netmask 255.255.255.0 broadcast 192.168.43.255\n ether 00:0c:29:92:1c:db txqueuelen 1000 (Ethernet)\n RX packets 57041 bytes 70741421 (67.4 MiB)\n RX errors 0 dropped 0 overruns 0 frame 0\n TX packets 39561 bytes 4756727 (4.5 MiB)\n TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0\n\nlo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536\n inet 127.0.0.1 netmask 255.0.0.0\n loop txqueuelen 1 (Local Loopback)\n RX packets 4 bytes 340 (340.0 B)\n RX errors 0 dropped 0 overruns 0 frame 0\n TX packets 4 bytes 340 (340.0 B)\n 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\n/dev/mapper/cl-root 17G 1.5G 16G 9% /\ndevtmpfs 982M 0 982M 0% /dev\ntmpfs 993M 12K 993M 1% /dev/shm\ntmpfs 993M 8.5M 984M 1% /run\ntmpfs 993M 0 993M 0% /sys/fs/cgroup\n/dev/sda1 1014M 121M 894M 12% /boot\ntmpfs 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}