【1227】自動化運維grains,pillar,配置管理文件目錄遠程命令計劃任務,salt-ssh使用
24.6 grains
24.7 pillar
24.8 安裝配置httpd
24.9 配置管理文件
24.10 配置管理目錄
24.11 配置管理遠程命令
24.12 配置管理計劃任務
24.13 其他命令
24.14 salt-ssh使用
24.6 grains
grains 是在 minion 啓動時收集到的一些信息,比如操作系統類型、網卡 ip、內核版本、cpu 架構等
salt ‘主機名’ grains.ls 列出所有的grains項目名字
[root@arslinux-01 ~]# salt 'arslinux-01' grains.ls
arslinux-01:
- SSDs
- biosreleasedate
- biosversion
- cpu_flags
- cpu_model
- cpuarch
- disks
- dns
- domain
- fqdn
- fqdn_ip4
- fqdn_ip6
- fqdns
- gid
- gpus
- groupname
- host
- hwaddr_interfaces
- id
- init
- ip4_gw
- ip4_interfaces
salt ‘arslinux-01’ grains.items 列出所有grains項目以及值
[root@arslinux-01 ~]# salt 'arslinux-01' grains.items
arslinux-01:
----------
SSDs:
biosreleasedate:
07/02/2015
biosversion:
6.00
—— grains 的信息並不是動態的,並不會實時變更,它是在 minion 啓動時收集到的。
—— 我們可以根據 grains 收集到的一些信息,做配置管理工作
—— grains支持自定義信息
1、在 minion 端的 /etc/salt/grains 裏添加兩行,重啓 salt-minion
[root@arslinux-02 ~]# vim /etc/salt/grains
env: test
role: nginx
[root@arslinux-02 ~]# systemctl restart salt-minion
2、master 上獲取 grains
[root@arslinux-01 ~]# salt '*' grains.item role env
arslinux-01:
----------
env:
role:
arslinux-02:
----------
env:
test
role:
nginx
——可以藉助 grains 的一些屬性信息來執行
salt -G 鍵:值 具體操作 藉助 grains 信息執行
[root@arslinux-01 ~]# salt '*' grains.item role env
arslinux-01:
----------
env:
role:
arslinux-02:
----------
env:
test
role:
nginx
[root@arslinux-01 ~]# salt -G role:nginx cmd.run 'hostname'
arslinux-02:
arslinux-02
[root@arslinux-01 ~]# salt -G role:nginx cmd.run 'ifconfig'
arslinux-02:
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.194.132 netmask 255.255.255.0 broadcast 192.168.194.255
inet6 fe80::4c99:ed43:5757:e772 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:14:4f:d9 txqueuelen 1000 (Ethernet)
RX packets 7957 bytes 1228538 (1.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7860 bytes 1432289 (1.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 1019 bytes 89448 (87.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1019 bytes 89448 (87.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@arslinux-01 ~]# salt -G role:nginx test.ping
arslinux-02:
True
可以給同一類或同一組機器自定義 grains,比如只針對web,mysql或者redis,這一批機器去執行幾個命令,就可以給這同一類或同一組機器自定義 grains,然後通過 grains 對這些機器進行遠程操作執行命令。
24.7 pillar
pillar 和 grains不一樣,是在 master 上定義的,並且是針對 minion 定義的一些信息。像一些比較重要的數據(密碼)可以存在 pillar 裏,還可以定義變量等
配置自定義 pillar
1、在配置文件 /etc/salt/master,找到 pillar_roots: 和之後共三行,取消註釋,重啓 salt-master
[root@arslinux-01 ~]# vim /etc/salt/master
pillar_roots:
base:
- /srv/pillar
[root@arslinux-01 ~]# systemctl restart salt-master
注意配置中的空格,base 前有2個空格,- 前有4個空格,不能省略
2、創建 /srv/pillar,並在目錄下創建 test.sls,內容爲 conf: /etc/123.conf,可以再創建個 test2.sls
[root@arslinux-01 ~]# mkdir /srv/pillar
[root@arslinux-01 ~]# vi /srv/pillar/test.sls
conf: /etc/123.conf
[root@arslinux-01 ~]# vi /srv/pillar/test2.sls
dir: /data/123
[root@arslinux-01 ~]# vi /srv/pillar/top.sls //作爲總入口
base:
'arslinux-02':
- test
- test2 //根據需要和實際可以定義多個
3、當更改完 pillar 配置文件後,我們可以通過刷新 pillar 配置來獲取新的 pillar 狀態,無需重啓 salt-master
[root@arslinux-01 ~]# salt '*' saltutil.refresh_pillar
arslinux-01:
True
arslinux-02:
True
4、驗證狀態
[root@arslinux-01 ~]# salt '*' pillar.item conf
arslinux-01:
----------
conf:
arslinux-02:
----------
conf:
/etc/123.conf
[root@arslinux-01 ~]# salt '*' pillar.item conf dir
arslinux-01:
----------
conf:
dir:
arslinux-02:
----------
conf:
/etc/123.conf
dir:
/data/123
——當然,也可以將不同機器的參數寫到同一個 top.sls 中,例如:
base:
'arslinux-02':
- test
'arslinux-01':
- test2
[root@arslinux-01 ~]# salt '*' saltutil.refresh_pillar
arslinux-02:
True
arslinux-01:
True
[root@arslinux-01 ~]# salt '*' pillar.item conf dir
arslinux-01:
----------
conf:
dir:
/data/123
arslinux-02:
----------
conf:
/etc/123.conf
dir:
可以看看和之前操作結果的差別
5、pillar 同樣可以用來作爲 salt 的匹配對象
salt -I ‘參數’ test.ping
[root@arslinux-01 ~]# salt -I 'conf:/etc/123.conf' cmd.run 'w'
arslinux-02:
23:21:44 up 1:16, 1 user, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.194.1 22:06 24.00s 0.17s 0.17s -bash
[root@arslinux-01 ~]# salt -I 'conf:/etc/123.conf' test.ping
arslinux-02:
True
24.8 安裝配置httpd
1、master 配置文件中找到 file_roots: 啓用其文件存放目錄
[root@arslinux-01 ~]# vim /etc/salt/master
file_roots:
base:
- /srv/salt/
2、創建 /srv/salt/ 目錄並進入
[root@arslinux-01 ~]# mkdir /srv/salt/
[root@arslinux-01 ~]# cd !$
cd /srv/salt/
3、創建 top.sls,重啓 salt-master
[root@arslinux-01 salt]# vim top.sls
base:
'*':
- httpd
[root@arslinux-01 salt]# systemctl restart salt-master
意思是,在所有的客戶端上執行httpd模塊
4、創建 httpd.sls
[root@arslinux-01 salt]# vim httpd.sls
httpd-service:
pkg.installed:
- names:
- httpd
- httpd-devel
service.running:
- name: httpd
- enable: True
說明: httpd-service 是 id 的名字,自定義的。pkg.installed 爲包安裝函數,下面是要安裝的包的名字。service.running 也是一個函數,來保證指定的服務啓動,enable 表示開機啓動
5、執行安裝命令
[root@arslinux-01 salt]# salt 'luo-1' state.highstate
執行命令後,會到 /srv/salt/ 下去找 top.sls,然後其中根據提到的相關模塊,再去執行
整個過程靜默安裝
執行操作前,記得關閉佔用 80 端口的服務,不然會報錯,httpd 啓動不了
24.9 配置管理文件
1、master 上創建 test.sls
[root@arslinux-01 salt]# vim test.sls
file_test:
file.managed:
- name: /tmp/arslinux
- source: salt://test/123/ppp.txt
- user: root
- group: root
- mode: 600
說明: 第一行的 file_test 爲自定的名字,表示該配置段的名字,可以在別的配置段中引用它;file.managed 模塊可以定義參數;name 指在minion 端上創建的文件路徑、名稱;source指定文件從哪裏拷貝;這裏的 salt://test/123/1.txt 相當於是 /srv/salt/test/123/1.txt
2、創建 ppp.txt 文件
[root@arslinux-01 salt]# mkdir test
[root@arslinux-01 salt]# mkdir test/123/
[root@arslinux-01 salt]# cp /etc/inittab test/123/ppp.txt
3、更改 top.sls
[root@arslinux-01 salt]# vim top.sls
base:
'*':
- test
4、執行操作
[root@arslinux-01 salt]# salt 'arslinux-02' state.highstate
arslinux-02:
----------
ID: file_test
Function: file.managed
Name: /tmp/arslinux
Result: True
Comment: File /tmp/arslinux updated
Started: 22:43:37.846500
Duration: 167.482 ms
Changes:
----------
diff:
New file
Summary for arslinux-02
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
Total run time: 167.482 ms
5、minion 端查看是否成功創建文件
[root@arslinux-02 ~]# ll /tmp/arslinux
-rw------- 1 root root 511 12月 27 22:43 /tmp/arslinux
24.10 配置管理目錄
1、master 上創建 test_dir.sls
[root@arslinux-01 salt]# vim testdir.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,加上它之後,源刪除文件或目錄,目標(minion端)也會跟着刪除,否則不會刪除;其他參數都和之前管理文件類似
2、改 top.sls,可以直接增加
[root@arslinux-01 salt]# echo ' - testdir' >> top.sls
[root@arslinux-01 salt]# cat top.sls
base:
'*':
- test
- testdir
3、執行操作
[root@arslinux-01 salt]# salt 'arslinux-02' state.highstate
arslinux-02:
----------
ID: file_test
Function: file.managed
Name: /tmp/arslinux
Result: True
Comment: File /tmp/arslinux is in the correct state
Started: 23:00:27.660586
Duration: 95.354 ms
Changes:
----------
ID: file_dir
Function: file.recurse
Name: /tmp/testdir
Result: True
Comment: Recursively updated /tmp/testdir
Started: 23:00:27.756271
Duration: 325.589 ms
Changes:
----------
/tmp/testdir/ppp.txt:
----------
diff:
New file
mode:
0640
Summary for arslinux-02
------------
Succeeded: 2 (changed=1)
Failed: 0
------------
Total states run: 2
Total run time: 420.943 ms
4、查看 minion 端是否成功創建及權限是否正確
[root@arslinux-02 ~]# ll /tmp/testdir/
總用量 4
-rw-r----- 1 root root 511 12月 27 23:00 ppp.txt
[root@arslinux-02 ~]# ll -d /tmp/testdir/
drwxr-x--- 2 root root 21 12月 27 23:00 /tmp/testdir/
5、如果在次執行 state.highstate 會報錯,因爲沒有了 /test/123/
[root@arslinux-01 salt]# cd test/
[root@arslinux-01 test]# mkdir abc
[root@arslinux-01 test]# touch 123.txt
[root@arslinux-01 test]# rm -rf 123
[root@arslinux-01 test]# ls
123.txt abc
[root@arslinux-01 test]# salt 'arslinux-02' state.highstate
arslinux-02:
----------
ID: file_test
Function: file.managed
Name: /tmp/arslinux
Result: False
Comment: Source file salt://test/123/ppp.txt not found in saltenv 'base'
Started: 23:08:19.655224
Duration: 140.84 ms
Changes:
----------
ID: file_dir
Function: file.recurse
Name: /tmp/testdir
Result: False
Comment: Recurse failed: none of the specified sources were found
Started: 23:08:19.796420
Duration: 32.291 ms
Changes:
Summary for arslinux-02
------------
Succeeded: 0
Failed: 2
------------
Total states run: 2
Total run time: 173.131 ms
因爲刪除了 /test/123/ 因此基於這個目錄的操作會出錯
6、解決問題,將 top.sls 中 test 去除,不再引用它
[root@arslinux-01 salt]# vim top.sls
base:
'*':
- testdir
7、創建 /srv/salt/test/123/
[root@arslinux-01 salt]# mkdir test/123/
[root@arslinux-01 salt]# mv test/abc test/123.txt test/123/
8、再操作
[root@arslinux-01 salt]# salt 'arslinux-02' state.highstate
arslinux-02:
----------
ID: file_dir
Function: file.recurse
Name: /tmp/testdir
Result: True
Comment: Recursively updated /tmp/testdir
Started: 23:16:26.961983
Duration: 420.045 ms
Changes:
----------
/tmp/testdir/123.txt:
----------
diff:
New file
mode:
0640
removed:
- /tmp/testdir/ppp.txt
Summary for arslinux-02
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
Total run time: 420.045 ms
9、minion 端並沒有同步 abc 目錄,因爲 abc 爲空,如果想要同步,必須目錄不爲空
[root@arslinux-02 ~]# ll /tmp/testdir/
總用量 0
-rw-r----- 1 root root 0 12月 27 23:16 123.txt
24.11 配置管理遠程命令
1、編輯 top.sls
[root@arslinux-01 salt]# vim top.sls
base:
'*':
- shell_test
2、創建 shell_test.sls
[root@arslinux-01 salt]# vim shell_test.sls
shell_test:
cmd.script:
- source: salt://test/1.sh
- user: root
3、創建腳本 1.sh
[root@arslinux-01 salt]# vim test/1.sh
#!/bin/bash
touch /tmp/111.txt
if [ ! -d /tmp/1233 ]
then
mkdir /tmp/1233
fi
4、執行操作
[root@arslinux-01 salt]# salt 'arslinux-02' state.highstate
arslinux-02:
----------
ID: hell_test
Function: cmd.script
Result: True
Comment: Command 'hell_test' run
Started: 16:54:25.741342
Duration: 168.634 ms
Changes:
----------
pid:
4413
retcode:
0
stderr:
stdout:
Summary for arslinux-02
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
Total run time: 168.634 ms
5、minion 端查看
[root@arslinux-02 ~]# ll /tmp/
總用量 4
-rw-r--r-- 1 root root 0 12月 27 16:54 111.txt
drwxr-xr-x 2 root root 6 12月 23 16:54 1233
-rw------- 1 root root 511 12月 27 22:43 arslinux
24.12 配置管理計劃任務
1、編輯 top.sls
[root@arslinux-01 salt]# vim top.sls
base:
'*':
- cron_test
2、創建 cron_test
[root@arslinux-01 salt]# vim cron_test.sls
cron_test:
cron.present:
- name: /bin/touch /tmp/12121212.txt
- user: root
- minute: '20'
- hour: 17
- daymonth: '*'
- month: '*'
- dayweek: '*'
注意: *需要用單引號引起來。當然我們還可以使用file.managed模塊來管理cron,因爲系統的cron都是以配置文件的形式存在的
——想要刪除該cron,需要增加:
cron.absent:
- name: /bin/touch /tmp/111.txt
兩者不能共存,要想刪除一個 cron,那之前的 present 就得去掉
3、執行操作
[root@arslinux-01 salt]# salt 'arslinux-02' state.highstate
arslinux-02:
----------
ID: cron_test
Function: cron.present
Name: /bin/touch /tmp/12121212.txt
Result: True
Comment: Cron /bin/touch /tmp/12121212.txt added to root's crontab
Started: 17:16:36.800747
Duration: 543.17 ms
Changes:
----------
root:
/bin/touch /tmp/12121212.txt
Summary for arslinux-02
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
Total run time: 543.170 ms
4、minion 端查看
[root@arslinux-02 ~]# date
2019年 12月 27日 星期五 17:18:11 CST
[root@arslinux-02 ~]# ll /tmp/
總用量 4
-rw-r--r-- 1 root root 0 12月 27 16:54 111.txt
drwxr-xr-x 2 root root 6 12月 27 16:54 1233
-rw------- 1 root root 511 12月 27 22:43 arslinux
[root@arslinux-02 ~]# crontab -l
# Lines below here are managed by Salt, do not edit
# SALT_CRON_IDENTIFIER:/bin/touch /tmp/12121212.txt
20 17 * * * /bin/touch /tmp/12121212.txt
5、17點20之後再查看 minion 端
[root@arslinux-02 ~]# ll /tmp/
總用量 4
-rw-r--r-- 1 root root 0 12月 27 16:54 111.txt
-rw-r--r-- 1 root root 0 12月 27 17:20 12121212.txt
drwxr-xr-x 2 root root 6 12月 27 16:54 1233
-rw------- 1 root root 511 12月 27 22:43 arslinux
已經成功
6、添加之後不能擅自改動 minion 端的 crontab,否則 master 再次執行 salt 時會再添加一次
[root@arslinux-02 ~]# crontab -e
crontab: installing new crontab
[root@arslinux-02 ~]# crontab -l
# SALT_CRON_IDENTIFIER:/bin/touch /tmp/12121212.txt
20 17 * * * /bin/touch /tmp/12121212.txt
[root@arslinux-01 salt]# salt 'arslinux-02' state.highstate
arslinux-02:
----------
ID: cron_test
Function: cron.present
Name: /bin/touch /tmp/12121212.txt
Result: True
Comment: Cron /bin/touch /tmp/12121212.txt added to root's crontab
Started: 17:29:33.617502
Duration: 491.19 ms
Changes:
----------
root:
/bin/touch /tmp/12121212.txt
Summary for arslinux-02
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
Total run time: 491.190 ms
[root@arslinux-02 ~]# crontab -l
# SALT_CRON_IDENTIFIER:/bin/touch /tmp/12121212.txt
20 17 * * * /bin/touch /tmp/12121212.txt
# Lines below here are managed by Salt, do not edit
# SALT_CRON_IDENTIFIER:/bin/touch /tmp/12121212.txt
20 17 * * * /bin/touch /tmp/12121212.txt
——看到提示 # Lines below here are managed by Salt, do not edit
我們不能隨意改動它,否則就沒法刪除或者修改這個cron
7、先修改 minion 端 crontab 到正確狀態
[root@arslinux-02 ~]# crontab -e
crontab: installing new crontab
[root@arslinux-02 ~]# crontab -l
# Lines below here are managed by Salt, do not edit
# SALT_CRON_IDENTIFIER:/bin/touch /tmp/12121212.txt
20 17 * * * /bin/touch /tmp/12121212.txt
8、master 端執行刪除 crontab,使用 cron.absent: 模塊
[root@arslinux-01 salt]# vim cron_test.sls
cron_test:
cron.absent:
- name: /bin/touch /tmp/12121212.txt
[root@arslinux-01 salt]# salt 'arslinux-02' state.highstate
arslinux-02:
----------
ID: cron_test
Function: cron.absent
Name: /bin/touch /tmp/12121212.txt
Result: True
Comment: Cron /bin/touch /tmp/12121212.txt removed from root's crontab
Started: 17:34:42.720616
Duration: 437.822 ms
Changes:
----------
root:
/bin/touch /tmp/12121212.txt
Summary for arslinux-02
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
Total run time: 437.822 ms
[root@arslinux-02 ~]# crontab -l
# Lines below here are managed by Salt, do not edit
24.13 其他命令
cp.get_file 拷貝 master 上的文件到客戶端
cp.get_dir 拷貝 master 上的目錄到客戶端
[root@arslinux-01 salt]# cp /etc/passwd test/1.txt
[root@arslinux-01 salt]# salt '*' cp.get_file salt://test/1.txt /tmp/1234567.txt
arslinux-02:
/tmp/1234567.txt
arslinux-01:
/tmp/1234567.txt
[root@arslinux-01 salt]# salt '*' cp.get_dir salt://test/123/ /tmp/
arslinux-01:
- /tmp//123/123.txt
- /tmp//123/abc
arslinux-02:
- /tmp//123/123.txt
- /tmp//123/abc
salt-run manage.up 顯示存活的 minion
salt ‘*’ cmd.script salt://腳本 命令行下執行 master 上的 shell 腳本
[root@arslinux-01 salt]# salt-run manage.up
- arslinux-01
- arslinux-02
[root@arslinux-01 salt]# salt '*' cmd.script salt://test/1.sh
arslinux-01:
----------
pid:
21621
retcode:
0
stderr:
stdout:
arslinux-02:
----------
pid:
7289
retcode:
0
stderr:
stdout:
24.14 salt-ssh使用
salt-ssh 不需要對客戶端做認證,客戶端也不用安裝 salt-minion,它類似 pssh/expect
1、安裝 salt-ssh
[root@arslinux-01 ~]# yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-latest-2.el7.noarch.rpm
[root@arslinux-01 ~]# yum install -y salt-ssh
2、編輯配置文件 roster
[root@arslinux-01 ~]# vim /etc/salt/roster
Luo-1:
host: 192.168.237.132
user: root
passwd: xxxxxxx
luozhu:
host: 192.168.237.136
user: root
passwd: xxxxxxx
3、測試能否登錄
[root@arslinux-01 ~]# salt-ssh --key-deploy '*' -r 'w'
[ERROR ] Failed collecting tops for Python binary python3.
arslinux-02:
----------
retcode:
0
stderr:
stdout:
[email protected]'s password:
19:25:46 up 2:42, 1 user, load average: 0.00, 0.06, 0.09
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.194.1 16:44 1:50m 0.09s 0.09s -bash
arslinux-01:
----------
retcode:
0
stderr:
stdout:
[email protected]'s password:
19:25:46 up 2:42, 1 user, load average: 0.45, 0.22, 0.17
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.194.1 16:44 10.00s 9.07s 0.04s /usr/bin/python /usr/bin/salt-ssh --key-deploy * -r w
[root@arslinux-01 ~]# date
2019年 12月 27日 星期五 19:27:10 CST
[root@arslinux-01 ~]# ll /root/.ssh/authorized_keys
-rw-r--r--. 1 root root 1191 12月 27 19:25 /root/.ssh/authorized_keys
[root@arslinux-02 ~]# ll /root/.ssh/authorized_keys
-rw-r--r--. 1 root root 1199 12月 27 19:25 /root/.ssh/authorized_keys
公鑰已經傳遞了過去
4、刪除 roster 中的密碼,再執行,可以登錄
[root@arslinux-01 ~]# salt-ssh --key-deploy '*' -r 'w'
arslinux-02:
----------
retcode:
0
stderr:
stdout:
19:30:23 up 2:47, 1 user, load average: 0.00, 0.03, 0.06
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.194.1 16:44 1:27 0.10s 0.10s -bash
arslinux-01:
----------
retcode:
0
stderr:
stdout:
19:30:23 up 2:47, 1 user, load average: 0.25, 0.18, 0.16
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.194.1 16:44 7.00s 1.49s 0.02s /usr/bin/python /usr/bin/salt-ssh -