Salt common commands

       

real-time management

cmd.run way

Excuting an order

sudo salt '*' cmd.run 'uptime'

system module

For the self-built modules of the system, refer to the salt documentation  http://docs.saltstack.com/en/latest/ref/modules/all/index.html
For example, to view the disk usage of the minion, use the usage function of the disk module

sudo salt '*' disk.usage

Use the sys.doc module to query the related usage of the salt module. sys.doc is equivalent to the system man, you can query the online doc of the salt module

sudo salt '*'  sys.doc disk

custom module

The directory /srv/salt/_modules/of the custom module, the custom module path is generally /srv/salt/_modules/custom.py . Example:

$cat /srv/salt/_modules/custom.py
def test():return'i am test'

Manually sync modules to minion

sudo salt '*' saltutil.sync_modules

execution module

sudo salt '*' custom.test

Targetting

Simple Matching: Shell Style & Perl Regular Matching

model meaning
* match all minions
web1 match web1
web* Match the minion at the beginning of web
web? Match the minion with the first 4 characters of the web ID
web[1-5] match web1 to web5
web[1,3] match web1 and web3
web1-(prod|devel) match web1-prod and web1-devel

list match

sudo salt -L 'web1,web2' test.ping

Grains match

sudo salt - G 'virtual:physical'   test . ping   #match all physical machines 
sudo salt - G 'virtual:phy*'   test . ping   #grains matching method can also use Shell Style & Perl regular method

Pillar match

sudo salt -I 'master:ipv6:False' test.ping

SLS文件中的匹配方式

'virtual:physical': match: grain

States

states用于实现对minion进行状态管理,官方参考文档 http://docs.saltstack.com/ref/states/all/index.html
states 定义路径/src/salt(在/etc/salt/master中的file_roots变量定义),states文件使用YAML格式定义
states文件的后缀是sls(Salt State),sls文件编写需要注意在:之后要保留一个空格,否则会导致解析错误

手动执行state的方式,以修改admin账号的bashrc为例

准备/src/salt/bashrc.sls,内容如下

/home/admin/.bashrc:
  file.managed:- source: salt://files/bashrc- user: admin
      -group: admin
      - mode:644

准备好用于分发的bash文件,salt://files/bashrc 对应/srv/salt/files/bashrc
使bash.sls生效

sudo salt '*' state.sls 'bashrc'

Highstate的方式。其实是使用top.sls作为state的入口文件

/src/salt/top.sls文件如下,top.sls引用bashrc.sls

base:'*':- bashrc

手动执行highstate生效

sudo salt '*' state.highstate

使用schedule 让minion自动执行highstate

定义 /srv/pillar/top.sls

base:'*':- schedule

定义 /srv/pillar/schedule.sls (30分钟为单位)

schedule:
    highstate:function: state.highstate
            minutes:30

Pillar

官方文档 http://docs.saltstack.com/topics/tutorials/pillar.html
pillar数据定义路径/srv/pillar, 入口文件:/srv/pillar/top.sls
查看pillar信息

sudo salt '*' pillar.data

Grains

官方文档 http://docs.saltstack.com/topics/targeting/grains.html
查看grains分类

sudo salt '*' grains.ls

查看grains所有信息

sudo salt '*' grains.items 

查看grains某个信息

sudo salt '*' grains.item osrelease 

自定义grains

grains自定义目录/srv/salt/_grains/,自定义路径/srv/salt/_grains/grans_test.py,示例:

def grans_test():
  grains ={}
  grains['grans_test']='this is a grans test!'return grains
if __name__ =='__main__':print grans_test()

同步grains

sudo salt '*' saltutil.sync_grains

查看机器grains信息

sudo salt '*' grains.item grans_test

Job管理

Salt实时管理的任务都是作为Job来执行

查看正在执行的Job

sudo salt-run jobs.active

查看Job列表(包括执行过的)

sudo salt-run jobs.list_jobs

查看指定Job的状态

sudo salt-run jobs.lookup_jid 20140408112045976162

Minion状态管理

查看minion的状态up or down

sudo salt-run manage.status  #查看所有状态
sudo salt-run manage.up        #只看up的
sudo salt-run manage.down  #只看down

查看minion的版本,该命令会提示哪些minion的版本需要升级

sudo salt-run manage.versions

参考文档

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326567403&siteId=291194637