自动化运维工具saltstack03 -- 之SaltStack的数据系统

SaltStack数据系统

        saltstack有两种数据系统:grains与pillar

1、SaltStack数据系统之grains

        grains可以收集minion端的静态数据(即机器启动时收集一次,机器运行时出现修该参数不会收集),可以做匹配(可以匹配操作系统等),可以用在配置管理操作。

1.1、grains的三种用法

  •  可以收集基础信息
  •  可以在minion客户端匹配Grains进行批量执行
  •  在top.sls里面匹配Grains进行批量执行

1.2、列出所有grains的名称

# salt 'saltstack-41' grains.ls   #列出所有grains的名称
saltstack-41:
    - SSDs
    - biosreleasedate
    - biosversion
    - cpu_flags
    - cpu_model
    - cpuarch
    - domain
    - fqdn
    - fqdn_ip4
    - fqdn_ip6
    - gpus
    - host
    - hwaddr_interfaces
    - id
    - init
    - ip4_interfaces
    - ip6_interfaces
    - ip_interfaces
    - ipv4
    - ipv6
    - kernel
    - kernelrelease
    - locale_info
    - localhost
    - lsb_distrib_id
    - machine_id
    - manufacturer
    - master
    - mdadm
    - mem_total
    - nodename
    - num_cpus
    - num_gpus
    - os
    - os_family
    - osarch
    - oscodename
    - osfinger
    - osfullname
    - osmajorrelease
    - osrelease
    - osrelease_info
    - path
    - productname
    - ps
    - pythonexecutable
    - pythonpath
    - pythonversion
    - saltpath
    - saltversion
    - saltversioninfo
    - selinux
    - serialnumber
    - server_id
    - shell
    - systemd
    - virtual
- zmqversion

1.3、显示grains名称对应的内容

# salt 'saltstack-41' grains.items   #显示Grains的所有的内容
saltstack-41:
    ----------
    SSDs:
    biosreleasedate:
        07/02/2015
    biosversion:
        6.00
    cpu_flags:
        - fpu
        - vme
        - de
        - pse
        - tsc
        - msr
        - pae
        - mce
        - cx8
        - apic
        - sep
        - mtrr
        - pge
        - mca
        - cmov
        - pat
        - pse36
        - clflush
        - dts
        - mmx
        - fxsr
        - sse
        - sse2
        - ss
        - syscall
        - nx
        - pdpe1gb
        - rdtscp
        - lm
        - constant_tsc
        - arch_perfmon
        - pebs
        - bts
        - nopl
        - xtopology
        - tsc_reliable
        - nonstop_tsc
        - aperfmperf
        - 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
        - ida
        - arat
        - epb
        - pln
        - pts
        - dtherm
        - fsgsbase
        - tsc_adjust
        - bmi1
        - avx2
        - smep
        - bmi2
        - invpcid
        - rdseed
        - adx
        - smap
        - xsaveopt
    cpu_model:
        Intel(R) Core(TM) i5-5200U CPU @ 2.20GHz
    cpuarch:
        x86_64
    domain:
    fqdn:
        saltstack-41
    fqdn_ip4:
        - 172.16.1.41
    fqdn_ip6:
        - fe80::89d4:576f:7497:41b
        - fe80::8dbd:f1ae:d9d3:b966
        - fe80::5bae:377:65d3:7d6b
        - fe80::3ac2:51c0:e6ac:1471
        - fe80::5248:a475:ac57:9b3e
        - fe80::5c8:50dd:5038:5eba
    gpus:
        |_
          ----------
          model:
              SVGA II Adapter
          vendor:
              unknown
    host:
        saltstack-41
    hwaddr_interfaces:
        ----------
        eth0:
            00:0c:29:ab:27:4d
        eth1:
            00:0c:29:ab:27:57
        lo:
            00:00:00:00:00:00
    id:
        saltstack-41
    init:
        systemd
    ip4_interfaces:
        ----------
        eth0:
            - 10.0.0.41
        eth1:
            - 172.16.1.41
        lo:
            - 127.0.0.1
    ip6_interfaces:
        ----------
        eth0:
            - fe80::5c8:50dd:5038:5eba
            - fe80::8dbd:f1ae:d9d3:b966
            - fe80::5248:a475:ac57:9b3e
        eth1:
            - fe80::89d4:576f:7497:41b
            - fe80::5bae:377:65d3:7d6b
            - fe80::3ac2:51c0:e6ac:1471
        lo:
            - ::1
    ip_interfaces:
        ----------
        eth0:
            - 10.0.0.41
            - fe80::5c8:50dd:5038:5eba
            - fe80::8dbd:f1ae:d9d3:b966
            - fe80::5248:a475:ac57:9b3e
        eth1:
            - 172.16.1.41
            - fe80::89d4:576f:7497:41b
            - fe80::5bae:377:65d3:7d6b
            - fe80::3ac2:51c0:e6ac:1471
        lo:
            - 127.0.0.1
            - ::1
    ipv4:
        - 10.0.0.41
        - 127.0.0.1
        - 172.16.1.41
    ipv6:
        - ::1
        - fe80::3ac2:51c0:e6ac:1471
        - fe80::5248:a475:ac57:9b3e
        - fe80::5bae:377:65d3:7d6b
        - fe80::5c8:50dd:5038:5eba
        - fe80::89d4:576f:7497:41b
        - fe80::8dbd:f1ae:d9d3:b966
    kernel:
        Linux
    kernelrelease:
        3.10.0-514.el7.x86_64
    locale_info:
        ----------
        defaultencoding:
            UTF-8
        defaultlanguage:
            en_US
        detectedencoding:
            UTF-8
    localhost:
        saltstack-41
    lsb_distrib_id:
        CentOS Linux
    machine_id:
        12bba36e67de4c8e94aa369560cfdbfd
    manufacturer:
        VMware, Inc.
    master:
        172.16.1.41
    mdadm:
    mem_total:
        590
    nodename:
        saltstack-41
    num_cpus:
        1
    num_gpus:
        1
    os:
        CentOS
    os_family:
        RedHat
    osarch:
        x86_64
    oscodename:
        Core
    osfinger:
        CentOS Linux-7
    osfullname:
        CentOS Linux
    osmajorrelease:
        7
    osrelease:
        7.3.1611
    osrelease_info:
        - 7
        - 3
        - 1611
    path:
        /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
    productname:
        VMware Virtual Platform
    ps:
        ps -efH
    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:
        2015.5.10
    saltversioninfo:
        - 2015
        - 5
        - 10
        - 0
    selinux:
        ----------
        enabled:
            False
        enforced:
            Disabled
    serialnumber:
        VMware-56 4d 60 e3 e6 1a a1 7a-bc 86 4c d5 ac ab 27 4d
    server_id:
        1040546676
    shell:
        /bin/sh
    systemd:
        ----------
        features:
            +PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN
        version:
            219
    virtual:
        VMware
    zmqversion:
        3.2.5

1.3.1、显示指定grains名称对应的值

# salt 'saltstack-41' grains.item fqdn   #查看Grains信息里面的单条
saltstack-41:
    ----------
    fqdn:
        saltstack-41

1.4、通过grains获取eth0网卡的地址

# salt 'salt*' grains.get ip_interfaces:eth0   #显示所有eth0的网卡信息
saltstack-41:
    - 10.0.0.41    #ipv4的ip地址
    - fe80::5248:a475:ac57:9b3e     #ipv6的ip地址

1.5、通过grains获取服务器系统名称

# salt 'saltstack-41' grains.get os
saltstack-41:
    CentOS

1.6、通过grains匹配系统,在指定类型的系统上执行一条命令

# salt -G os:CentOS cmd.run 'w'
saltstack-41:
     00:52:41 up  6:53,  1 user,  load average: 0.05, 0.04, 0.05
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/1    10.0.0.1         21:24    1.00s  0.90s  0.33s /usr/bin/python /usr/bin/salt -G os:CentOS cmd.run w
db02-36:
     17:49:37 up  6:53,  1 user,  load average: 0.00, 0.01, 0.05
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/1    10.0.0.1         14:21   46:49   0.28s  0.28s -bash

注:-G:指使用Grains进行匹配,匹配到centos系统则执行w命令

同理,如果说以后想在有eth0网卡的服务器上批量执行一个命令就可以这样匹配,在vmware虚拟机上批量执行一个语句等等。

1.7、在minion端指定grains名称,然后在master端匹配到指定的grains名称就执行想要执行的指令

1、minion端指定grains名称
# vim /etc/salt/minion
# sed -n '88,91p' /etc/salt/minion    #去掉注释就好,不要添加其他的空格
grains:
  roles:
    - webserver
    - memcache

2、重启minion端
# systemctl restart salt-minion

3、master端匹配grains名称,然后执行指定
# salt -G 'roles:memcache' cmd.run 'echo "hehe"'
saltstack-41:
hehe
注:在所有minion里面配置了roles:memcache的机器上执行echo "hehe"

1.7.1、如果觉得在minion里配置不方便,可以在minion端单独写一个文件指定grains名称

1、如果写/etc/salt/grains文件为定义grains名称文件,内容为:    
# cat /etc/salt/grains
web: nginx    #注意冒号后面有一个空格

2、重启minion
# systemctl restart salt-minion

3、在master端匹配minion端的grains名称并执行指定命令:
# salt -G 'web:nginx' cmd.run "echo 1111"
saltstack-41:
    1111
注:即在/etc/salt/grains文件里匹配到web:nginx配置的服务器则批量执行echo 1111

1.8、在top.sls(高级状态)里匹配grains并执行指定的指令

# cat /srv/salt/top.sls
base:    #定义base
  'web:nginx':   #minion端匹配了web:nginx
    - match: grain   #匹配方式:Grains
    - apache    #以上两条都匹配了则执行apache的状态

1.8.1、配置后执行高级状态

# salt '*' state.highstate

2、SaltStack数据系统之pillar

        Pillar给minion(客户端)指定它想要的数据,(给那个minion指定那个minion才能看得到数据。)Pillar存放在master端,

2.1、查看pillar

# salt '*' pillar.items
db02-36:
    ----------    #没有
saltstack-41:
    ----------

2.2、设置pillar

1、master端配置pillar
# sed -n '529,531p' /etc/salt/master   #取消一下行的注释
pillar_roots:
  base:
- /srv/pillar

2、创建Pillar目录
# mkdir /srv/pillar

3、设置后重启master
# systemctl restart salt-master

4、写pillar状态
# cat /srv/pillar/apache.sls
{% if grains['os'] == 'CentOS' %}   #如果是CentOS系统
apache: httpd     #就显示httpd
{% elif grains['os'] == 'Debian' %}    #如果是Debian系统
apache: apche2   #就显示apche2
{% endif %}

2.3、在top.sls(高级状态)里设置那台minion端可以查看pillar状态

# cat /srv/pillar/top.sls   #在top.sls文件里指定那个客户端可以查看pillar状态
base:
  '*':    #设置所有主机(*表示所有)可以查看apache的状态,也可以指定单个主机
    - apache    #可以查看的状态

2.3.1、设置后在minion端查看pillar

# salt '*' pillar.items     #查看pillar的命令
db02-36:    #由于是centos系统
    ----------
    apache:    #所以查看到的状态是apache
        httpd
saltstack-41:
    ----------
    apache:
        httpd

2.4、用pillar定位主机

-I:匹配pillar

# salt '*' saltutil.refresh_pillar   #定位主机前需要先刷新一下,通知设置pillar客户端
db02-36:
    True
saltstack-41:
    True
# salt -I 'apache:httpd' test.ping  #然后定位存在apache:httpd的主机
db02-36:
    True   #表示成功
saltstack-41:
    True

grains和pillar的区别

 

posted @ 2018-09-26 10:13  欧-阳  阅读(248)  评论(0编辑  收藏  举报