saltstack配置管理
1、YAML语言
YAML是一种直观的能够被电脑识别的数据序列化格式,是一个可读性高并且容易被人类阅读,容易和脚本语言交互,用来表达资料序列的编程语言。
它类似于标准通用标记语言的子集XML的数据描述语言,语法比XML简单很多。
YAML的基本规则:
- 使用缩进来表示层级关系,每层2个空格,禁止使用TAB键
- 当冒号不是处于最后时,冒号后面必须有一个空格
例如:- name: httpd httpd前面要有一个空格
- 用 - 表示列表,- 的后面必须有一个空格
- 用 # 表示注释
YAML配置文件要放到SaltStack让我们放的位置,可以在SaltStack的 Master 配置文件中查找file_roots即可看到。
[root@master ~]# vim /etc/salt/master
......
file_roots:
base:
- /srv/salt/base
test:
- /srv/salt/test
dev:
- /srv/salt/dev
prod:
- /srv/salt/prod
......
[root@master ~]# mkdir -p /srv/salt/{base,test,dev,prod} //根据配置文件创建相应的目录
[root@master ~]# tree /srv/salt/
/srv/salt/
├── base
├── dev
├── prod
└── test
[root@master ~]# systemctl restart salt-master //重启master才会生效
2、用SaltStack配置一个apache实例
在Master上部署sls配置文件并执行
//跟改master主机上的master配置文件
[root@master ~]# cd /etc/salt/
[root@master salt]# vim master
......
#file_roots:
# base:
# - /srv/salt
#
//添加以下内容,
file_roots:
base: //基础环境,base是默认的位置,如果file_roots只有一个,则base是必备的且必须叫base,不能改名
- /srv/salt/base
test: //测试环境
- /srv/salt/test
dev: //开发环境
- /srv/salt/dev
prod: //生产环境
- /srv/salt/prod
# The master_roots setting configures a master-only copy of the file_roots dictionary,
......
//创建配置文件中的文件目录
[root@master ~]# mkdir -p /srv/salt/{base,test,dev,prod}
[root@master ~]# tree /srv/salt/
/srv/salt/
├── base
├── dev
├── prod
└── test
//重启master,因为修改了配置文件
[root@master salt]# systemctl restart salt-master.service
//到/srv/salt/base/中创建相应的目录和状态文件
[root@master base]# mkdir -p web/apache
[root@master web]# cd apache/
[root@master apache]# vim install.sls
apache-install: //安装Apache软件任务
pkg.installed: //使用pkg包模块,installed达到已经安装的状态
- name: httpd //安装的软件名
apache-service: //Apache的服务控制
service.running: //service模块,达到运行状态
- name: httpd //要被控制的服务名称
- enable: true //达到开机自启状态
[root@master web]# pwd
/srv/salt/base/web
[root@master base]# tree web/
web/
└── apache
└── apache.sls
1 directory, 1 file
//编写好转态文件后下一步执行即可,开始前要先ping一下看是否和目标主机通讯
[root@master web]# salt 'node01' test.ping
node01:
True
//执行操作,saltenv这是是指定环境的,base环境可以不写,但是其他环境必须要写saltenv
[root@master web]# salt 'node01' state.sls web.apache.install saltenv=base
# state.sls state状态模块,sls状态描述文件,用这个模块中的这个功能,执行install文件
# web.apache.install 状态文件的位置/web/apache/install.sls文件,在命令行中不需要输入后缀
# 如果在执行命令后出现了以下内容,这不是报错,复制最下面一行执行即可,就可以看到执行的结果
node01:
Minion did not return. [No response]
The minions may not have all finished running and any remaining minions will return upon completion. To look up the return data for this job later, run the following command:
salt-run jobs.lookup_jid 20211103012720859511
-------------------------------------------------------------------------------------------
node01:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 18:28:13.073733
Duration: 696.394 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 18:28:13.771890
Duration: 37.412 ms
Changes:
Summary for node01
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 733.806 ms
# 只要没有报错就是没有问题
//然后到node01主机上查看是否达到我们所希望的状态
[root@node01 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@node01 ~]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since 二 2021-11-02 14:47:27 CST; 3h 43min ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 10630 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─10630 /usr/sbin/httpd -DFOREGROUND
├─10631 /usr/sbin/httpd -DFOREGROUND
├─10632 /usr/sbin/httpd -DFOREGROUND
├─10633 /usr/sbin/httpd -DFOREGROUND
├─10634 /usr/sbin/httpd -DFOREGROUND
└─10635 /usr/sbin/httpd -DFOREGROUND
11月 02 14:47:27 node01 systemd[1]: Starting The Apache HTTP Server...
11月 02 14:47:27 node01 httpd[10630]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using... message
11月 02 14:47:27 node01 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
*此时已经达到了我们所希望的状态*
实际生产过程中肯定不是一条命令管理一个主机的执行,都是不同的主机执行不同的操作,如果是这样的话再使用上面的命令方式就很麻烦,如果数量多甚至不可能完成,所以就有了下面的top file。
3、top file
直接通过命令执行sls文件时够自动化吗?答案是否定的,因为我们还要告诉某台主机要执行某个任务,自动化应该是我们让它干活时,它自己就知道哪台主机要干什么活,但是直接通过命令执行sls文件并不能达到这个目的,为了解决这个问题,top file 应运而生。
top file就是一个入口,top file的文件名可通过在 Master的配置文件中搜索top.sls找出,且此文件必须在 base 环境中,默认情况下此文件必须叫top.sls。
top file的作用就是告诉对应的主机要干什么活,比如让web服务器启动web服务,让数据库服务器安装mysql等等。
top file配置方式
//首先要在web中在创建一个Nginx目录,在里面写一个Nginx状态描述文件。实现两个主机安装不同的软件
[root@master web]# mkdir nginx
[root@master web]# cd nginx/
[root@master nginx]# vim install.sls
nginx-install:
pkg.installed:
- name: nginx
nginx-service:
service.running:
- name: nginx
- enable: true
[root@master base]# tree web
web
├── apache
│ └── install.sls
└── nginx
└── install.sls
2 directories, 3 files
//在/srv/salt/base/下创建一个top.sls,来编写top file
[root@master base]# pwd
/srv/salt/base
[root@master base]# vim top.sls
# node01执行 base环境中的/web/apache/install.sls 状态描述文件;node01安装httpd
# master执行 base环境中的/web/nginx/install.sls 状态描述文件:master安装nginx
base:
'node01':
- web.apache.install
'master:'
- web.nginx.install
##################以下内容仅为方便理解,并不编写在top file文件中#############################
# 这种情况node01和master两个主机都需要执行两个任务
# 第一个先要执行"*"所有主机的任务,第二个是自身的任务,所有这种情况就是每个主机执行两个任务
base:
'*':
- web.xxx.xxx
'node01':
- web.apache.install
'master':
- web.nginx.install
#################################结束########################################
上面这种方式是对所有主机的操作,而在实际生产环境中,是不会这样的。
实际生产环境中,都是对某个服务的集群执行高级状态,所以就有了下面的高级状态的使用
3.1 highstate高级状态的使用
管理SaltStack时一般最常用的管理操作就是执行高级状态
[root@master ~]# salt ‘*’ state.highstate //生产环境禁止这样使用salt命令
上面让所有人执行高级状态,但实际工作当中,一般不会这么用,工作当中一般都是通知某台或某些台目标主机来执行高级状态,具体是否执行则是由top file来决定的
若在执行高级状态时加上参数test=True,则它会告诉我们它将会做什么,但是它不会真的去执行这个操作。
node01主机执行top file
[root@master nginx]# salt 'node01' state.highstate
# 表示对node01主机使用高级状态管理,先会把主机名和top file中的主机匹配
#如果有这个主机就执行topfile中node01的任务,如果没有这个主机则会报错,提示没有这个主机
[root@master nginx]# salt 'node01' state.highstate //单独对topfile中的node01执行高级状态
node01:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: The following packages were installed/updated: httpd
Started: 19:24:34.527130
Duration: 9551.211 ms
Changes:
----------
httpd:
----------
new:
2.4.6-97.el7.centos.1
old:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd has been enabled, and is running
Started: 19:24:44.094179
Duration: 257.773 ms
Changes:
----------
httpd:
True
Summary for node01
------------
Succeeded: 2 (changed=2)
Failed: 0
------------
Total states run: 2
Total run time: 9.809 s
//到node01查看是否达到目标状态
[root@node01 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@node01 ~]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since 二 2021-11-02 19:24:44 CST; 2min 28s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 77278 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─77278 /usr/sbin/httpd -DFOREGROUND
├─77308 /usr/sbin/httpd -DFOREGROUND
├─77309 /usr/sbin/httpd -DFOREGROUND
├─77310 /usr/sbin/httpd -DFOREGROUND
├─77311 /usr/sbin/httpd -DFOREGROUND
└─77312 /usr/sbin/httpd -DFOREGROUND
11月 02 19:24:44 node01 systemd[1]: Starting The Apache HTTP Server...
11月 02 19:24:44 node01 httpd[77278]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using... message
11月 02 19:24:44 node01 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
4、数据系统grains和pillar的使用
SaltStack有两大数据系统,分别是:
Grains
Pillar
Grains与Pillar的区别
数据系统 | 存储位置 | 类型 | 采集方式 | 应用场景 |
---|---|---|---|---|
Grains | minion | 静态 | minion启动时采集可通过刷新避免重启minion服务 | 1.信息查询2.在命令行下进行目标匹配3.在top file中进行目标匹配4.在模板中进行目标匹配 |
Pillar | master | 动态 | 指定,实时生效 | 1.目标匹配2.敏感数据配置 |
4.1 Grains
Grains是SaltStack的一个组件,其存放着minion启动时收集到的信息,如果启动后修改了一些信息是收集不到的,这个时候就需要重启,这样新修改的内容就变成了启动前的信息,就可以收集到了。
Grains是SaltStack组件中非常重要的组件之一,因为我们在做配置部署的过程中会经常使用它,Grains是SaltStack记录minion的一些静态信息的组件。可简单理解为Grains记录着每台minion的一些常用属性,比如CPU、内存、磁盘、网络信息等。我们可以通过grains.items查看某台minion的所有Grains信息。
Grains的功能:
- 收集资产信息
Grains应用场景:
- 信息查询
- 在命令行下进行目标匹配
- 在top file中进行目标匹配
- 在目标中进行目标匹配
信息查询方式:
//查看node01的minion启动前grains收集的信息
[root@master ~]# salt 'node01' grains.items //这些信息包含软件、硬件等各种信息
# 同样也可以使用正则表达式在查看某一类名称的主机信息
node01:
----------
biosreleasedate:
07/22/2020
biosversion:
6.00
cpu_flags:
- fpu
- vme
- de
- pse
- tsc
- msr
- pae
- mce
- cx8
- apic
- sep
- mtrr
- pge
- mca
- cmov
- pat
- pse36
- clflush
- mmx
- fxsr
- sse
- sse2
- ss
- ht
- syscall
- nx
- pdpe1gb
- rdtscp
- lm
- constant_tsc
- arch_perfmon
- rep_good
- nopl
- xtopology
- tsc_reliable
- nonstop_tsc
- eagerfpu
- pni
- pclmulqdq
- vmx
- ssse3
- fma
- cx16
- pcid
- sse4_1
- sse4_2
- x2apic
- movbe
- popcnt
- tsc_deadline_timer
- aes
- xsave
- avx
- f16c
- rdrand
- hypervisor
- lahf_lm
- abm
- 3dnowprefetch
- tpr_shadow
- vnmi
- ept
- vpid
- fsgsbase
- tsc_adjust
- bmi1
- avx2
- smep
- bmi2
- erms
- invpcid
- avx512f
- avx512dq
- rdseed
- adx
- smap
- avx512ifma
- clflushopt
- clwb
- avx512cd
- sha_ni
- avx512bw
- avx512vl
- xsaveopt
- xsavec
- xgetbv1
- ibpb
- ibrs
- stibp
- arat
- avx512vbmi
- pku
- ospke
- avx512_vpopcntdq
- spec_ctrl
- intel_stibp
- arch_capabilities
cpu_model:
11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
cpuarch:
x86_64
cwd:
/
disks:
- sda
- sr0
dns:
----------
domain:
ip4_nameservers:
- 114.114.114.114
ip6_nameservers:
nameservers:
- 114.114.114.114
options:
search:
sortlist:
domain:
efi:
False
efi-secure-boot:
False
fqdn:
node01
fqdn_ip4:
- 192.168.164.128
fqdn_ip6:
- fe80::65f2:205e:c9e8:7e94
fqdns:
- node01
gid:
0
gpus:
|_
----------
model:
SVGA II Adapter
vendor:
vmware
groupname:
root
host:
node01
hwaddr_interfaces:
----------
ens33:
00:0c:29:73:96:01
lo:
00:00:00:00:00:00
id:
node01
init:
systemd
ip4_gw:
192.168.164.2
ip4_interfaces:
----------
ens33:
- 192.168.164.128
lo:
- 127.0.0.1
ip6_gw:
False
ip6_interfaces:
----------
ens33:
- fe80::65f2:205e:c9e8:7e94
lo:
- ::1
ip_gw:
True
ip_interfaces:
----------
ens33:
- 192.168.164.128
- fe80::65f2:205e:c9e8:7e94
lo:
- 127.0.0.1
- ::1
ipv4:
- 127.0.0.1
- 192.168.164.128
ipv6:
- ::1
- fe80::65f2:205e:c9e8:7e94
kernel:
Linux
kernelparams:
|_
- BOOT_IMAGE
- /vmlinuz-3.10.0-862.el7.x86_64
|_
- root
- /dev/mapper/centos-root
|_
- ro
- None
|_
- crashkernel
- auto
|_
- rd.lvm.lv
- centos/root
|_
- rd.lvm.lv
- centos/swap
|_
- rhgb
- None
|_
- quiet
- None
|_
- LANG
- zh_CN.UTF-8
kernelrelease:
3.10.0-862.el7.x86_64
kernelversion:
#1 SMP Fri Apr 20 16:44:24 UTC 2018
locale_info:
----------
defaultencoding:
UTF-8
defaultlanguage:
zh_CN
detectedencoding:
UTF-8
timezone:
unknown
localhost:
node01
lsb_distrib_codename:
CentOS Linux 7 (Core)
lsb_distrib_id:
CentOS Linux
lvm:
----------
centos:
- root
- swap
machine_id:
fc1652ee0006497ab60f3d63a28022f6
manufacturer:
VMware, Inc.
master:
192.168.164.133
mdadm:
mem_total:
3773
nodename:
node01
num_cpus:
2
num_gpus:
1
os:
CentOS
os_family:
RedHat
osarch:
x86_64
oscodename:
CentOS Linux 7 (Core)
osfinger:
CentOS Linux-7
osfullname:
CentOS Linux
osmajorrelease:
7
osrelease:
7.5.1804
osrelease_info:
- 7
- 5
- 1804
path:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
pid:
1300
productname:
VMware Virtual Platform
ps:
ps -efHww
pythonexecutable:
/usr/bin/python3
pythonpath:
- /usr/bin
- /usr/lib64/python36.zip
- /usr/lib64/python3.6
- /usr/lib64/python3.6/lib-dynload
- /usr/lib64/python3.6/site-packages
- /usr/lib/python3.6/site-packages
pythonversion:
- 3
- 6
- 8
- final
- 0
saltpath:
/usr/lib/python3.6/site-packages/salt
saltversion:
3004
saltversioninfo:
- 3004
selinux:
----------
enabled:
False
enforced:
Disabled
serialnumber:
VMware-56 4d a9 f7 d7 2d 47 d5-83 ee 8a 49 05 73 96 01
server_id:
1441650718
shell:
/bin/sh
ssds:
swap_total:
3071
systemd:
----------
features:
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN
version:
219
systempath:
- /usr/local/sbin
- /usr/local/bin
- /usr/sbin
- /usr/bin
transactional:
False
uid:
0
username:
root
uuid:
f7a94d56-2dd7-d547-83ee-8a4905739601
virtual:
VMware
zfs_feature_flags:
False
zfs_support:
False
zmqversion:
4.1.4
//不仅可以查看说有信息还可以单独查看某一些信息
[root@master ~]# salt 'node01' grains.get os
node01:
CentOS
[root@master ~]# salt 'node01' grains.get ip4_interfaces
node01:
----------
ens33:
- 192.168.164.128
lo:
- 127.0.0.1
[root@master ~]# salt 'node01' grains.get ip4_interfaces:ens33
node01:
- 192.168.164.128
目标匹配:
//1、跟系统来匹配主机,并显示grains所收集的信息
[root@master ~]# salt -G 'os:CentOS' grains.items
# -G是使用grains 来对所收集信息中的os系统进行匹配centos系统,如果匹配到就显示所收集的所有信息
# 因为现在是node01是centos所以匹配显示的收集信息中只有node01的
node01:
----------
biosreleasedate:
07/22/2020
biosversion:
6.00
cpu_flags:
- fpu
- vme
- de
- pse
- tsc
- msr
- pae
- mce
- cx8
- apic
- sep
- mtrr
......
//2、 根据系统对某一些主机进行操作
[root@master ~]# salt -G 'os:CentOS' test.ping
node01:
True
top file:
[root@master base]# pwd
/srv/salt/base
[root@master base]# vim top.sls
base:
'os:CentOS':
- match: grain //如果是用系统来决定什么系统安装执行什么任务,这里必要要写,这是表示用什么方式获取
- web.apache.install
'master':
- web.nginx.install
[root@node01 ~]# systemctl stop httpd.service
[root@master base]# salt 'node01' state.highstate
node01:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 09:44:36.408648
Duration: 660.322 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd is already enabled, and is running
Started: 09:44:37.070746
Duration: 193.282 ms
Changes:
----------
httpd:
True
Summary for node01
------------
Succeeded: 2 (changed=1)
Failed: 0
------------
Total states run: 2
Total run time: 853.604 ms
**自定义Grains的两种方法:**
- minion配置文件,在配置文件中搜索grains
- 在/etc/salt下生成一个grains文件,在此文件中定义(推荐方式)
第一种修改minion配置文件的方式不推荐,因为如果每个主机的配置文件不同,到时候管理配置的很麻烦甚至不可能实现所需目标,所以推荐使用第二种方式
[root@master ~]# vim /etc/salt/grains
app: httpd
[root@master ~]# systemctl restart salt-minion
[root@master salt]# salt 'master' grains.get app
master:
httpd
下面这种凡是是不要重启的,直接生效使用
[root@master salt]# cat grains
app: httpd
今天星期几: 今天星期三
[root@master salt]# salt '*' saltutil.sync_grains // 同步grains
node01:
master:
[root@master salt]# salt '' grains.get 今天星期几
master:
今天星期三
4.2 Pillar
Pillar也是SaltStack组件中非常重要的组件之一,是数据管理中心,经常配置states在大规模的配置管理工作中使用它。Pillar在SaltStack中主要的作用就是存储和定义配置管理中需要的一些数据,比如软件版本号、用户名密码等信息,它的定义存储格式与Grains类似,都是YAML格式。
在Master配置文件中有一段Pillar settings选项专门定义Pillar相关的一些参数:
[root@master salt]# vim master
# highstate format, and is generally just key/value pairs.
#pillar_roots: #这里是默认的位置,如果要修改直接跟改配置重启即可
# base:
# - /srv/pillar
#
#ext_pillar:
# - hiera: /etc/hiera.yaml
# - cmd_yaml: cat /etc/salt/yaml
默认Base环境下Pillar的工作目录在/srv/pillar目录下。若你想定义多个环境不同的Pillar工作目录,只需要修改此处配置文件即可。
Pillar的特点:
- 可以给指定的minion定义它需要的数据
- 只有指定的人才能看到定义的数据
- 在master配置文件里设置
[root@master salt]# salt '*' pillar.items //这是正常的情况
node01:
----------
master:
----------
*默认pillar是没有任何信息的,如果想查看信息,需要在 master 配置文件上把 pillar_opts的注释取消,并将其值设为 True。*
# the pillar called "master". This is used to set simple configurations in the
# master config file that can then be used on minions.
pillar_opts: true //改为true,这样就可以看见信息了
# The pillar_safe_render_error option prevents the master from passing pillar
root@master ~]# systemctl restart salt-master
[root@master salt]# salt 'node01' pillar.items //这样就可以查看到信息了
......略
user:
root
utils_dirs:
- /var/cache/salt/master/extmods/utils
verify_env:
True
winrepo_branch:
master
winrepo_cachefile:
winrepo.p
winrepo_dir:
/srv/salt/win/repo
winrepo_dir_ng:
/srv/salt/win/repo-ng
winrepo_fallback:
winrepo_insecure_auth:
False
winrepo_passphrase:
winrepo_password:
winrepo_privkey:
winrepo_pubkey:
winrepo_refspecs:
- +refs/heads/*:refs/remotes/origin/*
- +refs/tags/*:refs/tags/*
winrepo_remotes:
- https://github.com/saltstack/salt-winrepo.git
winrepo_remotes_ng:
- https://github.com/saltstack/salt-winrepo-ng.git
winrepo_ssl_verify:
True
winrepo_user:
worker_threads:
5
zmq_backlog:
1000
zmq_filtering:
False
zmq_monitor:
False
**pillar自定义数据:**
//修改配置文件
[root@master ]# vim /etc/salt/master
......
# highstate format, and is generally just key/value pairs.
pillar_roots: //取消注释,并修改下面的位置
base:
- /srv/pillar/base
#
#ext_pillar:
......
[root@master ~]# mkdir -p /srv/pillar/{base,prod}
[root@master ~]# tree /srv/pillar/
/srv/pillar/
└── base
[root@master ~]# systemctl restart salt-master //重启让配置文件生效
[root@master base]# pwd
/srv/pillar/base
[root@master base]# vim apache.sls
{% if grains['os'] == 'CentOS' %}
# 如果系统是centos就安装httpd
apache: httpd
{% elif grains['os'] == 'RedHat' %}
# 如果系统是Redhat的就安装test ,这个test只是便于理解,具体根据项目需求来
apache: test
{% endif %}
//创建一个top file文件,这个top file和/srv/salt/base/中的top file不一样
# 这里的top file 只是设置谁能执行或使用/srv/pillar/base/apache
[root@master base]# vim top.sls
base: //指定环境
'*': //指定主机
- apache //指定任务状态文件 apache.sls
//查看自定义数据
[root@master base]# salt '*' pillar.items
node01: //node01是centos系统所以这里是正确没问题的
----------
apache:
httpd
master:
----------
......
......
在salt下修改apache的状态文件,引用pillar的数据
[root@master apache]# pwd
/srv/salt/base/web/apache
[root@master apache]# cat install.sls
apache-install:
pkg.installed:
- name: {{ pillar['apache'] }}
# 这里变量引用和ansible有所区别,ansible中变量必须用双引号引起了,而saltstack可加可不加
apache-service:
service.running:
- name: {{ pillar['apache'] }}
- enable: true
[root@master base]# salt 'node01' state.highstate
node01:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: The following packages were installed/updated: httpd
Started: 10:55:52.665777
Duration: 27948.223 ms
Changes:
----------
httpd:
----------
new:
2.4.6-97.el7.centos.1
old:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd has been enabled, and is running
Started: 10:56:20.630457
Duration: 273.282 ms
Changes:
----------
httpd:
True
Summary for node01
------------
Succeeded: 2 (changed=2)
Failed: 0
------------
Total states run: 2
Total run time: 28.222 s