1.saltstack的grains
列出所有grains的key和value
[root@node01 ~]# salt 'node02' grains.items
node02:
----------
SSDs:
biosreleasedate:
07/29/2019
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
- nopl
- xtopology
- tsc_reliable
- nonstop_tsc
- eagerfpu
- pni
- pclmulqdq
- ssse3
- fma
- cx16
- pcid
- sse4_1
- sse4_2
- x2apic
- movbe
- popcnt
- tsc_deadline_timer
- aes
- xsave
- avx
- f16c
- rdrand
- hypervisor
- lahf_lm
- abm
- 3dnowprefetch
- fsgsbase
- tsc_adjust
- bmi1
- avx2
- smep
- bmi2
- invpcid
- mpx
- rdseed
- adx
- smap
- clflushopt
- xsaveopt
- xsavec
- arat
cpu_model:
Intel(R) Core(TM) i5-7300HQ CPU @ 2.50GHz
cpuarch:
x86_64
disks:
- sda
- sr0
- dm-0
- dm-1
dns:
----------
domain:
ip4_nameservers:
- 8.8.8.8
ip6_nameservers:
nameservers:
- 8.8.8.8
options:
search:
sortlist:
domain:
fqdn:
node02
fqdn_ip4:
- 192.168.100.132
fqdn_ip6:
- fe80::bd4c:afba:c0f8:1b4b
fqdns:
gid:
0
gpus:
|_
----------
model:
SVGA II Adapter
vendor:
vmware
groupname:
root
host:
node02
hwaddr_interfaces:
----------
ens33:
00:0c:29:31:84:0c
lo:
00:00:00:00:00:00
id:
node02
init:
systemd
ip4_gw:
192.168.100.2
ip4_interfaces:
----------
ens33:
- 192.168.100.132
lo:
- 127.0.0.1
ip6_gw:
False
ip6_interfaces:
----------
ens33:
- fe80::bd4c:afba:c0f8:1b4b
lo:
- ::1
ip_gw:
True
ip_interfaces:
----------
ens33:
- 192.168.100.132
- fe80::bd4c:afba:c0f8:1b4b
lo:
- 127.0.0.1
- ::1
ipv4:
- 127.0.0.1
- 192.168.100.132
ipv6:
- ::1
- fe80::bd4c:afba:c0f8:1b4b
kernel:
Linux
kernelrelease:
3.10.0-693.el7.x86_64
kernelversion:
#1 SMP Thu Jul 6 19:56:57 EDT 2017
locale_info:
----------
defaultencoding:
UTF-8
defaultlanguage:
zh_CN
detectedencoding:
UTF-8
timezone:
CST
localhost:
node02
lsb_distrib_codename:
Red Hat Enterprise Linux Server 7.4 (Maipo)
lsb_distrib_id:
Red Hat Enterprise Linux Server
lsb_distrib_release:
7.4
machine_id:
7174a32a21c849aba5aec27a0c7e7b57
manufacturer:
VMware, Inc.
master:
192.168.100.131
mdadm:
mem_total:
1823
nodename:
node02
num_cpus:
4
num_gpus:
1
os:
RedHat
os_family:
RedHat
osarch:
x86_64
oscodename:
Maipo
osfinger:
Red Hat Enterprise Linux Server-7
osfullname:
Red Hat Enterprise Linux Server
osmajorrelease:
7
osrelease:
7.4
osrelease_info:
- 7
- 4
path:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
pid:
27940
productname:
VMware Virtual Platform
ps:
ps -efHww
pythonexecutable:
/usr/bin/python
pythonpath:
- /usr/bin
- /usr/lib64/python27.zip
- /usr/lib64/python2.7
- /usr/lib64/python2.7/plat-linux2
- /usr/lib64/python2.7/lib-tk
- /usr/lib64/python2.7/lib-old
- /usr/lib64/python2.7/lib-dynload
- /usr/lib64/python2.7/site-packages
- /usr/lib64/python2.7/site-packages/gtk-2.0
- /usr/lib/python2.7/site-packages
pythonversion:
- 2
- 7
- 5
- final
- 0
saltpath:
/usr/lib/python2.7/site-packages/salt
saltversion:
2019.2.3
saltversioninfo:
- 2019
- 2
- 3
- 0
selinux:
----------
enabled:
True
enforced:
Permissive
serialnumber:
VMware-56 4d 4f c0 85 99 4f cf-a4 54 a7 b4 2d 31 84 0c
server_id:
711731264
shell:
/bin/sh
swap_total:
2047
systemd:
----------
features:
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN
version:
219
uid:
0
username:
root
uuid:
c04f4d56-9985-cf4f-a454-a7b42d31840c
virtual:
VMware
zfs_feature_flags:
False
zfs_support:
False
zmqversion:
4.1.4
查看所有的grains的key
[root@node01 ~]# salt 'node02' grains.ls
node02:
- 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
- ip6_gw
- ip6_interfaces
- ip_gw
- ip_interfaces
- ipv4
- ipv6
- kernel
- kernelrelease
- kernelversion
- locale_info
- localhost
- lsb_distrib_codename
- lsb_distrib_id
- lsb_distrib_release
- machine_id
- manufacturer
- master
- mdadm
- mem_total
- nodename
- num_cpus
- num_gpus
- os
- os_family
- osarch
- oscodename
- osfinger
- osfullname
- osmajorrelease
- osrelease
- osrelease_info
- path
- pid
- productname
- ps
- pythonexecutable
- pythonpath
- pythonversion
- saltpath
- saltversion
- saltversioninfo
- selinux
- serialnumber
- server_id
- shell
- swap_total
- systemd
- uid
- username
- uuid
- virtual
- zfs_feature_flags
- zfs_support
- zmqversion
通过查询key值来获取iPv4的地址
[root@node01 ~]# salt '*' grains.get fqdn_ip4
node01:
- 192.168.100.131
node02:
- 192.168.100.132
用grains来匹配不同的key值对应的minios来操作
[root@node01 ~]# salt -G 'os:redhat' cmd.run 'ls'
node02:
anaconda-ks.cfg
在base环境下写apache的状态文件
[root@node01 ~]# cd /srv/salt/base/
[root@node01 base]# ls
top.sls web
[root@node01 base]# cat web/apache/apache.sls
apache-install:
pkg.installed:
- name: httpd
apache.service:
service.running:
- name: httpd
- enable: True
在base环境下写top.file文件并且匹配的用grains来匹配key是redhat 的执行状态文件apache
[root@node01 base]# vi top.sls
[root@node01 base]# cat top.sls
base:
'os:redhat':
- match: grain
- web.apache.apache
查看系统是发现node2是redhat
[root@node01 base]# salt '*' grains.get os
node01:
CentOS
node02:
RedHat
查看mnode02的httpd的状态 是关闭状态
[root@node02 systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: inactive (dead) since 二 2020-02-18 19:35:40 CST; 6s ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 30171 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Process: 29061 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Process: 30098 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=0/SUCCESS)
Main PID: 30098 (code=exited, status=0/SUCCESS)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
2月 18 19:35:23 node02 systemd[1]: Starting The Apache HTTP Server...
2月 18 19:35:24 node02 httpd[30098]: AH00558: httpd: Could not reliably determine...ge
2月 18 19:35:29 node02 systemd[1]: Started The Apache HTTP Server.
2月 18 19:35:39 node02 systemd[1]: Stopping The Apache HTTP Server...
2月 18 19:35:40 node02 systemd[1]: Stopped The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
当执行状态文件时应该是s2的httpd会自动起来并且是开启自启动状态
[root@node01 base]# salt '*' state.highstate saltenv=base
node01:
----------
ID: states
Function: no.None
Result: False
Comment: No Top file or master_tops data matches found. Please see master log for details.
Changes:
Summary for node01
------------
Succeeded: 0
Failed: 1
------------
Total states run: 1
Total run time: 0.000 ms
node02:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 19:39:59.448603
Duration: 535.874 ms
Changes:
----------
ID: apache.service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd is already enabled, and is running
Started: 19:39:59.986189
Duration: 5451.58 ms
Changes:
----------
httpd:
True
Summary for node02
------------
Succeeded: 2 (changed=1)
Failed: 0
------------
Total states run: 2
Total run time: 5.987 s
ERROR: Minions returned with non-zero exit code
###node2的httpd已经启动并且是开机自启动
[root@node02 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since 二 2020-02-18 19:40:05 CST; 28s ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 30171 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Process: 29061 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Main PID: 30285 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─30285 /usr/sbin/httpd -DFOREGROUND
├─30289 /usr/sbin/httpd -DFOREGROUND
├─30290 /usr/sbin/httpd -DFOREGROUND
├─30291 /usr/sbin/httpd -DFOREGROUND
├─30292 /usr/sbin/httpd -DFOREGROUND
└─30293 /usr/sbin/httpd -DFOREGROUND
2月 18 19:40:00 node02 systemd[1]: Starting The Apache HTTP Server...
2月 18 19:40:05 node02 httpd[30285]: AH00558: httpd: Could not reliably determine...ge
2月 18 19:40:05 node02 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
自定义grains,在/etc/salt里面创建grains文件,并且定义一个key,然后就可以直接查询到这个key
[root@node01 ~]# cd /etc/salt/
[root@node01 salt]# cat grains
wangqing: runtime
[root@node01 salt]# salt '*' grains.get wangqing
node01:
runtime
node02:
2.SaltStack组件之Pillar
在配置文件里面定义pillar的环境变量,重启服务
[root@node01 ~]# cd /etc/salt/
cloud.deploy.d cloud.providers.d master.d minion_id proxy.d
[root@node01 salt]# vi master
[root@node01 salt]# head -827 master|tail -3
pillar_roots:
base:
- /srv/pillar/base
[root@node01 base]# systemctl restart salt-master
创建base环境定义的状态文件,如果是redhat就匹配httpd,如果是debian就匹配apache2,
[root@node01 salt]# mkdir -p /srv/pillar/base
[root@node01 salt]# cd /srv/pillar/base
[root@node01 base]# vi /srv/pillar/base/apache.sls
[root@node01 base]# cat /srv/pillar/base/apache.sls
{% if grains['os'] == 'RedHat' %}
apache: httpd
{% elif grains['os'] == 'Debian' %}
apache: apache2
{% endif %}
定义top.file文件
[root@node01 base]# ls
apache.sls top.sls web
[root@node01 base]# cat top.sls
base:
'node02':
- apache
查看pillar的信息
[root@node01 base]# salt '*' pillar.items
node01:
----------
node02:
----------
apache:
httpd
创建apache的状态文件,
[root@node01 base]# pwd
/srv/pillar/base
[root@node01 base]# mkdir -p web/apache
[root@node01 base]# touch web/apache/apache.sls
[root@node01 base]# ls
apache.sls top.sls web
[root@node01 base]# cat web/apache/apache.sls
apache-install:
pkg,installed:
- name: {{ pillar['apache'] }}
apache.service:
service.running:
- name:{{ pillar['apache'] }}
- enable: True
查看node02的httpd信息是停止状态
[root@node02 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: inactive (dead) since 二 2020-02-18 20:55:12 CST; 1s ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 30496 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Process: 29061 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Process: 30285 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=0/SUCCESS)
Main PID: 30285 (code=exited, status=0/SUCCESS)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
2月 18 19:40:00 node02 systemd[1]: Starting The Apache HTTP Server...
2月 18 19:40:05 node02 httpd[30285]: AH00558: httpd: Could not reliably determine...ge
2月 18 19:40:05 node02 systemd[1]: Started The Apache HTTP Server.
2月 18 20:55:11 node02 systemd[1]: Stopping The Apache HTTP Server...
2月 18 20:55:12 node02 systemd[1]: Stopped The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
然后用state的高级状态模块来执行
[root@node01 base]# salt '*' state.highstate
node01:
----------
ID: states
Function: no.None
Result: False
Comment: No Top file or master_tops data matches found. Please see master log for details.
Changes:
Summary for node01
------------
Succeeded: 0
Failed: 1
------------
Total states run: 1
Total run time: 0.000 ms
node02:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 20:56:40.454786
Duration: 706.348 ms
Changes:
----------
ID: apache.service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 20:56:41.162040
Duration: 91.224 ms
Changes:
----------
ID: samba-install
Function: pkg.installed
Name: samba
Result: True
Comment: All specified packages are already installed
Started: 20:56:41.253976
Duration: 20.815 ms
Changes:
----------
ID: samba-service
Function: service.running
Name: nmb
Result: True
Comment: The service nmb is already running
Started: 20:56:41.275336
Duration: 50.581 ms
Changes:
----------
ID: samba-service
Function: service.running
Name: smb
Result: True
Comment: The service smb is already running
Started: 20:56:41.326198
Duration: 64.611 ms
Changes:
Summary for node02
------------
Succeeded: 5
Failed: 0
------------
Total states run: 5
Total run time: 933.579 ms
ERROR: Minions returned with non-zero exit code
查看node02的httpd的状态已经启动
[root@node02 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: inactive (dead) since 二 2020-02-18 20:55:12 CST; 1s ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 30496 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Process: 29061 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Process: 30285 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=0/SUCCESS)
Main PID: 30285 (code=exited, status=0/SUCCESS)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
2月 18 19:40:00 node02 systemd[1]: Starting The Apache HTTP Server...
2月 18 19:40:05 node02 httpd[30285]: AH00558: httpd: Could not reliably determine...ge
2月 18 19:40:05 node02 systemd[1]: Started The Apache HTTP Server.
2月 18 20:55:11 node02 systemd[1]: Stopping The Apache HTTP Server...
2月 18 20:55:12 node02 systemd[1]: Stopped The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@node02 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since 二 2020-02-18 20:55:27 CST; 5s ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 30496 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Process: 29061 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Main PID: 30552 (httpd)
Status: "Processing requests..."
CGroup: /system.slice/httpd.service
├─30552 /usr/sbin/httpd -DFOREGROUND
├─30553 /usr/sbin/httpd -DFOREGROUND
├─30554 /usr/sbin/httpd -DFOREGROUND
├─30555 /usr/sbin/httpd -DFOREGROUND
├─30557 /usr/sbin/httpd -DFOREGROUND
└─30558 /usr/sbin/httpd -DFOREGROUND
2月 18 20:55:26 node02 systemd[1]: Starting The Apache HTTP Server...
2月 18 20:55:27 node02 httpd[30552]: AH00558: httpd: Could not reliably determine...ge
2月 18 20:55:27 node02 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.