saltstack

Posted 码出未来_远

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了saltstack相关的知识,希望对你有一定的参考价值。

saltstack(2)

YAML

YAML 是 “YAML Ain’t a Markup Language”(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:“Yet Another Markup Language”(仍是一种标记语言)。

YAML 的语法和其他高级语言类似,并且可以简单表达清单、散列表,标量等数据形态。它使用空白符号缩进和大量依赖外观的特色,特别适合用来表达或编辑数据结构、各种配置文件、倾印调试内容、文件大纲(例如:许多电子邮件标题格式和YAML非常接近)。

YAML 的配置文件后缀为 .yml,如:runoob.yml

基本语法

  • 大小写敏感
  • 使用缩进表示层级关系
  • 缩进不允许使用tab,只允许空格
  • 缩进的空格数不重要,只要相同层级的元素左对齐即可
  • '#'表示注释

数据类型

YAML 支持以下几种数据类型:

  • 对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
  • 数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
  • 纯量(scalars):单个的、不可再分的值

YAML 对象

对象键值对使用冒号结构表示 key: value,冒号后面要加一个空格。

也可以使用 key:{key1: value1, key2: value2, …}

还可以使用缩进表示层级关系;

key: 
    child-key: value
    child-key2: value2

较为复杂的对象格式,可以使用问号加一个空格代表一个复杂的 key,配合一个冒号加一个空格代表一个 value:

?  
    - complexkey1
    - complexkey2
:
    - complexvalue1
    - complexvalue2

意思即对象的属性是一个数组 [complexkey1,complexkey2],对应的值也是一个数组 [complexvalue1,complexvalue2]

YAML 数组

- 开头的行表示构成一个数组:

- A
- B
- C

YAML 支持多维数组,可以使用行内表示:

key: [value1, value2, ...]

数据结构的子成员是一个数组,则可以在该项下面缩进一个空格。

-
 - A
 - B
 - C

一个相对复杂的例子:

companies:
    -
        id: 1
        name: company1
        price: 200W
    -
        id: 2
        name: company2
        price: 500W

意思是 companies 属性是一个数组,每一个数组元素又是由 id、name、price 三个属性构成。

数组也可以使用流式(flow)的方式表示:

companies: [{id: 1,name: company1,price: 200W},{id: 2,name: company2,price: 500W}]

复合结构

数组和对象可以构成复合结构,例:

languages:
  - Ruby
  - Perl
  - Python 
websites:
  YAML: yaml.org 
  Ruby: ruby-lang.org 
  Python: python.org 
  Perl: use.perl.org

转换为 json 为:

{ 
  languages: [ 'Ruby', 'Perl', 'Python'],
  websites: {
    YAML: 'yaml.org',
    Ruby: 'ruby-lang.org',
    Python: 'python.org',
    Perl: 'use.perl.org' 
  } 
}

纯量

纯量是最基本的,不可再分的值,包括:

  • 字符串
  • 布尔值
  • 整数
  • 浮点数
  • Null
  • 时间
  • 日期

使用一个例子来快速了解纯量的基本使用:

boolean: 
    - TRUE  #true,True都可以
    - FALSE  #false,False都可以
float:
    - 3.14
    - 6.8523015e+5  #可以使用科学计数法
int:
    - 123
    - 0b1010_0111_0100_1010_1110    #二进制表示
null:
    nodeName: 'node'
    parent: ~  #使用~表示null
string:
    - 哈哈
    - 'Hello world'  #可以使用双引号或者单引号包裹特殊字符
    - newline
      newline2    #字符串可以拆成多行,每一行会被转化成一个空格
date:
    - 2018-02-17    #日期必须使用ISO 8601格式,即yyyy-MM-dd
datetime: 
    -  2018-02-17T15:02:31+08:00    #时间使用ISO 8601格式,时间和日期之间使用T连接,最后使用+代表时区

引用

& 锚点和 ***** 别名,可以用来引用:

defaults: &defaults
  adapter:  postgres
  host:     localhost

development:
  database: myapp_development
  <<: *defaults

test:
  database: myapp_test
  <<: *defaults

相当于:

defaults:
  adapter:  postgres
  host:     localhost

development:
  database: myapp_development
  adapter:  postgres
  host:     localhost

test:
  database: myapp_test
  adapter:  postgres
  host:     localhost

& 用来建立锚点(defaults),<< 表示合并到当前数据,***** 用来引用锚点。

下面是另一个例子:

- &showell Steve 
- Clark 
- Brian 
- Oren 
- *showell 

转为 javascript 代码如下:

[ 'Steve', 'Clark', 'Brian', 'Oren', 'Steve' ]

创建目录

[root@master salt]# mkdir -p /srv/salt/{test,base,dev,prod}
[root@master salt]# tree /srv/
/srv/
└── salt
    ├── base
    ├── dev
    ├── prod
    └── test

修改配置文件,添加状态和环境

[root@master ~]# cd /etc/salt/
[root@master salt]# ls
cloud         cloud.deploy.d  cloud.profiles.d   master    minion    minion_id  proxy    roster
cloud.conf.d  cloud.maps.d    cloud.providers.d  master.d  minion.d  pki        proxy.d
[root@master salt]# vim master  //base必须有
file_roots:
  base:
    - /srv/salt/base
  test:
    - /srv/salt/test


重启服务并测试

[root@master salt]# systemctl restart salt-master
[root@master salt]# salt '*' test.ping
minion:
    True
master:
    True
192.168.207.144:
    True

创建sls文件

[root@master salt]# cd /srv/salt/base/
[root@master base]# mkdir -p /web/nginx
[root@master nginx]# pwd
/srv/salt/base/web/nginx
[root@master nginx]# vim nginx.sls 

nginx-install:
  pkg.installed:
    - name: nginx

nginx-service:
  service.running:
    - name: nginx
    - enable: true

执行

[root@master nginx]# salt 'minion' state.sls web.nginx.nginx
[root@minion ~]# ss -antl
State           Recv-Q          Send-Q                   Local Address:Port                     Peer Address:Port          Process          
LISTEN          0               128                            0.0.0.0:80                            0.0.0.0:*                              
LISTEN          0               128                            0.0.0.0:22                            0.0.0.0:*                              
LISTEN          0               128                               [::]:80                               [::]:*                              
LISTEN          0               128                               [::]:22                               [::]:*      
//成功

image-20210703162528662

[root@master nginx]# mv nginx.sls install.sls
[root@master nginx]# ls
install.sls
[root@master nginx]# salt 'minion' state.sls web.nginx.install

top file和highstate

op file介绍

直接通过命令执行sls文件时够自动化吗?答案是否定的,因为我们还要告诉某台主机要执行某个任务,自动化应该是我们让它干活时,它自己就知道哪台主机要干什么活,但是直接通过命令执行sls文件并不能达到这个目的,为了解决这个问题,top file 应运而生。

top file就是一个入口,top file的文件名可通过在 Master的配置文件中搜索top.sls找出,且此文件必须在 base 环境中,默认情况下此文件必须叫top.sls。

top file的作用就是告诉对应的主机要干什么活,比如让web服务器启动web服务,让数据库服务器安装mysql等等。

[root@master base]# pwd
/srv/salt/base
[root@master base]# vim top.sls
base:
  minion:
    - web.nginx.install
[root@minion ~]# systemctl stop nginx
[root@minion ~]# ss -antl
State           Recv-Q          Send-Q                   Local Address:Port                     Peer Address:Port          Process          
LISTEN          0               128                            0.0.0.0:22                            0.0.0.0:*                              
LISTEN          0               128                               [::]:22                               [::]:*
[root@master base]# salt 'minion' state.highstate
[root@minion ~]# ss -antl
State           Recv-Q          Send-Q                   Local Address:Port                     Peer Address:Port          Process          
LISTEN          0               128                            0.0.0.0:80                            0.0.0.0:*                              
LISTEN          0               128                            0.0.0.0:22                            0.0.0.0:*                              
LISTEN          0               128                               [::]:80                               [::]:*                              
LISTEN          0               128                               [::]:22                               [::]:*     

测试执行效果(不会真正执行)

[root@master base]# salt 'minion' state.highstate test=True

SaltStack数据系统

SaltStack有两大数据系统,分别是:

  • Grains
  • Pillar

SaltStack数据系统组件

SaltStack组件之Grains

GrainsSaltStack的一个组件,其存放着minion启动时收集到的信息。

GrainsSaltStack组件中非常重要的组件之一,因为我们在做配置部署的过程中会经常使用它,GrainsSaltStack记录minion的一些静态信息的组件。可简单理解为Grains记录着每台minion的一些常用属性,比如CPU、内存、磁盘、网络信息等。我们可以通过grains.items查看某台minion的所有Grains信息。

Grains的功能:

  • 收集资产信息

Grains应用场景:

  • 信息查询
  • 在命令行下进行目标匹配
  • 在top file中进行目标匹配
  • 在模板中进行目标匹配

模板中进行目标匹配请看:https://docs.saltstack.com/en/latest/topics/pillar/

信息查询实例:

//列出所有grains的key和value
[root@master base]# salt 'minion' grains.items
minion:
    ----------
    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
        - nopl
        - xtopology
        - tsc_reliable
        - nonstop_tsc
        - cpuid
        - 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
        - cpuid_fault
        - invpcid_single
        - pti
        - ssbd
        - ibrs
        - ibpb
        - stibp
        - fsgsbase
        - tsc_adjust
        - bmi1
        - avx2
        - smep
        - bmi2
        - invpcid
        - rdseed
        - adx
        - smap
        - clflushopt
        - xsaveopt
        - xsavec
        - xgetbv1
        - xsaves
        - arat
        - md_clear
        - flush_l1d
        - arch_capabilities
    cpu_model:
        Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    cpuarch:
        x86_64
    cwd:
        /
    disks:
        - sr0
        - sda
    dns:
        ----------
        domain:
        ip4_nameservers:
            - 192.168.207.2
        ip6_nameservers:
        nameservers:
            - 192.168.207.2
        options:
        search:
            - localdomain
        sortlist:
    domain:
    fqdn:
        minion
    fqdn_ip4:
        - 192.168.207.143
    fqdn_ip6:
        - fe80::20c:29ff:fea6:9e07
    fqdns:
        - minion
    gid:
        0
    gpus:
        |_
          ----------
          model:
              SVGA II Adapter
          vendor:
              vmware
    groupname:
        root
    host:
        minion
    hwaddr_interfaces:
        ----------
        ens33:
            00:0c:29:a6:9e:07
        lo:
            00:00:00:00:00:00
    id:
        minion
    init:
        systemd
    ip4_gw:
        192.168.207.2
    ip4_interfaces:
        ----------
        ens33:
            - 192.168.207.143
        lo:
            - 127.0.0.1
    ip6_gw:
        False
    ip6_interfaces:
        ----------
        ens33:
            - fe80::20c:29ff:fea6:9e07
        lo:
            - ::1
    ip_gw:
        True
    ip_interfaces:
        ----------
        ens33:
            - 192.168.207.143
            - fe80::20c:29ff:fea6:9e07
        lo:
            - 127.0.0.1
            - ::1
    ipv4:
        - 127.0.0.1
        - 192.168.207.143
    ipv6:
        - ::1
        - fe80::20c:29ff:fea6:9e07
    kernel:
        Linux
    kernelparams:
        |_
          - BOOT_IMAGE
          - (hd0,msdos1)/vmlinuz-4.18.0-294.el8.x86_64
        |_
          - root
          - /dev/mapper/cs-root
        |_
          - ro
          - None
        |_
          - crashkernel
          - auto
        |_
          - resume
          - /dev/mapper/cs-swap
        |_
          - rd.lvm.lv
          - cs/root
        |_
          - rd.lvm.lv
          - cs/swap
        |_
          - rhgb
          - None
        |_
          - quiet
          - None
    kernelrelease:
        4.18.0-294.el8.x86_64
    kernelversion:
        #1 SMP Mon Mar 15 22:38:42 UTC 2021
    locale_info:
        ----------
        defaultencoding:
            UTF-8
        defaultlanguage:
            en_US
        detectedencoding:
            UTF-8
        timezone:
            CST
    localhost:
        minion
    lsb_distrib_codename:
        CentOS Stream 8
    lsb_distrib_id:
        CentOS Stream
    lsb_distrib_release:
        8
    lvm:
        ----------
        cs:
            - root
            - swap
    machine_id:
        4f90c4e08fcc45459b32a145840cce05
    manufacturer:
        VMware, Inc.
    master:
        192.168.207.131
    mdadm:
    mem_total:
        3709
    nodename:
        minion
    num_cpus:
        2
    num_gpus:
        1
    os:
        CentOS Stream
    os_family:
        RedHat
    osarch:
        x86_64
    oscodename:
        CentOS Stream 8
    osfinger:
        CentOS Stream-8
    osfullname:
        CentOS Stream
    osmajorrelease:
        8
    osrelease:
        8
    osrelease_info:
        - 8
    path:
        /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
    pid:
        10649
    productname:
        VMware Virtual Platform
    ps:
        ps -efHww
    pythonexecutable:
        /usr/bin/python3.6
    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:
        3003.1
    saltversioninfo:
        - 3003
        - 1
    selinux:
        ----------
        enabled:
            True
        enforced:
            Permissive
    serialnumber:
        VMware-56 4d d1 41 07 c8 43 ec-15 57 fc 9f 54 a6 9e 07
    server_id:
        279719642
    shell:
        /bin/sh
    ssds:
    swap_total:
        2047
    systemd:
        ----------
        features:
            +PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=legacy
        version:
            239
    systempath:
        - /usr/local/sbin
        - /usr/local/bin
        - /usr/sbin
        - /usr/bin
    uid:
        0
    username:
        root
    uuid:
        41d14d56-c807-ec43-1557-fc9f54a69e07
    virtual:
        VMware
    zfs_feature_flags:
        False
    zfs_support:
        False
    zmqversion:
        4.3.4
//只查询所有的grains的key
[root@master base]# salt 'minion' grains.ls
minion:
    - biosreleasedate
    - biosversion
    - cpu_flags
    - cpu_model
    - cpuarch
    - cwd
    - 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
    - kernelparams
    - kernelrelease
    - kernelversion
    - locale_info
    - localhost
    - lsb_distrib_codename
    - lsb_distrib_id
    - lsb_distrib_release
    - lvm
    - 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
    - ssds
    - swap_total
    - systemd
    - systempath
    - uid
    - username
    - uuid
    - virtual
    - zfs_feature_flags
    - zfs_support
    - zmqversion
//查询某个key的值,比如想获取ip地址
[root@master base]# salt 'minion' grains.get fqdn_ip4
minion:
    - 192.168.207.143
[root@master base]# salt '*' grains.get fqdn_ip4
minion:
    - 192.168.207.143
master:
    - 192.168.207.131
192.168.207.144:
    - 192.168.207.144

目标匹配实例:
Grains来匹配minion

//在所有centos系统中执行命令
[root@master base]# salt -G 'os:CentOS' cmd.run 'uptime'
192.168.207.144:
     20:16:07 up  5:49,  1 user,  load average: 0.00, 0.01, 0.05
[root@master base]# salt -G 'os:CentOS Stream' cmd.run 'uptime'
minion:
     20:12:16 up  7:01,  1 user,  load average: 0.00, 0.00, 0.00
master:
     20:12:20 up  7:04,  1 user,  load average: 0.00, 0.00, 0.00

在top file里面使用Grains:

[root@master base]# vim /srv/salt/base/top.sls 
base:
  'os:Centos':
    - match: grain
    - web.nginx.install
                            

自定义Grains的两种方法:

  • minion配置文件,在配置文件中搜索grains
  • 在/etc/salt下生成一个grains文件,在此文件中定义(推荐方式)
[root@minion ~]# cd /etc/salt/
[root@minion salt]# vim grains
-bash: vim: command not found
[root@minion salt]# vi grains
[root@minion salt]# 
test-grains: jack
[root@minion salt]# systemctl restart salt-minion
[root@master salt]# salt 'minion' grains.items
····
 test-grains:
        jack
    uid:
        0
    username:
        root
    uuid:
        41d14d56-c807-ec43-1557-fc9f54a69e07
    virtual:
        VMware
···
[root@master salt]# salt 'minion' grains.get test-grains
minion:
    jack

不重启的情况下自定义Grains

[root@minion salt]# vi grains
test-grains: jack
one: two
[root@master salt]# salt 'minion' saltutil.sync_grains
minion:
[root@master salt]# salt 'minion' grains.get one
minion:
    two

SaltStack组件之Pillar

Pillar也是SaltStack组件中非常重要的组件之一,是数据管理中心,经常配置states在大规模的配置管理工作中使用它。Pillar在SaltStack中主要的作用就是存储和定义配置管理中需要的一些数据,比如软件版本号、用户名密码等信息,它的定义存储格式与Grains类似,都是YAML格式。

在Master配置文件中有一段Pillar settings选项专门定义Pillar相关的一些参数:

#pillar_roots:
#  base:
#    - /srv/pillar

默认Base环境下Pillar的工作目录在/srv/pillar目录下。若你想定义多个环境不同的Pillar工作目录,只需要修改此处配置文件即可。

Pillar的特点:

  • 可以给指定的minion定义它需要的数据
  • 只有指定的人才能看到定义的数据
  • 在master配置文件里设置
[root@master salt]# vim /etc/salt/master
pillar_roots:
  base:
    - /srv/pillar/base
[root@master salt]# mkdir -p /srv/pillar/base
[root@master salt]# salt 'minion' pillar.items
minion:
    ----------

默认pillar是没有任何信息的,如果想查看信息,需要在 master 配置文件上把 pillar_opts的注释取消,并将其值设为 True。

[root@master salt]# vim /etc/salt/master
pillar_opts: True
[root@master salt]# salt 'minion' pillar.items
minion:
    ----------
    master:
        ----------
        __cli:
            salt-master
        __role:
            master
        allow_minion_key_revoke:
            True
        archive_jobs:
            False
        auth_events:
            True
        auth_mode:
            1
        auto_accept:
            False
        azurefs_update_interval:
            60
        cache:
            localfs
        cache_sreqs:
            True
        cachedir:
            /var/cache/salt/master
        clean_dynamic_modules:
            True
        cli_summary:
            False
        client_acl_verify:
            True
        cluster_mode:
            False
        con_cache:
            False
        conf_file:
            /etc/salt/master
        config_dir:
            /etc/salt
        cython_enable:
            False
        daemon:
            False
        decrypt_pillar:
        decrypt_pillar_default:
            gpg
        decrypt_pillar_delimiter:
            :
        decrypt_pillar_renderers:
            - gpg
        default_include:
            master.d/*.conf
        default_top:
            base
        discovery:
            False
        django_auth_path:
        django_auth_settings:
        drop_messages_signature_fail:
            False
        dummy_pub:
            False
        eauth_acl_module:
        eauth_tokens:
            localfs
        enable_gpu_grains:
            False
        enable_ssh_minions:
            False
        enforce_mine_cache:
            False
        engines:
        env_order:
        event_match_type:
            startswith
        event_publisher_niceness:
            None
        event_return:
        event_return_blacklist:
        event_return_niceness:
            None
        event_return_queue:
            0
        event_return_whitelist:
        ext_job_cache:
        ext_pillar:
        extension_modules:
            /var/cache/salt/master/extmods
        external_auth:
            ----------
        extmod_blacklist:
            ----------
        extmod_whitelist:
            ----------
        failhard:
            False
        file_buffer_size:
            1048576
        file_client:
            local
        file_ignore_glob:
        file_ignore_regex:
        file_recv:
            False
        file_recv_max_size:
            100
        file_roots:
            ----------
            base:
                - /srv/salt/base
            test:
                - /srv/salt/test
        fileserver_backend:
            - roots
        fileserver_followsymlinks:
            True
        fileserver_ignoresymlinks:
            False
        fileserver_limit_traversal:
            False
        fileserver_update_niceness:
            None
        fileserver_verify_config:
            True
        fips_mode:
            False
        gather_job_timeout:
            10
        git_pillar_base:
            master
        git_pillar_branch:
            master
        git_pillar_env:
        git_pillar_fallback:
        git_pillar_global_lock:
            True
        git_pillar_includes:
            True
        git_pillar_insecure_auth:
            False
        git_pillar_passphrase:
        git_pillar_password:
        git_pillar_privkey:
        git_pillar_pubkey:
        git_pillar_refspecs:
            - +refs/heads/*:refs/remotes/origin/*
            - +refs/tags/*:refs/tags/*
        git_pillar_root:
        git_pillar_ssl_verify:
            True
        git_pillar_update_interval:
            60
        git_pillar_user:
        git_pillar_verify_config:
            True
        gitfs_base:
            master
        gitfs_disable_saltenv_mapping:
            False
        gitfs_fallback:
        gitfs_global_lock:
            True
        gitfs_insecure_auth:
            False
        gitfs_mountpoint:
        gitfs_passphrase:
        gitfs_password:
        gitfs_privkey:
        gitfs_pubkey:
        gitfs_ref_types:
            - branch
            - tag
            - sha
        gitfs_refspecs:
            - +refs/heads/*:refs/remotes/origin/*
            - +refs/tags/*:refs/tags/*
        gitfs_remotes:
        gitfs_root:
        gitfs_saltenv:
        gitfs_saltenv_blacklist:
        gitfs_saltenv_whitelist:
        gitfs_ssl_verify:
            True
        gitfs_update_interval:
            60
        gitfs_user:
        gpg_cache:
            False
        gpg_cache_backend:
            disk
        gpg_cache_ttl:
            86400
        hash_type:
            sha256
        hgfs_base:
            default
        hgfs_branch_method:
            branches
        hgfs_mountpoint:
        hgfs_remotes:
        hgfs_root:
        hgfs_saltenv_blacklist:
        hgfs_saltenv_whitelist:
        hgfs_update_interval:
            60
        http_connect_timeout:
            20.0
        http_max_body:
            107374182400
        http_request_timeout:
            3600.0
        id:
            minion
        interface:
            0.0.0.0
        ipc_mode:
            ipc
        ipc_write_buffer:
            0
        ipv6:
            None
        jinja_env:
            ----------
        jinja_lstrip_blocks:
            False
        jinja_sls_env:
            ----------
        jinja_trim_blocks:
            False
        job_cache:
            True
        job_cache_store_endtime:
            False
        keep_acl_in_token:
            False
        keep_jobs:
            24
        key_cache:
        key_logfile:
            /var/log/salt/key
        key_pass:
            None
        keysize:
            2048
        local:
            True
        lock_saltenv:
            False
        log_datefmt:
            %H:%M:%S
        log_datefmt_console:
            %H:%M:%S
        log_datefmt_logfile:
            %Y-%m-%d %H:%M:%S
        log_file:
            /var/log/salt/master
        log_fmt_console:
            [%(levelname)-8s] %(message)s
        log_fmt_jid:
            [JID: %(jid)s]
        log_fmt_logfile:
            %(asctime)s,%(msecs)03d [%(name)-17s:%(lineno)-4d][%(levelname)-8s][%(process)d] %(message)s
        log_granular_levels:
            ----------
        log_level:
            warning
        log_level_logfile:
            warning
        log_rotate_backup_count:
            0
        log_rotate_max_bytes:
            0
        loop_interval:
            60
        maintenance_niceness:
            None
        master_job_cache:
            local_cache
        master_pubkey_signature:
            master_pubkey_signature
        master_roots:
            ----------
            base:
                - /srv/salt-master
        master_sign_key_name:
            master_sign
        master_sign_pubkey:
            False
        master_stats:
            False
        master_stats_event_iter:
            60
        master_tops:
            ----------
        master_tops_first:
            False
        master_use_pubkey_signature:
            False
        max_event_size:
            1048576
        max_minions:
            0
        max_open_files:
            100000
        memcache_debug:
            False
        memcache_expire_seconds:
            0
        memcache_full_cleanup:
            False
        memcache_max_items:
            1024
        min_extra_mods:
        minion_data_cache:
            True
        minion_data_cache_events:
            True
        minion_id:
            minion
        minionfs_blacklist:
        minionfs_env:
            base
        minionfs_mountpoint:
        minionfs_update_interval:
            60
        minionfs_whitelist:
        module_dirs:
        mworker_niceness:
            None
        mworker_queue_niceness:
            None
        netapi_allow_raw_shell:
            False
        nodegroups:
            ----------
        on_demand_ext_pillar:
            - libvirt
            - virtkey
        open_mode:
            False
        optimization_order:
            - 0
            - 1
            - 2
        order_masters:
            False
        outputter_dirs:
        peer:
            ----------
        permissive_acl:
            False
        permissive_pki_access:
            False
        pidfile:
            /var/run/salt-master.pid
        pillar_cache:
            False
        pillar_cache_backend:
            disk
        pillar_cache_ttl:
            3600
        pillar_includes_override_sls:
            False
        pillar_merge_lists:
            False
        pillar_opts:
            True
        pillar_roots:
            ----------
            base:
                - /srv/pillar/base
        pillar_safe_render_error:
            True
        pillar_source_merging_strategy:
            smart
        pillar_version:
            2
        pillarenv:
            None
        ping_on_rotate:
            False
        pki_dir:
            /etc/salt/pki/master
        preserve_minion_cache:
            False
        pub_hwm:
            1000
        pub_server_niceness:
            None
        publish_port:
            4505
        publish_session:
            86400
        publisher_acl:
            ----------
        publisher_acl_blacklist:
            ----------
        python2_bin:
            python2
        python3_bin:
            python3
        queue_dirs:
        range_server:
            range:80
        reactor:
        reactor_niceness:
            None
        reactor_refresh_interval:
            60
        reactor_worker_hwm:
            10000
        reactor_worker_threads:
            10
        regen_thin:
            False
        renderer:
            jinja|yaml
        renderer_blacklist:
        renderer_whitelist:
        req_server_niceness:
            None
        require_minion_sign_messages:
            False
        ret_port:
            4506
        root_dir:
            /
        roots_update_interval:
            60
        rotate_aes_key:
            True
        runner_dirs:
        runner_returns:
            True
        s3fs_update_interval:
            60
        salt_cp_chunk_size:
            98304
        saltenv:
            None
        saltversion:
            3003.1
        schedule:
            ----------
        search:
        serial:
            msgpack
        show_jid:
            False
        show_timeout:
            True
        sign_pub_messages:
            True
        signing_key_pass:
            None
        sock_dir:
            /var/run/salt/master
        sock_pool_size:
            1
        sqlite_queue_dir:
            /var/cache/salt/master/queues
        ssh_config_file:
            /root/.ssh/config
        ssh_identities_only:
            False
        ssh_list_nodegroups:
            ----------
        ssh_log_file:
            /var/log/salt/ssh
        ssh_passwd:
        ssh_port:
            22
        ssh_priv_passwd:
        ssh_scan_ports:
            22
        ssh_scan_timeout:
            0.01
        ssh_sudo:
            False
        ssh_sudo_user:
        ssh_timeout:
            60
        ssh_use_home_key:
            False
        ssh_user:
            root
        ssl:
            None
        state_aggregate:
            False
        state_auto_order:
            True
        state_events:
            False
        state_output:
            full
        state_output_diff:
            False
        state_output_profile:
            True
        state_top:
            salt://top.sls
        state_top_saltenv:
            None
        state_verbose:
            True
        sudo_acl:
            False
        svnfs_branches:
            branches
        svnfs_mountpoint:
        svnfs_remotes:
        svnfs_root:
        svnfs_saltenv_blacklist:
        svnfs_saltenv_whitelist:
        svnfs_tags:
            tags
        svnfs_trunk:
            trunk
        svnfs_update_interval:
            60
        syndic_dir:
            /var/cache/salt/master/syndics
        syndic_event_forward_timeout:
            0.5
        syndic_failover:
            random
        syndic_forward_all_events:
            False
        syndic_jid_forward_cache_hwm:
            100
        syndic_log_file:
            /var/log/salt/syndic
        syndic_master:
            masterofmasters
        syndic_pidfile:
            /var/run/salt-syndic.pid
        syndic_wait:
            5
        tcp_keepalive:
            True
        tcp_keepalive_cnt:
            -1
        tcp_keepalive_idle:
            300
        tcp_keepalive_intvl:
            -1
        tcp_master_pub_port:
            4512
        tcp_master_publish_pull:
            4514
        tcp_master_pull_port:
            4513
        tcp_master_workers:
            4515
        test:
            False
        thin_extra_mods:
        thorium_interval:
            0.5
        thorium_roots:
            ----------
            base:
                - /srv/thorium
        thorium_top:
            top.sls
        thoriumenv:
            None
        timeout:
            5
        token_dir:
            /var/cache/salt/master/tokens
        token_expire:
            43200
        token_expire_user_override:
            False
        top_file_merging_strategy:
            merge
        transport:
            zeromq
        unique_jid:
            False
        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自定义数据:
在master的配置文件里找pillar_roots可以看到其存放pillar的位置

[root@master ~]# cd /srv/pillar/base/
[root@master base]# ls
[root@master base]# vim nginx.sls
{% if grains['os'] == 'CentOS Stream' %}
nginx: nginx 
{% elif grains['os'] == 'CentOS' %}
nginx: nginx
{% endif %}    
[root@master base]# vim /srv/pillar/base/top.sls
base:
  'minion':
    - nginx
[root@master base]# salt '*' pillar.items
      zmq_monitor:
            False
    nginx:
        nginx
192.168.207.144:
    ----------
//在salt下修改apache的状态文件,引用pillar的数据

[root@master web]# cd nginx/
[root@master nginx]# ls
install.sls
[root@master nginx]# vim install.sls 
nginx-install:
  pkg.installed:
    - name: {{ pillar['nginx'] }}

nginx-service:
  service.running:
    - name: {{ pillar['nginx'] }}
    - enable: True

//执行高级状态文件
[root@master ~]# salt 'minion' state.highstate

Grains与Pillar的区别

存储位置类型采集方式应用场景
Grainsminion静态minion启动时采集 可通过刷新避免重启minion服务1.信息查询 2.在命令行下进行目标匹配 3.在top file中进行目标匹配 4.在模板中进行目标匹配
Pillarmaster动态指定,实时生效1.目标匹配 2.敏感数据配置

以上是关于saltstack的主要内容,如果未能解决你的问题,请参考以下文章

首发分析 | SaltStack远程执行代码多个高危漏洞透析(CVE-2021-25281/25282/25283)

Python操作Saltstack

预警通报关于SaltStack多个高危漏洞的预警通报

漏洞告警SaltStack多个高危漏洞(CVE-2021-25281/25282/25283)

漏洞更新SaltStack 多个高危漏洞(含漏洞分析及深信服解决方案)

Saltstack