02 . SaltStack高階用法(Python API)

men發表於2020-07-06

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}

相關文章