saltstack的配置管理與數(shù)據(jù)系統(tǒng)

YAML語言

YAML是一種直觀的能夠被電腦識別的數(shù)據(jù)序列化格式炊甲,是一個可讀性高并且容易被人類閱讀俭缓,容易和腳本語言交互嫡秕,用來表達資料序列的編程語言钙蒙。

它類似于標準通用標記語言的子集XML的數(shù)據(jù)描述語言,語法比XML簡單很多腕铸。

YAML語言的格式如下:

house:
  family:
    name: Doe
    parents:
      - John
      - Jane
    children:
      - Paul
      - Mark
      - Simone
  address:
    number: 34
    street: Main Street
    city: Nowheretown
    zipcode: 12345

YAML的基本規(guī)則:

  • 使用縮進來表示層級關系惜犀,每層2個空格,禁止使用TAB鍵
  • 當冒號不是處于最后時狠裹,冒號后面必須有一個空格
  • 用 - 表示列表虽界,- 的后面必須有一個空格
  • 用 # 表示注釋

YAML配置文件要放到SaltStack讓我們放的位置,可以在SaltStack的 Master 配置文件中查找file_roots即可看到涛菠。

[root@master salt]# pwd
/etc/salt

[root@master salt]# vim master
681 file_roots:
 682   base:
 683     - /srv/salt/base
 684   test:
 685     - /srv/salt/test
 686   dev:
 687     - /srv/salt/dev
 688   prod:
 689     - /srv/salt/prod

[root@master ~]# mkdir -p /srv/salt/{base,test,dev,prod}

[root@master base]# systemctl restart salt-master.service
[root@master base]# systemctl restart salt-minion.service

需要注意:

  • base是默認的位置莉御,如果file_roots只有一個,則base是必備的且必須叫base碗暗,不能改名

使用SaltStack配置一個apache實例

在Master上部署sls配置文件并執(zhí)行

[root@master base]# pwd
/srv/salt/base

[root@master base]# mkdir -p web/apache
[root@master apache]# pwd
/srv/salt/base/web/apache

[root@master apache]# touch apache.sls  //生成一個狀態(tài)描述文件
[root@master apache]# vim apache.sls
apache-install:
  pkg.installed:
    - name: httpd

apache-service:
  service.running:
    - name: httpd
    - enable: True


// YAML 配置文件中頂格寫的被稱作ID颈将,必須全局唯一,不能重復
// SaltStack 讀 YAML 配置文件時是從上往下讀言疗,所以要把先執(zhí)行的寫在前面

在執(zhí)行狀態(tài)文件前先用test.ping測試需要執(zhí)行狀態(tài)文件的主機是否能正常通信晴圾,然后再執(zhí)行狀態(tài)文件。
[root@master salt]# salt 'minion01' test.ping
minion01:
    True

//執(zhí)行狀態(tài)描述文件
[root@master ~]# salt 'minion01' state.sls web.apache.apache saltenv=base
minion01:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: The following packages were installed/updated: httpd
     Started: 15:42:04.287654
    Duration: 18111.181 ms
     Changes:   
              ----------
              apr:
                  ----------
                  new:
                      1.6.3-9.el8
                  old:
              apr-util:
                  ----------
                  new:
                      1.6.1-6.el8
                  old:
              apr-util-bdb:
                  ----------
                  new:
                      1.6.1-6.el8
                  old:
              apr-util-openssl:
                  ----------
                  new:
                      1.6.1-6.el8
                  old:
              httpd:
                  ----------
                  new:
                      2.4.37-21.module+el8.2.0+5008+cca404a3
                  old:
              httpd-filesystem:
                  ----------
                  new:
                      2.4.37-21.module+el8.2.0+5008+cca404a3
                  old:
              httpd-tools:
                  ----------
                  new:
                      2.4.37-21.module+el8.2.0+5008+cca404a3
                  old:
              mailcap:
                  ----------
                  new:
                      2.1.48-3.el8
                  old:
              mod_http2:
                  ----------
                  new:
                      1.11.3-3.module+el8.2.0+4377+dc421495
                  old:
              redhat-logos-httpd:
                  ----------
                  new:
                      81.1-1.el8
                  old:
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd has been enabled, and is running
     Started: 15:42:22.458329
    Duration: 758.569 ms
     Changes:   
              ----------
              httpd:
                  True

Summary for minion01
------------
Succeeded: 2 (changed=2)
Failed:    0
------------
Total states run:     2
Total run time:  18.870 s

在minion上檢查apache的狀態(tài)
[root@minion01 salt]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: d>
   Active: active (running) since Tue 2021-11-02 15:42:22 CST; 40min ago
     Docs: man:httpd.service(8)
 Main PID: 111233 (httpd)

使用SaltStack在minion02上配置nginx實例

在Master上部署sls配置文件并執(zhí)行
[root@master web]# pwd
/srv/salt/base/web
[root@master web]# mkdir 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 base]# salt 'minion02' state.sls web.nginx.nginx saltenv=base
minion02:
----------
          ID: nginx-install
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: The following packages were installed/updated: nginx
     Started: 18:36:09.485277
    Duration: 11087.069 ms
     Changes:   
              ----------
              centos-indexhtml:
                  ----------
                  new:
                      7-9.el7.centos
                  old:
              gperftools-libs:
                  ----------
                  new:
                      2.6.1-1.el7
                  old:
              nginx:
                  ----------
                  new:
                      1:1.20.1-9.el7
                  old:
              nginx-filesystem:
                  ----------
                  new:
                      1:1.20.1-9.el7
                  old:
              openssl11-libs:
                  ----------
                  new:
                      1:1.1.1g-3.el7
                  old:
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: True
     Comment: Service nginx has been enabled, and is running
     Started: 18:36:20.603264
    Duration: 192.469 ms
     Changes:   
              ----------
              nginx:
                  True

Summary for minion02
------------
Succeeded: 2 (changed=2)
Failed:    0
------------
Total states run:     2
Total run time:  11.280 s

在minion02上檢查

[root@minion02 ~]# systemctl status nginx.service 
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2021-11-02 18:36:20 CST; 1min 19s ago
 Main PID: 20747 (nginx)

top file

top file的介紹

直接通過命令執(zhí)行sls文件時夠自動化嗎噪奄?答案是否定的死姚,因為我們還要告訴某臺主機要執(zhí)行某個任務,自動化應該是我們讓它干活時勤篮,它自己就知道哪臺主機要干什么活都毒,但是直接通過命令執(zhí)行sls文件并不能達到這個目的,為了解決這個問題碰缔,top file 應運而生偿洁。

top file就是一個入口逮诲,top file的文件名可通過在 Master的配置文件中搜索top.sls找出秸应,且此文件必須在 base 環(huán)境中荆针,默認情況下此文件必須叫top.sls。

top file的作用就是告訴對應的主機要干什么活梗肝,比如讓web服務器啟動web服務榛瓮,讓數(shù)據(jù)庫服務器安裝mysql等等。

top file 實例:

[root@master base]# pwd
/srv/salt/base

[root@master base]# touch top.sls
[root@master base]# vim top.sls
base:   //要執(zhí)行狀態(tài)文件的環(huán)境
  'minion01':  //要執(zhí)行狀態(tài)文件的目標
    - web.apache.apache  //要執(zhí)行的狀態(tài)文件

//停掉minion01上的httpd服務巫击,進行測試
[root@minion01 salt]# systemctl stop httpd.service

[root@master base]# salt 'minion01' state.highstate  //使用高級狀態(tài)來執(zhí)行
minion01:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 16:31:02.863779
    Duration: 713.364 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is running
     Started: 16:31:03.580843
    Duration: 362.207 ms
     Changes:   
              ----------
              httpd:
                  True

Summary for minion01
------------
Succeeded: 2 (changed=1)
Failed:    0
------------
Total states run:     2
Total run time:   1.076 s

再來查看minion的httpd狀態(tài)
[root@minion01 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor p>
   Active: active (running) since Tue 2021-11-02 16:31:03 CST; 1h 6min ago
     Docs: man:httpd.service(8)
 Main PID: 201195 (httpd)

使用top file來管理minion02上的nginx

[root@master base]# pwd
/srv/salt/base
[root@master base]# vim top.sls
base:
  'minion02':
    - web.nginx.nginx

[root@minion02 ~]# systemctl stop nginx.service

[root@master base]# salt 'minion02' state.highstate
minion02:
----------
          ID: nginx-install
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: All specified packages are already installed
     Started: 18:46:06.330211
    Duration: 631.054 ms
     Changes:   
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: True
[root@master base]# salt 'minion02' state.highstate
minion02:
----------
          ID: nginx-install
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: All specified packages are already installed
     Started: 18:46:38.797394
    Duration: 626.71 ms
     Changes:   
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: True
     Comment: Service nginx is already enabled, and is running
     Started: 18:46:39.426389
    Duration: 115.327 ms
     Changes:   
              ----------
              nginx:
                  True

Summary for minion02
------------
Succeeded: 2 (changed=1)
Failed:    0
------------
Total states run:     2
Total run time: 742.037 ms

[root@minion02 ~]# systemctl status nginx.service 
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2021-11-02 18:46:39 CST; 22s ago

同時管理minion01和02

[root@minion01 ~]# systemctl status httpd
[root@minion02 ~]# systemctl stop nginx.service
[root@master base]# salt '*' state.highstate
[root@master base]# salt '*' state.highstate
minion02:
----------
          ID: nginx-install
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: All specified packages are already installed
     Started: 18:49:51.691119
    Duration: 828.843 ms
     Changes:   
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: True
     Comment: Service nginx is already enabled, and is running
     Started: 18:49:52.523394
    Duration: 179.223 ms
     Changes:   
              ----------
              nginx:
                  True

Summary for minion02
------------
Succeeded: 2 (changed=1)
Failed:    0
------------
Total states run:     2
Total run time:   1.008 s
minion01:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 18:49:50.300689
    Duration: 625.583 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is running
     Started: 18:49:50.928605
    Duration: 315.648 ms
     Changes:   
              ----------
              httpd:
                  True

Summary for minion01
------------
Succeeded: 2 (changed=1)
Failed:    0
------------
Total states run:     2
Total run time: 941.231 ms

[root@minion01 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: d>
   Active: active (running) since Tue 2021-11-02 18:49:51 CST; 33s ago

[root@minion02 ~]# systemctl status nginx.service 
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2021-11-02 18:49:52 CST; 49s ago

注意: 若top file里面的目標是用表示的禀晓,要注意的是精续,top file里面的表示的是所有要執(zhí)行狀態(tài)的目標,而salt '' state.highstate里面的表示通知所有機器干活粹懒,而是否要干活則是由top file來指定的

高級狀態(tài)highstate的使用

管理SaltStack時一般最常用的管理操作就是執(zhí)行高級狀態(tài)重付。

[root@master salt]# salt '*' state.highstate  //在生產(chǎn)環(huán)境中不要使用此操作

注意: 上面讓所有人執(zhí)行高級狀態(tài),但實際工作中凫乖,一般不會這么用堪夭,工作當中一般都是通知某臺或某些臺目標主機來執(zhí)行高級狀態(tài),具體是否執(zhí)行則是由top file來決定的拣凹。

若在執(zhí)行高級狀態(tài)時加上參數(shù)test=True,則它會告訴我們它將會做什么恨豁,但是它不會真的去執(zhí)行這個操作嚣镜。

停掉minion上的httpd服務
[root@minion01 ~]# systemctl stop httpd

在master上執(zhí)行高級狀態(tài)的測試
[root@master salt]# salt 'minion01' state.highstate test=true
minion01:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 17:54:34.099749
    Duration: 540.741 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: None
     Comment: Service httpd is set to start
     Started: 17:54:34.642736
    Duration: 60.081 ms
     Changes:   

Summary for minion01
------------
Succeeded: 2 (unchanged=1)
Failed:    0
------------
Total states run:     2
Total run time: 600.822 ms

[root@minion01 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: dis>
   Active: inactive (dead) since Tue 2021-11-02 17:52:46 CST; 3min 15s ago
     Docs: man:httpd.service(8)

可以看出httpd并沒有,說明高級命令并沒有執(zhí)行

SaltStack數(shù)據(jù)系統(tǒng)

SaltStack有兩大數(shù)據(jù)系統(tǒng)橘蜜,分別是:

  • Grains
  • Pillar

Grains與Pillar的區(qū)別

數(shù)據(jù)系統(tǒng) 存儲位置 類型 采集方式 應用場景
Grains minion 靜態(tài) minion啟動時采集菊匿;可通過刷新避免重啟minion服務 1. 信息查詢 2. 在命令行下進行目標匹配 3. 在top file中進行目標匹配 4. 在模板中進行目標匹配
Pillar master 動態(tài) 指定,實時生效 1. 目標匹配 2. 敏感數(shù)據(jù)配置

SaltStack組件之Grains

  • Grains是saltstack的一個組件计福,其存放著minion啟動時收集到的信息跌捆。
  • Grains是saltstack組件中非常重要的組件之一,因為我們在做配置部署的過程中會經(jīng)常使用它象颖,Grains是saltstack記錄minion的一些靜態(tài)信息的組件佩厚。可簡單理解為Grains記錄著每臺minion一些常用屬性说订,比如CPU抄瓦、內(nèi)存、磁盤陶冷、網(wǎng)絡信息等钙姊。我們可以通過grains.items查看某臺minion的所有Grains信息。

Grains的功能:

  • 收集資產(chǎn)信息

Grains應用場景:

  • 信息查詢
  • 在命令行下進行目標匹配
  • 在top file中進行目標匹配
  • 在模板中進行目標匹配

模板中的目標匹配請看

信息查詢實列:

列出minion01所有g(shù)rains的key和value值
[root@master base]# salt 'minion01' grains.items
minion01:
    ----------
    biosreleasedate:  //bios的時間
        02/27/2020
    biosversion:   //bios的版本
        6.00
    cpu_flags:   //cpu相關的屬性
        - fpu
        - vme
        - de
        - pse
        - tsc
        - msr
        - pae
        - mce
        - cx8
        - apic
        - sep
        - mtrr
        - pge
        - mca
        - cmov
        - pat
        - pse36
        - clflush
        - mmx
        - fxsr
        - sse
        - sse2
        - ht
        - syscall
        - nx
        - mmxext
        - fxsr_opt
        - pdpe1gb
        - rdtscp
        - lm
        - constant_tsc
        - rep_good
        - nopl
        - tsc_reliable
        - nonstop_tsc
        - cpuid
        - extd_apicid
        - pni
        - pclmulqdq
        - ssse3
        - fma
        - cx16
        - sse4_1
        - sse4_2
        - x2apic
        - movbe
        - popcnt
        - aes
        - xsave
        - avx
        - f16c
        - rdrand
        - hypervisor
        - lahf_lm
        - cmp_legacy
        - svm
        - extapic
        - cr8_legacy
        - abm
        - sse4a
        - misalignsse
        - 3dnowprefetch
        - osvw
        - perfctr_core
        - ssbd
        - ibpb
        - vmmcall
        - fsgsbase
        - bmi1
        - avx2
        - smep
        - bmi2
        - rdseed
        - adx
        - smap
        - clflushopt
        - sha_ni
        - xsaveopt
        - xsavec
        - xsaves
        - clzero
        - arat
        - npt
        - svm_lock
        - nrip_save
        - vmcb_clean
        - flushbyasid
        - decodeassists
        - overflow_recov
        - succor
    cpu_model:    //cpu的具體型號
        AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx
    cpuarch:    //cpu架構(gòu)
        x86_64
    cwd:
        /
    disks:
        - sr0
    dns:
        ----------
        domain:
        ip4_nameservers:
            - 192.168.182.2
        ip6_nameservers:
        nameservers:
            - 192.168.182.2
        options:
        search:
        sortlist:
    domain:
    efi:
        False
    efi-secure-boot:
        False
    fqdn:
        minion01
    fqdn_ip4:    //ip地址
        - 192.168.182.142
    fqdn_ip6:
        - fe80::20c:29ff:fef4:b4a9
    fqdns:
        - minion01
    gid:
        0
    gpus:
        |_
          ----------
          model:
              SVGA II Adapter
          vendor:
              vmware
    groupname:
        root
    host:     //主機名
        minion01
    hwaddr_interfaces:
        ----------
        ens160:
            00:0c:29:f4:b4:a9
        lo:
            00:00:00:00:00:00
    id:      //minion的ID
        minion01
    init:
        systemd
    ip4_gw:
        192.168.182.2
    ip4_interfaces:
        ----------
        ens160:
            - 192.168.182.142
        lo:
            - 127.0.0.1
    ip6_gw:
        False
    ip6_interfaces:
        ----------
        ens160:
            - fe80::20c:29ff:fef4:b4a9
        lo:
            - ::1
    ip_gw:
        True
    ip_interfaces:
        ----------
        ens160:
            - 192.168.182.142
            - fe80::20c:29ff:fef4:b4a9
        lo:
            - 127.0.0.1
            - ::1
    ipv4:
        - 127.0.0.1
        - 192.168.182.142
    ipv6:
        - ::1
        - fe80::20c:29ff:fef4:b4a9
    kernel:
        Linux
    kernelparams:
        |_
          - BOOT_IMAGE
          - (hd0,msdos1)/vmlinuz-4.18.0-193.el8.x86_64
        |_
          - root
          - /dev/mapper/rhel-root
        |_
          - ro
          - None
        |_
          - crashkernel
          - auto
        |_
          - resume
          - /dev/mapper/rhel-swap
        |_
          - rd.lvm.lv
          - rhel/root
        |_
          - rd.lvm.lv
          - rhel/swap
        |_
          - rhgb
          - None
        |_
          - quiet
          - None
    kernelrelease:
        4.18.0-193.el8.x86_64
    kernelversion:
        #1 SMP Fri Mar 27 14:35:58 UTC 2020
    locale_info:
        ----------
        defaultencoding:
            UTF-8
        defaultlanguage:
            zh_CN
        detectedencoding:
            UTF-8
        timezone:
            CST
    localhost:
        minion01
    lsb_distrib_codename:
        Red Hat Enterprise Linux 8.2 (Ootpa)
    lsb_distrib_id:
        Red Hat Enterprise Linux
    lsb_distrib_release:
        8.2
    lvm:
        ----------
        rhel:
            - home
            - root
            - swap
    machine_id:
        b8032364c46f4fadaefd58290347f0eb
    manufacturer:
        VMware, Inc.
    master:
        192.168.182.141
    mdadm:
    mem_total:
        1800
    nodename:
        minion01
    num_cpus:
        2
    num_gpus:
        1
    os:
        RedHat
    os_family:
        RedHat
    osarch:
        x86_64
    oscodename:
        Ootpa
    osfinger:
        Red Hat Enterprise Linux-8
    osfullname:
        Red Hat Enterprise Linux
    osmajorrelease:
        8
    osrelease:
        8.2
    osrelease_info:
        - 8
        - 2
    path:
        /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
    pid:
        49239
    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:
        3004
    saltversioninfo:
        - 3004
    selinux:
        ----------
        enabled:
            False
        enforced:
            Disabled
    serialnumber:
        VMware-56 4d af 2f 62 90 b5 d0-b0 1b e2 f8 e8 f4 b4 a9
    server_id:
        1293882994
    shell:
        /bin/sh
    ssds:
        - nvme0n1
    swap_total:
        2091
    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
    transactional:
        False
    uid:
        0
    username:
        root
    uuid:
        2faf4d56-9062-d0b5-b01b-e2f8e8f4b4a9
    virtual:
        VMware
    zfs_feature_flags:
        False
    zfs_support:
        False
    zmqversion:
        4.3.4

只查詢所有的grains的key

[root@master base]# salt 'minion01' grains.ls
minion01:
    - biosreleasedate
    - biosversion
    - cpu_flags
    - cpu_model
    - cpuarch
    - cwd
    - disks
    - dns
    - domain
    - efi
    - efi-secure-boot
    - 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
    - transactional
    - uid
    - username
    - uuid
    - virtual
    - zfs_feature_flags
    - zfs_support
    - zmqversion

查詢某個key的值埂伦,比如想獲取ip地址

[root@master base]# salt 'minion01' grains.get fqdn_ip4
minion01:
    - 192.168.182.142

[root@master base]# salt 'minion02' grains.get fqdn_ip4
minion02:
    - 192.168.182.131

獲取某個主機mac地址
[root@master base]# salt 'minion01' grains.get hwaddr_interfaces
minion01:
    ----------
    ens160:
        00:0c:29:f4:b4:a9
    lo:
        00:00:00:00:00:00

[root@master base]# salt '*' grains.get ip4_interfaces
minion01:
    ----------
    ens160:
        - 192.168.182.142
    lo:
        - 127.0.0.1
minion02:
    ----------
    ens33:
        - 192.168.182.131
    lo:
        - 127.0.0.1

[root@master base]# salt 'minion01' grains.get hwaddr_interfaces:ens160
minion01:
    00:0c:29:f4:b4:a9

目標匹配實例:
用Grains來匹配minion:

在所有centos系統(tǒng)中執(zhí)行命令
[root@master base]# salt -G 'os:CentOS' test.ping
minion02:
    True

在top file里面使用Grains:

[root@master base]# pwd
/srv/salt/base

[root@master base]# vim top.sls
base:
  'os:CentOS':  //表示所有的centos系統(tǒng)都會執(zhí)行要達到的狀態(tài)
    - match: grain
    - web.nginx.nginx

[root@master base]# salt -G 'os:CentOS' state.highstate
[root@minion02 ~]# systemctl stop nginx.service
[root@minion02 ~]# systemctl status nginx.service 
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2021-11-02 19:14:54 CST; 9s ago

自定義Grains的兩種方法:

  • minion配置文件煞额,在配置文件中搜索grains
  • 在/etc/salt下生成一個grains文件,在此文件中定義(推薦方式)
記得重啟服務沾谜,因為minion只會讀取重啟之前的的內(nèi)容膊毁,因為我們在minion開啟狀態(tài)下寫的所以需要重啟minion服務使其讀取生效
[root@minion01 ~]# vim /etc/salt/grains
system: linux
version: redhat

[root@minion01 ~]# systemctl restart salt-minion.service

[root@master base]# salt 'minion01' grains.items
swap_total:
        2091
    system:
        linux

version:
        redhat
    virtual:

若不想重啟也可以使用下面的方法
[root@minion01 ~]# vim /etc/salt/grains
age: 18

[root@master base]# salt 'minion01' saltutil.sync_grains
minion01:
[root@master base]# salt 'minion01' grains.get age
minion01:
    18

SaltStack組件之Pillar

Pillar也是SaltStack組件中非常重要的組件之一,是數(shù)據(jù)管理中心类早,經(jīng)常配置states在大規(guī)模的配置管理工作中使用它媚媒。Pillar在SaltStack中主要的作用就是存儲和定義配置管理中需要的一些數(shù)據(jù),比如軟件版本號涩僻、用戶名密碼等信息缭召,它的定義存儲格式與Grains類似栈顷,都是YAML格式。

在Master配置文件中有一段Pillar settings選項專門定義Pillar相關的一些參數(shù):

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

默認Base環(huán)境下Pillar的工作目錄在/srv/pillar目錄下嵌巷。若你想定義多個環(huán)境不同的Pillar工作目錄萄凤,只需要修改此處配置文件即可。
Pillar的特點:

  • 可以給指定的minion定義它需要的數(shù)據(jù)
  • 只有指定的人才能看到定義的數(shù)據(jù)
  • 在master配置文件里設置
//查看pillar的信息
[root@master base]# salt '*' pillar.items
minion02:
    ----------
minion01:
    ----------

默認pillar是沒有任何信息的搪哪,如果想查看信息靡努,需要在 master 配置文件上把 pillar_opts的注釋取消,并將其值設為 True晓折。

[root@master ~]# vim /etc/salt/master
pillar_roots:
  base:
    - /srv/pillar/bash

pillar_opts: True  //取消注釋惑朦,并將Fales改為True,這里是為了展示效果才開啟漓概,建議保持關閉狀態(tài)漾月。

[root@master ~]# systemctl restart salt-master.service

[root@master ~]# salt 'minion01' pillar.items
minion01:
    ----------
    master:
        ----------
        __cli:
            salt-master
        __role:
            master
        allow_minion_key_revoke:
            True

pillar自定義數(shù)據(jù):
在master的配置文件里找pillar_roots可以看到其存放pillar的位置

[root@master ~]# vim /etc/salt/master
pillar_roots:
  base:
    - /srv/pillar/base
[root@master ~]# systemctl restart salt-master.service

[root@master ~]# mkdir -p /srv/pillar/base
[root@master base]# cat apache.sls 
{% if grains['os'] == 'RedHat' %}
package: httpd
{% elif grains['os'] == 'CentOS' %}
package: test
{% endif %}

指定top file入口文件

[root@master base]# pwd
/srv/pillar/base
[root@master base]# vim top.sls
base:   //指定環(huán)境
  'minion*':   //指定目標
    - apache   //引用apache.sls

[root@master base]# salt '*' pillar.items
minion02:
    ----------
    apache:
        test
minion01:
    ----------
    apache:
        httpd

在salt下修改apache的狀態(tài)文件,引用pillar的數(shù)據(jù)

[root@master base]# cat /srv/salt/base/web/apache/apache.sls
apache-install:
  pkg.installed:
    - name: {{ pillar['apache'] }}

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

執(zhí)行高級狀態(tài)文件

[root@master base]# salt '*' state.highstate 
minion02:
----------
          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 minion02
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
Total run time:   0.000 ms
minion01:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 22:36:39.585376
    Duration: 721.042 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 22:36:40.310194
    Duration: 115.002 ms
     Changes:   

Summary for minion01
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
Total run time: 836.044 ms
ERROR: Minions returned with non-zero exit code
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末胃珍,一起剝皮案震驚了整個濱河市梁肿,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌觅彰,老刑警劉巖吩蔑,帶你破解...
    沈念sama閱讀 206,968評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異填抬,居然都是意外死亡烛芬,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評論 2 382
  • 文/潘曉璐 我一進店門飒责,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蛀骇,“玉大人,你說我怎么就攤上這事读拆∩勉荆” “怎么了?”我有些...
    開封第一講書人閱讀 153,220評論 0 344
  • 文/不壞的土叔 我叫張陵檐晕,是天一觀的道長暑诸。 經(jīng)常有香客問我,道長辟灰,這世上最難降的妖魔是什么个榕? 我笑而不...
    開封第一講書人閱讀 55,416評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮芥喇,結(jié)果婚禮上西采,老公的妹妹穿的比我還像新娘。我一直安慰自己继控,他們只是感情好械馆,可當我...
    茶點故事閱讀 64,425評論 5 374
  • 文/花漫 我一把揭開白布胖眷。 她就那樣靜靜地躺著,像睡著了一般霹崎。 火紅的嫁衣襯著肌膚如雪珊搀。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,144評論 1 285
  • 那天尾菇,我揣著相機與錄音境析,去河邊找鬼。 笑死派诬,一個胖子當著我的面吹牛劳淆,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播默赂,決...
    沈念sama閱讀 38,432評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼憔儿,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了放可?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,088評論 0 261
  • 序言:老撾萬榮一對情侶失蹤朝刊,失蹤者是張志新(化名)和其女友劉穎耀里,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體拾氓,經(jīng)...
    沈念sama閱讀 43,586評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡冯挎,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,028評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了咙鞍。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片房官。...
    茶點故事閱讀 38,137評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖续滋,靈堂內(nèi)的尸體忽然破棺而出翰守,到底是詐尸還是另有隱情,我是刑警寧澤疲酌,帶...
    沈念sama閱讀 33,783評論 4 324
  • 正文 年R本政府宣布蜡峰,位于F島的核電站,受9級特大地震影響朗恳,放射性物質(zhì)發(fā)生泄漏湿颅。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,343評論 3 307
  • 文/蒙蒙 一粥诫、第九天 我趴在偏房一處隱蔽的房頂上張望油航。 院中可真熱鬧,春花似錦怀浆、人聲如沸谊囚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,333評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽秒啦。三九已至熬粗,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間余境,已是汗流浹背驻呐。 一陣腳步聲響...
    開封第一講書人閱讀 31,559評論 1 262
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留芳来,地道東北人含末。 一個月前我還...
    沈念sama閱讀 45,595評論 2 355
  • 正文 我出身青樓,卻偏偏與公主長得像即舌,于是被迫代替她去往敵國和親佣盒。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 42,901評論 2 345

推薦閱讀更多精彩內(nèi)容