【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 -