Saltstack_使用指南06_遠程執行-指定目標

来源:https://www.cnblogs.com/zhanglianghhh/archive/2019/04/08/10673508.html
-Advertisement-
Play Games

1. 主機規劃 Targeting Minions文檔 另請參見:自動化運維神器之saltstack (三)節點組及複合匹配器 註意事項 修改了master或者minion的配置文件,那麼必須重啟對應的服務。 2. 目標指定方式 Letter Match Type Example Alt Delim ...


 

1. 主機規劃

 

Targeting Minions文檔

https://docs.saltstack.com/en/latest/contents.html

 

另請參見:自動化運維神器之saltstack (三)節點組及複合匹配器

 

註意事項

修改了master或者minion的配置文件,那麼必須重啟對應的服務。

 

2. 目標指定方式

Letter

Match Type

Example

Alt Delimiter?

G

Grains glob

G@os:Ubuntu

Yes

E

PCRE Minion ID

E@web\d+\.(dev|qa|prod)\.loc

No

P

Grains PCRE

P@os:(RedHat|Fedora|CentOS)

Yes

L

List of minions

[email protected],minion3.domain.com or bl*.domain.com

No

I

Pillar glob

I@pdata:foobar

Yes

J

Pillar PCRE

J@pdata:^(foo|bar)$

Yes

S

Subnet/IP address

[email protected]/24 or [email protected]

No

R

Range cluster

R@%foo.bar

No

Matchers can be joined using boolean andor, and not operators.  【複合匹配的時候】

 

2.1. 當前有哪些minion

1 [root@salt100 ~]# salt '*' test.ping
2 salt02:
3     True
4 salt100:
5     True
6 salt03:
7     True
8 salt01:
9     True

 

3. 通過minion id匹配

3.1. 通配符匹配

在 top file 中也仍然適用。【推薦使用】

 1 # Match all minions:
 2 salt '*' test.ping
 3 
 4 # Match all minions in the example.net domain or any of the example domains:
 5 salt '*.example.net' test.ping
 6 salt '*.example.*' test.ping
 7 
 8 # Match all the 「webN」 minions in the example.net domain (web1.example.net, web2.example.net … webN.example.net):
 9 salt 'web?.example.net' test.ping
10 
11 # Match the 「web1」 through 「web5」 minions:
12 salt 'web[1-5]' test.ping
13 
14 # Match the 「web1」 and 「web3」 minions:
15 salt 'web[1,3]' test.ping
16 
17 # Match the 「web-x」, 「web-y」, and 「web-z」 minions:
18 salt 'web-[x-z]' test.ping

 

3.2. 正則表達式(-E)

使用較少,因為正則寫錯的幾率會大些。

 

正則規則參見:

https://blog.csdn.net/woshizhangliang999/article/details/46859161

 

1 # Match both 「web1-prod」 and 「web1-devel」 minions:
2 salt -E 'web1-(prod|devel)' test.ping

 

3.2.1. 在 top file 中的使用

1 base:
2   'web1-(prod|devel)':
3   - match: pcre  # 使用正則匹配
4   - webserver

 

3.3. 列表匹配(-L)

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

 

4. 使用grains指定(-G)

1 # For example, the following matches all CentOS minions:
2 salt -G 'os:CentOS' test.ping
3 
4 # Match all minions with 64-bit CPUs, and return number of CPU cores for each matching minion:
5 salt -G 'cpuarch:x86_64' grains.item num_cpus

 

4.1. 嵌套匹配【細粒度匹配】

 1 [root@salt100 ~]# salt -G 'ip_interfaces:eth0' test.ping
 2 salt01:
 3     True
 4 salt02:
 5     True
 6 salt03:
 7     True
 8 salt100:
 9     True
10 [root@salt100 ~]# salt -G 'ip_interfaces:eth0:*11*' test.ping
11 salt01:
12     True

 

5. 使用pillar指定(-I)

像grains匹配一樣,也支持嵌套匹配。

1 # 具體匹配
2 salt -I 'somekey:specialvalue' test.ping

 

5.1. 嵌套匹配【細粒度匹配】

1 [root@salt100 ~]# salt -I 'level1:level2:my_user:*zhang*' test.ping 
2 salt03:
3     True
4 salt02:
5     True

 

6. 子網/IP 地址匹配(-S)

1 # Minions can easily be matched based on IP address, or by subnet
2 salt -S 172.16.1.11 test.ping     # 具體地址
3 salt -S 172.16.1.0/24 test.ping   # 網段
4 salt -S fe80::20c:29ff:fe95:1b7a test.ping  # IPv 6 具體配置
5 salt -S 2001:db8::/64 test.ping             # IPv 6 網段配置

 

6.1. 用於複合匹配

1 # Ipcidr matching can also be used in compound matches
2 salt -C '[email protected]/24 and G@os:Debian' test.ping

 

6.2. 用於pillar和狀態的top file匹配

1 '172.16.0.0/12':
2    - match: ipcidr  # 匹配方式
3    - internal

 

7. 複合匹配(-C)

Matchers can be joined using boolean andor, and not operators.  【複合匹配的時候】

 

 1 # the following string matches all 「Debian minions」 with a hostname that begins with 「webserv」, as well as any minions that have a hostname which matches the regular expression 「web-dc1-srv.* 」:
 2 salt -C 'webserv* and G@os:Debian or E@web-dc1-srv.*' test.ping
 3 
 4 # Excluding a minion based on its ID is also possible:
 5 salt -C 'not web-dc1-srv' test.ping
 6 
 7 # Versions prior to 2015.8.0 a leading 「not」 was not supported in compound matches. Instead, something like the following was required:
 8 salt -C '* and not G@kernel:Darwin' test.ping
 9 
10 # Excluding a minion based on its ID was also possible:
11 salt -C '* and not web-dc1-srv' test.ping

 

7.1. 在 top file 中的使用

1 base:
2   'webserv* and G@os:Debian or E@web-dc1-srv.*':
3     - match: compound  # 複合匹配
4     - webserver

 

7.2. 優先匹配

1 # 可以使用括弧實現優先匹配
2 # 一定要註意括弧和目標之間需要「空格」。不遵守此規則可能導致錯誤的目標!
3 salt -C '( ms-1 or G@id:ms-3 ) and G@id:ms-3' test.ping

 

7.3. 替換分隔符

1 # 預設為 「:」 改為其他字元分割
2 salt -C 'J|@foo|bar|^foo:bar$ or J!@gitrepo!https://github.com:example/project.git' test.ping

 

案例1

 1 [root@salt100 ~]# salt -C 'G@os:redhat03' test.ping
 2 salt01:
 3     True
 4 [root@salt100 ~]# 
 5 [root@salt100 ~]# salt -C 'G|@os|redhat03' test.ping  # 將分隔符從「:」 改為「| 6 salt01:
 7     True
 8 [root@salt100 ~]# salt -C 'G!@os!redhat03' test.ping  #將分隔符從「:」 改為「! 9 salt01:
10     True
11 [root@salt100 ~]# salt -C 'G!@os!redhat03 or salt02' test.ping 
12 salt02:
13     True
14 salt01:
15     True

 

案例2

 1 [root@salt-master-7 ~]# salt '*' pillar.item getos
 2 10.0.0.112:
 3     ----------
 4     getos:
 5         ----------
 6         apache:
 7             httpd
 8         git:
 9             git
10 172.16.1.111:
11     ----------
12     getos:
13         ----------
14         apache:
15             apache2:kkk
16         git:
17             git-core
18 salt-master-7:
19     ----------
20     getos:
21         ----------
22         apache:
23             httpd
24         git:
25             git
26 [root@salt-master-7 ~]# salt -I 'getos:apache:apache2:kkk' test.ping
27 172.16.1.111:
28     True
29 [root@salt-master-7 ~]# salt -C 'I@getos:apache:apache2:kkk' test.ping    # 因為有 apache2:kkk ,所以在某些情況下會出現錯誤 
30 172.16.1.111:
31     True
32 [root@salt-master-7 ~]# 
33 [root@salt-master-7 ~]# salt -C 'I#@getos#apache#apache2:kkk' test.ping   # 表示使用 # 作為分隔符,而不是 : 
34 172.16.1.111:
35     True

 

8. 節點組(-N)

備註:

1 1、當向主配置文件添加或修改節點組時,必須重新啟動master,以便完全識別這些更改。
2 2、在不重啟的情況下,可以使用命令行中 -N 作為目標的有限功能。

 

8.1. /etc/salt/master 配置

 1 # The nodegroups master config file parameter is used to define nodegroups. Here's an example nodegroup configuration within 「/etc/salt/master」:
 2 nodegroups:
 3   group1: '[email protected],bar.domain.com,baz.domain.com or bl*.domain.com'
 4   group2: 'G@os:Debian and foo.domain.com'
 5   group3: 'G@os:Debian and N@group1'
 6   group4:
 7     - 'G@foo:bar'
 8     - 'or'
 9     - 'G@foo:baz'
10 
11 # As of the 2017.7.0 release of Salt, group names can also be prepended with a dash【破折號】. This brings the usage in line with many other areas of Salt. For example:
12 # 組節點也可以使用 如下方式。  組名前面到破折號「-13 nodegroups:
14   - group1: '[email protected],bar.domain.com,baz.domain.com or bl*.domain.com'
15 
16 註意:
17 Nodegroups可以參考group3中看到的其他Nodegroups,確保沒有迴圈引用。迴圈引用將被檢測到,並導致部分擴展產生日誌錯誤消息。
18 註意:
19     「N@」 不能在命令行和top file中使用,只能在master config 中使用

 

8.2. 命令行匹配

salt -N group1 test.ping

 

8.3. 在 top file 中的使用

1 base:
2   group1:
3     - match: nodegroup  # 使用節點組匹配
4     - webserver

 

8.4. 根據列表的minion IDs定義為節點組

 1 # 常規的定義方式
 2 nodegroups:
 3   group1: L@host1,host2,host3
 4 
 5 # YAML 定義方式
 6 nodegroups:
 7   group1:
 8     - host1
 9     - host2
10     - host3

 

9. 批量大小(-b)

1 # The 「-b」 (or 「--batch-size」) option allows commands to be executed on only a specified number of minions at a time.
2 # 同一時間執行多少 minion,支持百分比和數字。  
3 salt '*' -b 10 test.ping  # 同一時間執行 10 台,完畢後執行另外 10 台,依次執行下去
4 salt '*' -b 80% test.ping  # 同一時間執行 80% 的minion 端,完畢後執行另外 80%【實際是最後的 20%】。
5 salt -G 'os:RedHat' --batch-size 25% apache.signal restart  # 
6 
7 # --batch-wait minion返回後,等待多少秒在發送命令給新的minion
8 salt '*' -b 25% --batch-wait 5 test.ping   # 第一批minion反饋後,等待 5 秒後,在發送命令給下一批的minion。

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 最近想給自己的代辦清單任務微信小程式想加個語音識別識別功能,廢話不多說,直接說重點,語音識別使用的是百度語音識別api,因為微信小程式的錄音輸入文件目前只能是mp3或aac 但是百度語音識別不支持這兩種(百度api介面文檔上有說明),所以需要把音頻格式轉換一下,我這邊使用的是Alvas.Audio. ...
  • Docker的volume捲 為了能持久話保存和共用容器的數據。 使用docker volume捲的兩種方式 1:數據捲 2:數據捲容器 1:數據捲 數據捲:數據捲會繞過docker 的ufs 直接寫在物理設備上,對數據捲的操作可以及時生效,並且數據捲是可以進行共用重用的,為一個或者多個容器提供訪問 ...
  • 轉自:https://mp.weixin.qq.com/s/iwtdISME4VotLgnuAhtflQ 很多公司技術支持崗位的工作,如配置功能變數名稱,部署環境,修改複位配置,服務重啟,擴容縮容,梳理和完善監控,根據開發的需要查找日誌等工作,需要和開發進行大量的溝通,如什麼是外網功能變數名稱,什麼是內網功能變數名稱、A ...
  • 原文鏈接:Create media for automated unattended install of Windows 10 我從來沒看到過像上面的文章一樣這麼詳細的描述過Windows10的無人值守安裝過程,看完長知識了,值得一看。之後我會將自己的製作過程發出來,讓大家學習一下。 只為了學習, ...
  • 1.使用tree命令查看根目錄的樹結構 如果沒有tree命令,可以使用yum進行安裝 執行命令後,即可看到根下一共有19個目錄 . 當前目錄 / 根目錄 /bin 存放必要的命令; 軟連接 /usr/bin /boot 存放內核以及啟動所需的文件; /dev 存放硬體設備文件 /etc 存放系統配置 ...
  • 1. 主機規劃 遠程執行教程文檔 所有模塊文檔 模塊在機器上存在的位置 註意事項 2. 使用格式 2.1. 指定目標 上一篇文章詳細說過,這裡簡單說下 2.2. 指定執行模塊 2.3. 執行參數 3. 使用示例 3.1. network 3.2. service 3.3. cp【可使用Salt-cp ...
  • Windows 使用 L2TP 協議連接VPN服務 首先, 點擊"添加 VPN 連接" 其次, 選擇 L2TP 協議 此時若嘗試連接發現會失敗, 打開網路共用中心, 會發現多了一個 VPN 的連接, 右鍵屬性 勾選相應選項, 再嘗試連接即可成功. ...
  • 先說BUG,最近要做項目需要樹莓派和陀螺儀,資金充足的話肯定是買一個硬體卡爾曼濾波的感測器類似JY901模塊,資金不足的就買MPU6050。 網上關於MPU6050在樹莓派上的代碼還能用,關於JY901的代碼真的是千奇百怪,而且複製現象特別嚴重,有很多系統本身有問題,導致很多像我一樣的新手在上面浪費 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...