Saltstack 操作目標,正則匹配,及組管理


 

   如果我們要維護好一個龐大的配置管理系統那么首選得維護好我們的管理對象,在saltstack系統中我們的管理對象叫做Target,

在master上我們可以采用不同Target去管理不同的Minion。這些Target都是通過去管理和匹配Minion的ID來做的一些集

  • 操作目標
  • 正則匹配
  • 組管理

 

操作目標

   借用別人博客上面的內容 貼在下面

1.匹配Minions Id

匹配所有 (*)

[root@node1 salt]# salt '*' test.ping
node2.minion:
True

匹配后面是.minion的

[root@node1 salt]# salt '*.minion' test.ping
node2.minion:
True

匹配一個(?)

[root@node1 salt]# salt '*node?.minion' test.ping
node2.minion:
True

匹配多個[1-5]

[root@node1 salt]# salt 'node[1-5].minion' test.ping
node2.minion:
True

 

匹配某個主機和某個

[root@node1 salt]# salt 'node[2,3].minion' test.ping
node2.minion:
True

 

匹配a-z

[root@node1 salt]# salt 'node2.minio[n-z]' test.ping
node2.minion:
True

 

正則匹配

命令格式: salt ‘<操作目標>’ <方法>[參數]

 

示例: 查看被控主機的內存使用情況

[root@hzbj-salt-020 ~]# salt 'hzbj-tomcat-021' cmd.run 'free -m' hzbj-tomcat-021: total used free shared buffers cached Mem: 980        406        574          0         17        257
    -/+ buffers/cache:        131        849 Swap: 1983          0       1983

 

    其中針對(操作目標),Saltstack提供了多種方法對被控端主機(id)進行過濾。下面列舉常用的具體參數。

 

1)-E,--pcre,通過正則表達式進行匹配。示例:比如hzbj字符開頭的主機id名是否連通,命令:

[root@hzbj-salt-020 ~]# salt -E '^hzbj.*' test.ping hzbj-tomcat-021: True hzbj-tomcat-022: True

 

2)-L,--list,以主機id名列表的形式進行過濾,格式與Python的列表相似,即不同主機id名稱使用逗號分隔,命令:

[root@hzbj-salt-020 ~]# salt -L 'hzbj-tomcat-021,hzbj-tomcat-022' grains.item osfullname hzbj-tomcat-021: ---------- osfullname: CentOS hzbj-tomcat-022: ---------- osfullname: CentOS

 

3)-S,--ipcidr,根據被控主機的IP地址或IP子網進行匹配,示例如下:

[root@hzbj-salt-020 ~]# salt -S 192.168.0.0/16 test.ping [root@hzbj-salt-020 ~]# salt -S 192.168.56.0/24 test.ping [root@hzbj-salt-020 ~]# salt -S 192.168.56.21 test.ping

 

 

組管理

根據主控端master配置文件中的分組名稱進行過濾。 組(主機信息支持正則表達式,grain,條件運算符等),通常根據業務類型划分,不同業務舉辦相同的特點,包含部署環境,應用平台,配置文件等。

[root@hzbj-salt-020 ~]# vim /etc/salt/master nodegroups: web1group: 'L@hzbj-tomcat-021' web2group: 'L@hzbj-tomcat-022'

其中,L@表示后面的主機id格式為列表,即主機id以逗號分隔;G@表示以grain格式描述;S@表示以IP子網或地址格式描述。

 

示例: 探測web2group被控主機的連通性,命令:

[root@hzbj-salt-020 ~]# salt -N web2group test.ping hzbj-tomcat-022: True

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM