saltstack的状态文件
生活随笔
收集整理的這篇文章主要介紹了
saltstack的状态文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
saltstack狀態文件設定:
編輯/etc/salt/master,修改其中關于“設置文件的目錄”的設置:
說明:注意語法格式,頂格/冒號/兩個空格
進入base環境下,并配置下top.sls
說明:base是指定一個名稱,init為文件夾的名稱,pkg為pkg.sls
案例1:使用salt初始化系統模塊:
[root@master?init]#?salt?'*'?state.sls?init.pkg node01.saltstack.com: ----------ID:?pkg.initFunction:?pkg.installedName:?mtrResult:?TrueComment:?Package?mtr?is?already?installed.Started:?14:56:02.574416Duration:?11389.014?msChanges:??? ----------ID:?pkg.initFunction:?pkg.installedName:?nmapResult:?TrueComment:?Package?nmap?is?already?installed.Started:?14:56:13.963968Duration:?3.619?msChanges:??? ----------ID:?pkg.initFunction:?pkg.installedName:?lrzszResult:?TrueComment:?Package?lrzsz?is?already?installed.Started:?14:56:13.967979Duration:?1.042?msChanges:??? Summary ------------ Succeeded:?3 Failed:????0 ------------ Total?states?run:?????3案例2:saltstack修改內核參數:
案例3:同步某個計劃任務
最近發現很多服務器上沒有配置ntp服務器指向,簡單寫個計劃任務,然后通過狀態文件下發 思路: a)準備好需要下發的文件 b)編輯sls文件 c)修改top.sls,添加信息進去 [root@master?~]#?cat?/var/spool/cron/root? */5?*?*?*?*?/usr/sbin/ntpdate?-u?202.120.2.101>/dev/null?2>&1 [root@master?~]#?cd?/etc/salt/states/ [root@master?states]#?ls init??prod??top.sls [root@master?states]#?cd?init/ [root@master?init]#?ls files??limit.sls??pkg.sls [root@master?init]#?cp?limit.sls?ntp-crontab.sls [root@master?init]#?ls files??limit.sls??ntp-crontab.sls??pkg.sls [root@master?init]#?cd?files/ [root@master?files]#?cp?/var/spool/cron/root?. [root@master?files]#?pwd /etc/salt/states/init/files [root@master?files]#?cat?root? */5?*?*?*?*?/usr/sbin/ntpdate?-u?202.120.2.101>/dev/null?2>&1 [root@master?files]#?mv?root?ntp-crontab.conf [root@master?files]#?cat?ntp-crontab.conf? */5?*?*?*?*?/usr/sbin/ntpdate?-u?202.120.2.101>/dev/null?2>&1 [root@master?files]#?cd?.. [root@master?init]#?ls files??limit.sls??ntp-crontab.sls??pkg.sls [root@master?~]#?cat?/etc/salt/states/init/ntp-crontab.sls? ntp-crontab-config:file.managed:-?name:?/var/spool/cron/root-?source:?salt://init/files/ntp-crontab.conf-?user:?root-?group:?root-?mode:?644 計劃任務更新執行結果: [root@master?init]#?salt?'*'?state.highstate node01.saltstack.com: ----------ID:?pkg.initFunction:?pkg.installedName:?mtrResult:?TrueComment:?Package?mtr?is?already?installed.Started:?21:09:06.608808Duration:?4265.514?msChanges:??? ----------ID:?pkg.initFunction:?pkg.installedName:?nmapResult:?TrueComment:?Package?nmap?is?already?installed.Started:?21:09:10.874647Duration:?0.685?msChanges:??? ----------ID:?pkg.initFunction:?pkg.installedName:?lrzszResult:?TrueComment:?Package?lrzsz?is?already?installed.Started:?21:09:10.875446Duration:?0.583?msChanges:??? ----------ID:?limit-conf-configFunction:?file.managedName:?/etc/security/limits.confResult:?TrueComment:?File?/etc/security/limits.conf?is?in?the?correct?stateStarted:?21:09:10.879350Duration:?4.1?msChanges:??? ----------ID:?ntp-crontab-configFunction:?file.managedName:?/var/spool/cron/rootResult:?TrueComment:?File?/var/spool/cron/root?updatedStarted:?21:09:10.883639Duration:?9.342?msChanges:???----------diff:New?filemode:0644 Summary ------------ Succeeded:?5?(changed=1) Failed:????0 ------------ Total?states?run:?????5 node02.saltstack.com: ----------ID:?pkg.initFunction:?pkg.installedName:?mtrResult:?TrueComment:?Package?mtr?is?already?installed.Started:?21:09:07.831431Duration:?4292.2?msChanges:??? ----------ID:?pkg.initFunction:?pkg.installedName:?nmapResult:?TrueComment:?Package?nmap?is?already?installed.Started:?21:09:12.123977Duration:?0.714?msChanges:??? ----------ID:?pkg.initFunction:?pkg.installedName:?lrzszResult:?TrueComment:?Package?lrzsz?is?already?installed.Started:?21:09:12.124798Duration:?0.426?msChanges:??? ----------ID:?limit-conf-configFunction:?file.managedName:?/etc/security/limits.confResult:?TrueComment:?File?/etc/security/limits.conf?is?in?the?correct?stateStarted:?21:09:12.128235Duration:?5.165?msChanges:??? ----------ID:?ntp-crontab-configFunction:?file.managedName:?/var/spool/cron/rootResult:?TrueComment:?File?/var/spool/cron/root?updatedStarted:?21:09:12.133621Duration:?8.761?msChanges:???----------diff:New?filemode:0644 Summary ------------ Succeeded:?5?(changed=1) Failed:????0 ------------ Total?states?run:?????5 檢查結果: [root@node01?spool]#?cd?/var/spool/cron/ [root@node01?cron]#?ls root [root@node01?cron]#?cat?root */5?*?*?*?*?/usr/sbin/ntpdate?-u?202.120.2.101>/dev/null?2>&1 [root@node02?~]#?cat?/var/spool/cron/root? */5?*?*?*?*?/usr/sbin/ntpdate?-u?202.120.2.101>/dev/null?2>&1 通過對比會發現,與master的下發文件一致案例4:同步內網的hosts文件(適用于內網沒有建立獨立DNS的情況)
[root@master?~]#?cd?/etc/salt/states/init/ [root@master?init]#?ll 總用量?16 drwxr-xr-x?2?root?root?4096?2月??18?21:01?files -rw-r--r--?1?root?root??168?2月??18?17:42?limit.sls -rw-r--r--?1?root?root??169?2月??18?21:08?ntp-crontab.sls -rw-r--r--?1?root?root???79?2月??15?14:55?pkg.sls [root@master?init]#?cd?files/ [root@master?files]#?vim?hosts.conf? 127.0.0.1???localhost?localhost.localdomain?localhost4?localhost4.localdomain4 ::1?????????localhost?localhost.localdomain?localhost6?localhost6.localdomain6 10.10.10.140????master??master.saltstack.com 10.10.10.141????node01??node01.saltstack.com 10.10.10.142????node02??node02.saltstack.com 10.10.10.143????node03??node03.saltstack.com [root@master?init]#?cat?hosts.sls? hosts-config:file.managed:-?name:?/etc/hosts-?source:?salt://init/files/hosts.conf-?user:?root-?group:?root-?mode:?644 說明:下發文件到/etc/hosts,源文件 [root@master?states]#?cat?/etc/salt/states/top.sls? base:'*':-?init.pkg-?init.limit-?init.ntp-crontab-?init.hosts [root@master?states]#?salt?'*'?state.highstate ----------前面的部分我直接省略了-------------- ----------ID:?hosts-configFunction:?file.managedName:?/etc/hostsResult:?TrueComment:?File?/etc/hosts?updatedStarted:?21:31:43.644962Duration:?13.119?msChanges:???----------diff:---??+++??@@?-3,3?+3,4?@@10.10.10.140?mastermaster.saltstack.com10.10.10.141?node01node01.saltstack.com10.10.10.142?node02node02.saltstack.com+10.10.10.143??node03node03.saltstack.com Summary ------------ Succeeded:?6?(changed=1) Failed:????0 ------------ Total?states?run:?????6 客戶端進行測試: [root@node01?cron]#?cat?/etc/hosts 127.0.0.1???localhost?localhost.localdomain?localhost4?localhost4.localdomain4 ::1?????????localhost?localhost.localdomain?localhost6?localhost6.localdomain6 10.10.10.140?master?master.saltstack.com 10.10.10.141?node01?node01.saltstack.com 10.10.10.142?node02?node02.saltstack.com 10.10.10.143?node03?node03.saltstack.com [root@node02?~]#?cat?/etc/hosts 127.0.0.1???localhost?localhost.localdomain?localhost4?localhost4.localdomain4 ::1?????????localhost?localhost.localdomain?localhost6?localhost6.localdomain6 10.10.10.140?master?master.saltstack.com 10.10.10.141?node01?node01.saltstack.com 10.10.10.142?node02?node02.saltstack.com 10.10.10.143?node03?node03.saltstack.com 如果此時我在master端修改hosts.conf文件 [root@master?init]#?pwd /etc/salt/states/init [root@master?init]#?cat?files/hosts.conf? 127.0.0.1???localhost?localhost.localdomain?localhost4?localhost4.localdomain4 ::1?????????localhost?localhost.localdomain?localhost6?localhost6.localdomain6 10.10.10.140?master?master.saltstack.com 10.10.10.141?node01?node01.saltstack.com 10.10.10.142?node02?node02.saltstack.com 10.10.10.143?node03?node03.saltstack.com 10.10.10.144?openstack01?openstack01.saltstack.com 10.10.10.145?openstack02?openstack02.saltstack.com [root@master?init]#?salt?'*'?state.highstate ----------前面的部分我直接省略了-------------- ----------ID:?hosts-configFunction:?file.managedName:?/etc/hostsResult:?TrueComment:?File?/etc/hosts?updatedStarted:?21:37:50.679328Duration:?78.269?msChanges:???----------diff:---??+++??@@?-4,3?+4,5?@@10.10.10.141node01node01.saltstack.com10.10.10.142node02node02.saltstack.com10.10.10.143node03node03.saltstack.com+10.10.10.144openstack01openstack01.saltstack.com+10.10.10.145openstack02openstack02.saltstack.com Summary ------------ Succeeded:?6?(changed=1) Failed:????0 ------------ Total?states?run:?????6 客戶端進行測試: [root@node01?cron]#?cat?/etc/hosts 127.0.0.1???localhost?localhost.localdomain?localhost4?localhost4.localdomain4 ::1?????????localhost?localhost.localdomain?localhost6?localhost6.localdomain6 10.10.10.140?master?master.saltstack.com 10.10.10.141?node01?node01.saltstack.com 10.10.10.142?node02?node02.saltstack.com 10.10.10.143?node03?node03.saltstack.com 10.10.10.144?openstack01?openstack01.saltstack.com 10.10.10.145?openstack02?openstack02.saltstack.com [root@node02?~]#?cat?/etc/hosts 127.0.0.1???localhost?localhost.localdomain?localhost4?localhost4.localdomain4 ::1?????????localhost?localhost.localdomain?localhost6?localhost6.localdomain6 10.10.10.140?master?master.saltstack.com 10.10.10.141?node01?node01.saltstack.com 10.10.10.142?node02?node02.saltstack.com 10.10.10.143?node03?node03.saltstack.com關于salt批量配置hosts文件:http://www.ttlsa.com/linux/salt-modules-hosts/
這里我只寫一個添加hosts文件的例子,更多內容可以參考上面的鏈接(干貨很多)
[root@master?~]#?salt?'*'?hosts.set_host?10.10.10.145?openstack02.saltstack.com node02.saltstack.com:True node01.saltstack.com:True [root@master?~]#?salt?'*'?hosts.set_host?10.10.10.143?openstack03.saltstack.com node02.saltstack.com:True node01.saltstack.com:True [root@master?~]#?salt-ssh?'*'?cmd.run?'tail?-2?/etc/hosts' node02:10.10.10.144?openstack01?openstack01.saltstack.com10.10.10.145?openstack02.saltstack.com node01:10.10.10.144?openstack01?openstack01.saltstack.com10.10.10.145?openstack02.saltstack.com轉載于:https://blog.51cto.com/molewan/1899125
總結
以上是生活随笔為你收集整理的saltstack的状态文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iteritems()与items()
- 下一篇: 视频转换器如何将腾讯QLV格式转换成MP