自動化運維之Saltstack

第三十八課 自動化運維之Saltstackhtml

目錄node

1、自動化運維介紹
2、 saltstack安裝
3、 啓動saltstack服務
4、 saltstack配置認證
5、 saltstack遠程執行命令
6、 grains
7、 pillar
8、 安裝配置httpd
9、 配置管理文件
10、 配置管理目錄
11、 配置管理遠程命令
12、 配置管理計劃任務
十3、 其餘命令
十4、 salt-ssh使用python


1、自動化運維介紹

認識自動化運維linux

傳統運維效率低,大多工做人爲完成ios

傳統運維工做繁瑣,容易出錯nginx

傳統運維每日重複作相同的事情web

傳統運維沒有標準化流程正則表達式

傳統運維的腳本繁多,不能方便管理shell

自動化運維就是要解決上面全部問題apache

常見自動化運維工具

Puppet (www.puppetlabs.com) 基於ruby開發,c/s架構,支持多平臺,可管理配置文件、用戶、cron任務、軟件包、系統服務等。 分爲社區版(免費)和企業版(收費),企業版支持圖形化配置。

Saltstack(官網 https://saltstack.com,文檔docs.saltstack.com ) 基於python開發,c/s架構,支持多平臺,比puppet輕量,在遠程執行命令時很是快捷,配置和使用比puppet容易,能實現puppet幾乎全部的功能。

Ansible (www.ansible.com ) 更加簡潔的自動化運維工具,不須要在客戶端上安裝agent,基於python開發。能夠實現批量操做系統配置、批量程序的部署、批量運行命令。


2、 saltstack安裝

saltstack介紹

https://docs.saltstack.com/en/latest/topics/index.html

saltstack既可使用salt-ssh遠程執行,相似ansible,也支持c/s模式。

演示環境:

saltserver 192.168.1.41 CentOS release 6.8 (Final)
saltminion 192.168.1.43 CentOS release 6.8 (Final)
saltminion01 192.168.1.42 CentOS release 6.8 (Final)

三臺機器都配置hosts

# vim /etc/hosts
// 添加
192.168.1.41 saltserver.local
1921.68.1.42 saltminion01.local
192.168.1.43 saltminion.local

Saltstack安裝

1.安裝epel-release源

// 也能夠從http://repo.saltstack.com/yum/redhat/下載安裝salt最新的源
[root@saltserver ~]# yum -y install epel-release

2.安裝相應的salt包

// 查看salt安裝包
[root@saltserver ~]# yum list | grep salt
python-salttesting.noarch                      2015.7.10-1.el6              epel
salt.noarch                                    2015.5.10-2.el6              epel
salt-api.noarch                                2015.5.10-2.el6              epel
salt-cloud.noarch                              2015.5.10-2.el6              epel
salt-master.noarch                             2015.5.10-2.el6              epel
salt-minion.noarch                             2015.5.10-2.el6              epel
salt-ssh.noarch                                2015.5.10-2.el6              epel
salt-syndic.noarch                             2015.5.10-2.el6              epel

// 服務器端安裝salt-master和salt-minion
[root@saltserver ~]# yum -y install salt-master.noarch salt-minion.noarch

// 客戶端安裝salt-minion便可
[root@saltminion01 ~]# yum -y install salt-minion.noarch


3、 啓動saltstack服務

1.啓動salt-master

[root@saltserver ~]# /etc/init.d/salt-master start
// 服務端監聽4505和4506兩個端口,4505爲消息發佈的端口,4506爲和客戶端通訊的端口
[root@saltserver ~]# netstat -nltup | egrep "4505|4506"
tcp        0      0 0.0.0.0:4505                0.0.0.0:*                   LISTEN      1589/python2.6      
tcp        0      0 0.0.0.0:4506                0.0.0.0:*                   LISTEN      1609/python2.6

2.編輯minion端的配置文件,並啓動minion

// 以saltminion01爲例 
[root@saltminion01 ~]# vim /etc/salt/minion
// 修改master爲服務端的主機名或ip地址,若是規模較大也能夠搭建本地dns服務來提供名稱解析服務
master: saltserver.local
// 修改id值爲客戶端的主機名,這會作爲服務端上顯示的客戶端標識
#id:
id: saltminion01.local
// 啓動minion
[root@saltminion01 ~]# /etc/init.d/salt-minion start


4、 saltstack配置認證

master端和minion端通訊須要創建一個安全通道,傳輸過程須要加密,因此得配置認證,也是經過密鑰對來加密解密的

minion在第一次啓動時會在/etc/salt/pki/minion/下生成minion.pem和minion.pub,其中.pub爲公鑰,它會把公鑰傳輸給master

master第一次啓動時也會在/etc/salt/pki/master下生成密鑰對,當master接收到minion傳過來的公鑰後,經過salt-key工具接受這個公鑰,一旦接受後就會在/etc/salt/pki/master/minions/目錄裏存放剛剛接受的公鑰,同時客戶端也會接受master傳過去的公鑰,把它放在/etc/salt/pki/minion目錄下,並命名爲minion_master.pub

以上過程須要藉助salt-key工具來實現

salt-key經常使用選項

-a  後面跟主機名,認證指定主機
 -A 認證全部主機
 -r  跟主機名,拒絕指定主機
 -R  拒絕全部主機
 -d 跟主機名,刪除指定主機認證
 -D 刪除所有主機認證
 -y 省略掉交互,至關於直接按了y

顯示key

// 服務端
[root@saltserver ~]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
saltminion.local
saltminion01.local
saltserver
Rejected Keys:
[root@saltserver ~]#

認證主機

// 能夠一臺一臺認證,也能夠批量認證
[root@saltserver ~]# salt-key -a saltserver
The following keys are going to be accepted:
Unaccepted Keys:
saltserver
Proceed? [n/Y] Y
Key for minion saltserver accepted.
[root@saltserver ~]# salt-key -L
Accepted Keys:
saltserver
Denied Keys:
Unaccepted Keys:
saltminion.local
saltminion01.local
Rejected Keys:
// 批量認證,也可經過修改配置文件實現
// 在 /etc/salt/master中取消下句註釋 #auto_accept: True
[root@saltserver ~]# salt-key -A
The following keys are going to be accepted:
Unaccepted Keys:
saltminion.local
saltminion01.local
Proceed? [n/Y] Y
Key for minion saltminion.local accepted.
Key for minion saltminion01.local accepted.
[root@saltserver ~]# salt-key -L
Accepted Keys:
saltminion.local
saltminion01.local
saltserver
Denied Keys:
Unaccepted Keys:
Rejected Keys:

刪除主機認證

// 以saltminion01爲例,在服務器上操做, -D刪除全部認證
[root@saltserver ~]# salt-key -d saltminion01.local -y
Deleting the following keys:
Accepted Keys:
saltminion01.local
Key for minion saltminion01.local deleted.
// minion端重啓服務
[root@saltminion01 ~]# /etc/init.d/salt-minion restart
Stopping salt-minion daemon:                               [  OK  ]
Starting salt-minion daemon:                               [  OK  ]
// 服務器再次檢查key
[root@saltserver ~]# salt-key -L
Accepted Keys:
saltminion.local
saltserver
Denied Keys:
Unaccepted Keys:
saltminion01.local
Rejected Keys:

拒絕key

// -R 拒絕全部
[root@saltserver ~]# salt-key -r saltminion01.local -y
Key for minion saltminion01.local rejected.
[root@saltserver ~]# salt-key -L
Accepted Keys:
saltminion.local
saltserver
Denied Keys:
Unaccepted Keys:
Rejected Keys:
saltminion01.local


5、 saltstack遠程執行命令

遠程執行命令語法

salt <target> <模塊名>.<方法> [參數…]

target支持如下類型

Glob通配,salt的默認類型

// 模塊的保存位置/usr/lib/python2.6/site-packages/salt/modules/
// 方法能夠查看模塊vim /usr/lib/python2.6/site-packages/salt/modules/test.py,
// 也能夠從salt的官方文檔查詢
// 查看minion可用模塊salt 'target' sys.list_modules
// 查看模塊的函數 salt 'target' sys.list_functions test
// 系統幫助文件 salt 'target' sys.doc test.ping
// *表示全部主機,test表明模塊,ping是方法。
[root@saltserver ~]# salt "*" test.ping
saltminion01.local:
    True
saltminion.local:
    True
saltserver:
    True

-L,長選項--list,該選項通個逗號分隔的列表來指定多個Minion。列表中不使用glob或正則表達式進行模式

[root@saltserver ~]# salt -L saltminion.local,saltminion01.local test.ping
saltminion.local:
    True
saltminion01.local:
    True

短選項:-S,長選項: --ipcidr。經過過指定一個IPv4地址或一個CIDR的ipv4子網來target minion。

[root@saltserver ~]# salt -S 192.168.1.0/24 test.ping
saltminion01.local:
    True
saltminion.local:
    True
saltserver:
    True

短選項: -E 長選項: --pcre。Perl語言兼容正則表達式(PCRE)

[root@saltserver ~]# salt -E  '^saltserver$' test.ping
saltserver:
    True
    
[root@saltserver ~]# salt -E  '.*.local$' test.ping            
saltminion.local:
    True
saltminion01.local:
    True

短選項:-G 長選項: --grain。

// 對os是CentOS的主機進行ping測試
[root@saltserver ~]# salt -G "os:CentOS" test.ping
saltserver:
    True
saltminion01.local:
    True
saltminion.local:
    True

Grain PCRE 短選項: 無 長選項:--grain-pcre

salt –grain-pcre ‘os:red(hat|flag) test.ping’

Pillar 短選項: -I,長選項: --pillar

實例見下,pillar部分

混合(compound),短選項: -C,長選項: --compound。混合target容許用戶在一個shell命令中指定多種target類型。默認使用glob,想指定其餘target類型,則須要在前面追加上類型簡寫和@符號

簡寫 target
G Grain
E PCRE Minion ID
P Grain PCRE
L 列表
I Pillar
S 子網/IP地址
R SECO範圍
# salt -C ‘G@os:Ubuntu,I@role:web,S@192.168l100.0/24’ test.ping

節點組(nodegroup)短選項:-N,長選項:--nodegroup。在命令行使用前必須先在master的配置文件中以target列表進行定義(使用混合匹配語法)

nodegroups:
    webdev: 'I@role:web,G@cluster:dev'
    webdqa: 'I@role:web,G@cluster:qa'
    webprod: 'I@role:web,G@cluster:prod'

節點組定義完畢並重載Master配置文件後,能夠經過salt進行target:

salt -N wendev test.ping


6、 grains

grains是在minion啓動時收集到的一些信息,好比操做系統類型、網卡ip、內核版本、cpu架構等。

列出全部的grains項目名字

[root@saltserver ~]# salt 'saltminion01.local' grains.ls | head -n 10
saltminion01.local:
    - SSDs
    - biosreleasedate
    - biosversion
    - cpu_flags
    - cpu_model
    - cpuarch
    - domain
    - fqdn
    - fqdn_ip4

列出全部grains項目以及值

[root@saltserver ~]# salt 'saltminion01.local' grains.items | head -n 10
saltminion01.local:
    ----------
    SSDs:
    biosreleasedate:
        07/02/2015
    biosversion:
        6.00
    cpu_flags:
        - fpu
        - vme

grains的信息並非動態的,並不會實時變動,它是在minion啓動時收集到的。

咱們能夠根據grains收集到的一些信息,作配置管理工做。

[root@saltserver ~]# salt -G 'os:CentOS' cmd.run 'hostname'
saltminion.local:
    saltminion.local
saltminion01.local:
    saltminion01.local
saltserver:
    saltserver.local

grains支持自定義信息。

// 在客戶端編輯/etc/salt/grains文件
[root@saltminion01 ~]# vim /etc/salt/grains
// 添加以下兩行自定義信息
role: nginx
env: test
// 重啓minion服務
[root@saltminion01 ~]# /etc/init.d/salt-minion restart
Stopping salt-minion daemon:                               [  OK  ]
Starting salt-minion daemon:                               [  OK  ]
// 在服務端查看自定義的信息
[root@saltserver ~]# salt 'saltminion01.local' grains.item role env
saltminion01.local:
    ----------
    env:
        test
    role:
        nginx
// 能夠利用自定義的信息來做爲target使用
[root@saltserver ~]# salt -G 'role:nginx' cmd.run 'hostname'
saltminion01.local:
    saltminion01.local


7、 pillar

pillar和grains不同,是在master上定義的,而且是針對minion定義的一些信息。像一些比較重要的數據(密碼)能夠存在pillar裏,還能夠定義變量等。

配置自定義pillar

// 在服務器端操做
[root@saltserver ~]# vim  /etc/salt/master
// 取消下三行的註釋
// salt中只能用空格對齊,不能用tab代替空格。空格數不限制,可是相同級別的縮進的空格數一致。
pillar_roots:
  base:
    - /srv/pillar
// 新建/src/pillar
rm: remove directory `/src/pillar'? y
[root@saltserver ~]# mkdir /srv/pillar
[root@saltserver ~]# ls -l !$
ls -l /srv/pillar
total 0
// 在/srv/pillar新建test.sls
[root@saltserver pillar]# echo 'conf: /etc/123.conf'>> test.sls
[root@saltserver pillar]# cat test.sls 
conf: /etc/123.conf
// 在/srv/pillar下新建top.sls
conf: /etc/123.conf
[root@saltserver pillar]# cat >>top.sls<<EOF
> base:
>   'saltminion01':
>     - test
> EOF
[root@saltserver pillar]# cat top.sls 
base:
  'saltminion01':
    - test

// 重啓master
[root@saltserver pillar]# /etc/init.d/salt-master restart
Stopping salt-master daemon:                               [  OK  ]
Starting salt-master daemon:                               [  OK  ]
// 刷新pillar配置來獲取新的pillar狀態
[root@saltserver pillar]# salt '*' saltutil.refresh_pillar
saltminion01.local:
    True
saltminion.local:
    True
saltserver:
    True

// 驗證
[root@saltserver pillar]# salt  '*' pillar.item conf
saltserver:
    ----------
saltminion.local:
    ----------
saltminion01.local:
    ----------
    conf:
        /etc/123.conf

//pillar一樣能夠用來做爲salt的匹配對象
[root@saltserver pillar]# salt -I 'conf:/etc/123.conf' test.ping
saltminion01.local:
    True


8、 安裝配置httpd

1.修輯master的配置文件

[root@saltserver pillar]# vim /etc/salt/master
// 取消下面三句的註釋
file_roots:
  base:
    - /srv/salt

// 建立/srv/salt目錄
[root@saltserver pillar]# mkdir /srv/salt/
[root@saltserver pillar]# cd /srv/salt/

2.建立/srv/salt/top.sls文件

[root@saltserver salt]# vim top.sls
base:
  '*':
    - httpd
// 重啓master
[root@saltserver salt]# /etc/init.d/salt-master restart
Stopping salt-master daemon:                               [  OK  ]
Starting salt-master daemon:                               [  OK  ]

3.新建/srv/salt/httpd.sls文件

[root@saltserver salt]# vim /srv/salt/httpd.sls
[root@saltserver salt]# vim httpd.sls
install_httpd:
  pkg.installed:
    - names:
      - httpd
      - httpd-devel
  service.running:
    - name: httpd
    - enable: True

4.測試安裝(以saltminion.local爲例)

[root@saltserver salt]# salt 'saltminion.local' state.highstate                 
saltminion.local:
----------
          ID: install_httpd
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: Package httpd is already installed.
     Started: 00:17:18.266735
    Duration: 1064.537 ms
     Changes:   
----------
          ID: install_httpd
    Function: pkg.installed
        Name: httpd-devel
      Result: True
     Comment: The following packages were installed/updated: httpd-devel
     Started: 00:17:19.331436
    Duration: 33639.881 ms
     Changes:   
              ----------
              apr:
                  ----------
                  new:
                      1.3.9-5.el6_9.1
                  old:
                      1.3.9-5.el6_2
              apr-devel:
                  ----------
                  new:
                      1.3.9-5.el6_9.1
                  old:
              apr-util-devel:
                  ----------
                  new:
                      1.3.9-3.el6_0.1
                  old:
              cyrus-sasl-devel:
                  ----------
                  new:
                      2.1.23-15.el6_6.2
                  old:
              db4:
                  ----------
                  new:
                      4.7.25-22.el6
                  old:
                      4.7.25-20.el6_7
              db4-cxx:
                  ----------
                  new:
                      4.7.25-22.el6
                  old:
              db4-devel:
                  ----------
                  new:
                      4.7.25-22.el6
                  old:
              db4-utils:
                  ----------
                  new:
                      4.7.25-22.el6
                  old:
                      4.7.25-20.el6_7
              expat:
                  ----------
                  new:
                      2.0.1-13.el6_8
                  old:
                      2.0.1-11.el6_2
              expat-devel:
                  ----------
                  new:
                      2.0.1-13.el6_8
                  old:
              httpd-devel:
                  ----------
                  new:
                      2.2.15-69.el6.centos
                  old:
              openldap:
                  ----------
                  new:
                      2.4.40-16.el6
                  old:
                      2.4.40-12.el6
              openldap-devel:
                  ----------
                  new:
                      2.4.40-16.el6
                  old:
----------
          ID: install_httpd
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd has been enabled, and is running
     Started: 00:17:53.015925
    Duration: 670.976 ms
     Changes:   
              ----------
              httpd:
                  True

Summary
------------
Succeeded: 3 (changed=2)
Failed:    0
------------
Total states run:     3

// 客戶端上檢查
[root@saltminion ~]# lsof -i :80
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
httpd   7505   root    4u  IPv6  43159      0t0  TCP *:http (LISTEN)
httpd   7507 apache    4u  IPv6  43159      0t0  TCP *:http (LISTEN)
httpd   7508 apache    4u  IPv6  43159      0t0  TCP *:http (LISTEN)
httpd   7509 apache    4u  IPv6  43159      0t0  TCP *:http (LISTEN)
httpd   7510 apache    4u  IPv6  43159      0t0  TCP *:http (LISTEN)
httpd   7511 apache    4u  IPv6  43159      0t0  TCP *:http (LISTEN)
httpd   7512 apache    4u  IPv6  43159      0t0  TCP *:http (LISTEN)
httpd   7513 apache    4u  IPv6  43159      0t0  TCP *:http (LISTEN)
httpd   7514 apache    4u  IPv6  43159      0t0  TCP *:http (LISTEN)


9、 配置管理文件

1.新建/srv/salt/test.sls

[root@saltserver salt]# vim /srv/salt/test.sls
// 添加以下內容
// 說明:第一行的file_test爲自定的名字,表示該配置段的名字,能夠在別的配置段中引用它,source指定文件從哪裏拷貝,
// 這裏的salt://test/123/1.txt至關因而/srv/salt/test/123/1.txt
file_test:
  file.managed:
    - name: /tmp/aminglinux.com
    - source: salt://test/123/1.txt
    - user: root
    - group: root
    - mode: 600

[root@saltserver salt]# mkdir -p test/123
[root@saltserver salt]# echo '11111' >> test/123/1.txt
[root@saltserver salt]# cat !$
cat test/123/1.txt
11111

2.編輯/srv/salt/top.sls文件

[root@saltserver salt]# vim top.sls 
base:
  '*':
    - test

3.同步測試

[root@saltserver salt]# salt 'saltminion01.local' state.highstate
saltminion01.local:
----------
          ID: file_test
    Function: file.managed
        Name: /tmp/aminglinux.com
      Result: True
     Comment: File /tmp/aminglinux.com updated
     Started: 13:08:40.688514
    Duration: 18.984 ms
     Changes:   
              ----------
              diff:
                  New file

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1

// saltminion01.local查看文件是否同步過去
[root@saltminion01 ~]# cat /tmp/aminglinux.com
11111
[root@saltminion01 ~]# ls -l /tmp/aminglinux.com
-rw------- 1 root root 6 Sep  5 13:08 /tmp/aminglinux.com


10、 配置管理目錄

1.在master上新建配置文件/srv/salt/test_dir.sls

[root@saltserver salt]# vim /srv/salt/test_dir.sls
// 添加內容以下
file_dir:
  file.recurse:
    - name: /tmp/testdir
    - source: salt://test/123
    - user: root
    - file_mode: 640
    - dir_mode: 750
    - mkdir: True
    - clean: True
// clean:True表示源刪除文件或目錄,目標也會跟着刪除,不然不會

2.修改top.sls

[root@saltserver salt]# vim top.sls 
base:
  '*':
    - test_dir

3.同步測試

[root@saltserver salt]# salt 'saltminion01.local' state.highstate
saltminion01.local:
----------
          ID: file_dir
    Function: file.recurse
        Name: /tmp/testdir
      Result: True
     Comment: Recursively updated /tmp/testdir
     Started: 13:16:30.379320
    Duration: 1044.869 ms
     Changes:   
              ----------
              /tmp/testdir:
                  ----------
                  mode:
                      0750
              /tmp/testdir/1.txt:
                  ----------
                  diff:
                      New file
                  mode:
                      0640

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1

// 在saltminion01.local查看目錄是否同步成功
drwxr-x--- 2 root root 4096 Sep  5 13:16 testdir
[root@saltminion01 ~]# ls -lR /tmp/
/tmp/:
total 8
-rw------- 1 root root    6 Sep  5 13:08 aminglinux.com
drwxr-x--- 2 root root 4096 Sep  5 13:16 testdir

/tmp/testdir:
total 4
-rw-r----- 1 root root 6 Sep  5 13:16 1.txt
// 注意,若是目錄爲空,則不會同步


11、 配置管理遠程命令

1.新建配置文件/srv/salt/shell_test.sls

[root@saltserver salt]# vim /srv/salt/shell_test.sls 
// 內容以下
shell_test:
  cmd.script:
    - source: salt://test/1.sh
    - user: root

2.在source目錄下新建腳本

[root@saltserver salt]#  vim /srv/salt/test/1.sh 
#!/bin/bash

echo "haha"

3.修改top.sls內容

"test/1.sh" [New] 3L, 25C written
[root@saltserver salt]# vim top.sls 
base:
  '*':
    - shell_test

4.測試

"top.sls" 3L, 30C written
[root@saltserver salt]# salt 'saltminion01.local' state.highstate 
saltminion01.local:
----------
          ID: shell_test
    Function: cmd.script
      Result: True
     Comment: Command 'shell_test' run
     Started: 13:27:50.265831
    Duration: 146.006 ms
     Changes:   
              ----------
              pid:
                  10120
              retcode:
                  0
              stderr:
              stdout:
                  haha

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1


12、 配置管理計劃任務

1.新建/srv/salt/cron_test.sls文件

Total states run:     1
[root@saltserver salt]# vim /srv/salt/cron_test.sls
// 添加以下內容
cron_test:
  cron.present:
    - name: /bin/touch /tmp/111.txt
    - user: root
    - minute: '*'
    - hour: 20
    - daymonth: '*'
    - month: '*'
    - dayweek: '*'
// 注意,*須要用單引號引發來。固然咱們還可使用file.managed模塊來管理cron,由於系統的cron都是以配置文件的形式存在的。

2.編輯top.sls文件

[root@saltserver salt]# vim top.sls 
base:
  '*':
    - cron_test

3.測試

// 在master端推送
[root@saltserver salt]# salt 'saltminion01.local' state.highstate
saltminion01.local:
----------
          ID: cron_test
    Function: cron.present
        Name: /bin/touch /tmp/111.txt
      Result: True
     Comment: Cron /bin/touch /tmp/111.txt added to root's crontab
     Started: 13:59:11.007707
    Duration: 19.99 ms
     Changes:   
              ----------
              root:
                  /bin/touch /tmp/111.txt

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1

//在saltminion01.local查看計劃任務,不要手動修改salt管理的crontab任務,不然就無法刪除或者修改這個cron了
[root@saltminion01 ~]# crontab -e
# Lines below here are managed by Salt, do not edit
# SALT_CRON_IDENTIFIER:/bin/touch /tmp/111.txt
* 20 * * * /bin/touch /tmp/111.txt

4.刪除任務計劃

// 修改corn_test.sls
// 二者不能共存,要想刪除一個cron,那以前的present就得去掉。
[root@saltserver salt]# vim cron_test.sls 
cron_test:
  cron.absent:
    - name: /bin/touch /tmp/111.txt

5.同步配置文件

[root@saltserver salt]# salt 'saltminion01.local' state.highstate
saltminion01.local:
----------
          ID: cron_test
    Function: cron.absent
        Name: /bin/touch /tmp/111.txt
      Result: True
     Comment: Cron /bin/touch /tmp/111.txt removed from root's crontab
     Started: 14:04:29.354015
    Duration: 12.691 ms
     Changes:   
              ----------
              root:
                  /bin/touch /tmp/111.txt

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1

6.在saltminion01.local上檢查

[root@saltminion01 ~]# crontab -e
// 已經刪除了
# Lines below here are managed by Salt, do not edit
~


十3、 其餘命令

1.拷貝master上的文件到客戶端

// cp.get_file
[root@saltserver salt]# salt '*' cp.get_file salt://test/1.sh /tmp/1.sh
saltminion01.local:
    /tmp/1.sh
saltminion.local:
    /tmp/1.sh
saltserver:
    /tmp/1.sh

//驗證
[root@saltminion01 ~]# ls -l /tmp/1.sh 
-rw-r--r-- 1 root root 25 Sep  5 14:08 /tmp/1.sh

2.拷貝master上的目錄到客戶端

// cp.get_dir
[root@saltserver salt]# salt '*' cp.get_dir salt://test/123 /tmp/    
saltminion01.local:
    - /tmp//123/1.txt
saltserver:
    - /tmp//123/1.txt
saltminion.local:
    - /tmp//123/1.txt

//在minion端驗證
[root@saltminion01 ~]# ls -lR /tmp/
/tmp/:
total 16
drwxr-xr-x 2 root root 4096 Sep  5 14:20 123
-rw-r--r-- 1 root root   25 Sep  5 14:08 1.sh
-rw------- 1 root root    6 Sep  5 13:08 aminglinux.com
drwxr-x--- 2 root root 4096 Sep  5 13:16 testdir

/tmp/123:
total 4
-rw-r--r-- 1 root root 6 Sep  5 14:20 1.txt

/tmp/testdir:
total 4
-rw-r----- 1 root root 6 Sep  5 13:16 1.txt

3.顯示存活的minion

//salt-run manage.up  
[root@saltserver salt]# salt-run manage.up  
- saltminion.local
- saltminion01.local
- saltserver

4.命令行下執行master上的shell腳本

//cmd.script 
[root@saltserver salt]# salt '*' cmd.script salt://test/1.sh  
saltminion01.local:
    ----------
    pid:
        10281
    retcode:
        0
    stderr:
    stdout:
        haha
saltserver:
    ----------
    pid:
        80391
    retcode:
        0
    stderr:
    stdout:
        haha
saltminion.local:
    ----------
    pid:
        9392
    retcode:
        0
    stderr:
    stdout:
        haha


十4、 salt-ssh使用

1.安裝salt-ssh

[root@saltserver salt]# yum -y install salt-ssh

2.新建配置文件 /etc/salt/roster

[root@saltserver salt]# vim /etc/salt/roster 
saltminion.local:
  host: 192.168.1.43
  user: root
  passwd: 123456
saltminion01.local:
  host: 192.168.1.42
  user: root
  passwd: 123456

3.推送key

// 第一次推送不成功
[root@saltserver salt]# salt-ssh --key-deploy '*' -r 'w' 
saltminion.local:
    ----------
    retcode:
        254
    stderr:
    stdout:
        The host key needs to be accepted, to auto accept run salt-ssh with the -i flag:
        The authenticity of host '192.168.1.43 (192.168.1.43)' can't be established.
        RSA key fingerprint is 3d:d4:e5:45:01:72:0e:15:2f:43:2f:6a:2e:c6:77:a7.
        Are you sure you want to continue connecting (yes/no)? 
saltminion01.local:
    ----------
    retcode:
        254
    stderr:
    stdout:
        The host key needs to be accepted, to auto accept run salt-ssh with the -i flag:
        The authenticity of host '192.168.1.42 (192.168.1.42)' can't be established.
        RSA key fingerprint is 3d:d4:e5:45:01:72:0e:15:2f:43:2f:6a:2e:c6:77:a7.
        Are you sure you want to continue connecting (yes/no)? 
//須要登陸一次
[root@saltserver salt]# ssh saltminion01.local
The authenticity of host 'saltminion01.local (192.168.1.42)' can't be established.
RSA key fingerprint is 3d:d4:e5:45:01:72:0e:15:2f:43:2f:6a:2e:c6:77:a7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'saltminion01.local' (RSA) to the list of known hosts.
root@saltminion01.local's password: 
Last login: Sat Sep  1 23:33:29 2018 from 192.168.1.9
[root@saltminion01 ~]# ssh saltminion.local  
The authenticity of host 'saltminion.local (192.168.1.43)' can't be established.
RSA key fingerprint is 3d:d4:e5:45:01:72:0e:15:2f:43:2f:6a:2e:c6:77:a7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'saltminion.local,192.168.1.43' (RSA) to the list of known hosts.
root@saltminion.local's password: 
Last login: Sat Sep  1 23:33:40 2018 from 192.168.1.9
[root@saltserver salt]# salt-ssh --key-deploy '*' -r 'w' 
saltminion01.local:
    ----------
    retcode:
        0
    stderr:
    stdout:
         14:48:01 up 4 days, 15:41,  2 users,  load average: 0.07, 0.03, 0.05
        USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
        root     tty1     -                Sat09    3days  0.04s  0.04s -bash
        root     pts/1    192.168.1.9      Sat23    5:19   0.15s  0.15s -bash
saltminion.local:
    ----------
    retcode:
        0
    stderr:
    stdout:
         14:48:10 up 4 days, 15:41,  2 users,  load average: 0.00, 0.01, 0.05
        USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
        root     tty1     -                Sat09    3days  0.00s  0.00s -bash
        root     pts/1    192.168.1.9      Sat23    5:38   0.04s  0.04s -bash
// 刪除roster中用戶的密碼,再次測試
[root@saltserver salt]# salt-ssh  '*' -r 'w'             
saltminion.local:
    ----------
    retcode:
        0
    stderr:
    stdout:
         14:49:55 up 4 days, 15:43,  2 users,  load average: 0.00, 0.01, 0.05
        USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
        root     tty1     -                Sat09    3days  0.00s  0.00s -bash
        root     pts/1    192.168.1.9      Sat23    7:23   0.04s  0.04s -bash
saltminion01.local:
    ----------
    retcode:
        0
    stderr:
    stdout:
         14:49:46 up 4 days, 15:42,  2 users,  load average: 0.01, 0.02, 0.05
        USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
        root     tty1     -                Sat09    3days  0.04s  0.04s -bash
        root     pts/1    192.168.1.9      Sat23    7:04   0.15s  0.15s -bash
相關文章
相關標籤/搜索