天天看點

saltstack使用指南:遠端管理minion

通過saltstack遠端管理minion

1.遠端執行指令的組成結構
2.如何使用多種方法定位和比對minion伺服器
           

一、遠端執行指令的組成結構

[[email protected] ~]# salt --help
Usage: salt [options] '<target>' <function> [arguments]

第一部分:salt指令本身
第二部分:指令行選項
第三部分:目标定位字元串
第四部分:salt子產品函數
第五部分:遠端執行函數的參數

* :代表任意字元串,也可以是空字元串
?:代表一個字元,但是不可以為空
[] :字元集合,例如[a-z]代表任何一個小寫字母,[0-9]代表數字

1.1 指令行選項
salt指令行選項的作用:通過不同的選項來改變程式本身的行為。

執行個體一:
[[email protected] ~]# salt -v '*' cmd.run 'free -m'
Executing job with jid 20180516144835766360
-------------------------------------------

izwz9f8xrvty50quc2gq50z:
                  total        used        free      shared  buff/cache   available
    Mem:           1839          97         609           0        1132        1574
    Swap:             0           0           0
iZbp150ikdomqe3b32qaubZ:
                  total        used        free      shared  buff/cache   available
    Mem:           1839         106         603           0        1128        1564
    Swap:             0           0           0

注:-v或--verbose:開啟指令的較長的描述---較長的描述指令運作後會發生什麼。


執行個體二:
[[email protected] ~]# salt --summary '*' cmd.run 'free -m'
izwz9f8xrvty50quc2gq50z:
                  total        used        free      shared  buff/cache   available
    Mem:           1839          97         609           0        1132        1574
    Swap:             0           0           0
iZbp150ikdomqe3b32qaubZ:
                  total        used        free      shared  buff/cache   available
    Mem:           1839         106         603           0        1128        1564
    Swap:             0           0           0


-------------------------------------------
Summary
-------------------------------------------
# of Minions Targeted: 2
# of Minions Returned: 2
# of Minions Did Not Return: 0
-------------------------------------------

注:--summary:顯示一條salt指令的概要。


執行個體三:
[[email protected] ~]# salt --out=json '*' cmd.run 'free -m'
{
    "izwz9f8xrvty50quc2gq50z": "              total        used        free      shared  buff/cache   available\nMem:           1839          97         609           0        1132        1574\nSwap:             0           0           0"
}
{
    "iZbp150ikdomqe3b32qaubZ": "              total        used        free      shared  buff/cache   available\nMem:           1839         106         603           0        1128        1565\nSwap:             0           0           0"
}

注:--out:控制salt執行後輸出結構的格式。
           

二、如何使用多種方法定位和比對minion伺服器

2.1 全局比對
[[email protected] ~]# salt '*' test.ping
izwz9f8xrvty50quc2gq50z:
    True
iZbp150ikdomqe3b32qaubZ:
    True

2.2 正則比對

後續會把正則規則總結出來,待後續補充......

注:salt使用python的re正則表達子產品,可以解析正規表達式。

2.3 清單比對
[[email protected] ~]# salt -L 'izwz9f8xrvty50quc2gq50z' test.ping
izwz9f8xrvty50quc2gq50z:
    True
注:比對多個主機,指令:salt -L 'izwz9f8xrvty50quc2gq50z,iZbp150ikdomqe3b32qaubZ' test.ping

2.4 minion端分組比對
[[email protected] ~]# vim /etc/salt/master  
nodegroups:  
  sa: '[email protected],10.0.10.101'  
  sb: '[email protected]/24'  

進行分組操作:
salt -N sa test.ping  
注:-N或--nodegroup表示分組比對。

2.5 網段/IP比對

網段比對
[[email protected] ~]# salt -S '172.25.16.0/24' test.ping
172-25-16-223:
    True
172-25-16-176:
    True
172-25-16-126:
    True

IP比對
[[email protected] ~]# salt -S '172.25.16.171' test.ping
172-25-16-171:
    True
注:-S表示網段IP比對。

2.6 grains比對

Grains可以認為是描述minion本身固有屬性的靜态資料。例如minion伺服器的作業系統版本、系統大小、網卡的MAC位址等

測試全部比對為centos系統的網絡聯通性
[[email protected] ~]# salt -G os:centos test.ping
172.25.34.20:
    True
172.25.34.9:
    True
172.25.34.19:
    True

定位系統版本是6.5的主機
[[email protected] ~]# salt -G 'osrelease:6.5' test.ping
192-168-2-235:
    True
192-168-2-67:
    True
172-25-2-145:
    True

定位網卡的實體位址的主機
[[email protected] ~]# salt -G 'hwaddr_interfaces:eth0:00:16:3e:08:1e:78' test.ping
izwz9f8xrvty50quc2gq50z:
    True

 查詢所有的item
[[email protected] pillar]# salt '*' grains.items
省去一萬行.....

查詢minion端作業系統
[[email protected] pillar]# salt '*' grains.item os
izwz9f8xrvty50quc2gq50z:
    ----------
    os:
        CentOS
iZbp150ikdomqe3b32qaubZ:
    ----------
    os:
        CentOS

注:-G:表示grains比對。

2.7 pillars比對
後續不斷完善中......

擴充:grains與pillar之間的差別?
1.grains資料通常是minion端的一些特性資料,比如作業系統類型,磁盤,記憶體資訊等,它是存儲在minion端的,緩存在master端。
2.pillar資料存儲在master端,用戶端有緩存。pillar資料可以通過salt '*' saltutil.refresh_pillar來重新整理pillar資料。
3.grains和pillar都是以key value形式的資料庫。
4.minion有權限操作自己的grains值,如增加、删除,但minion隻能檢視自己的pillar,無權修改。