Use of SaltStack advanced status

Keywords: saltstack

YAML language

YAML is an intuitive data serialization format that can be recognized by computer. It is a programming language with high readability, easy to be read by human beings, easy to interact with scripting language and used to express data sequences.

It is similar to the data description language of XML, a subset of the standard general markup language, and its syntax is much simpler than XML.

The format of YAML language is as follows:

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

Basic rules of YAML:

  • Indent is used to represent the hierarchical relationship. There are 2 spaces in each layer. The TAB key is prohibited
  • When the colon is not at the end, there must be a space after the colon
  • Use - to represent the list, and there must be a space after -
  • Use # to indicate comments

The YAML configuration file should be placed in the location of SaltStack. You can find the file in the Master configuration file of SaltStack_ You can see it from the roots.

[root@master ~]# vim /etc/salt/master
...Omitted here N that 's ok
file_roots:
  base:
    - /srv/salt/base
  test:
    - /srv/salt/test
  dev:
    - /srv/salt/dev
  prod:
    - /srv/salt/prod
...Omitted here N that 's ok

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

4 directories, 0 files
[root@master ~]# systemctl restart salt-master

Note:

  • Base is the default location, if file_ If there is only one root, base is required and must be called base, and cannot be renamed

Configuring an httpd instance with SaltStack

Deploy the sls configuration file on the Master and execute

[root@master ~]# mkdir -p /srv/salt/base
[root@master ~]# cd /srv/salt/base/
[root@master base]# mkdir -p web/apache
[root@master base]# cd web/apache/
[root@master apache]# touch install.sls 			// Generate a status description file
[root@master apache]# vim install.sls
httpd-install:
  pkg.installed:
    - name: httpd

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

// The top grid in YAML configuration file is called ID, which must be globally unique and cannot be repeated
// SaltStack reads YAML configuration files from top to bottom, so write the first execution in front


Tips for executing status files:
First use test.ping Test whether the host that needs to execute the status file can communicate normally, and then execute the status file
[root@master ~]# salt 'minion' state.sls web.apache.install saltenv=base
minion:
----------
          ID: httpd-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 02:49:12.849577
    Duration: 1055.507 ms
     Changes:   
----------
          ID: httpd-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 02:49:13.909687
    Duration: 48.368 ms
     Changes:   

Summary for minion
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
Total run time:   1.104 s

Check on minion

[root@minion ~]# ss -anlt
State       Recv-Q      Send-Q             Local Address:Port             Peer Address:Port      
LISTEN      0           128                      0.0.0.0:22                    0.0.0.0:*         
LISTEN      0           128                         [::]:22                       [::]:*         
LISTEN      0           128                            *:80                          *:*         
[root@minion ~]# 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 Wed 2021-11-03 02:07:47 CST; 43min ago
     Docs: man:httpd.service(8)
 Main PID: 957 (httpd)
   Status: "Running, listening on: port 80"
    Tasks: 213 (limit: 11300)
   Memory: 49.9M
   CGroup: /system.slice/httpd.service
           ├─ 957 /usr/sbin/httpd -DFOREGROUND
           ├─1052 /usr/sbin/httpd -DFOREGROUND
           ├─1053 /usr/sbin/httpd -DFOREGROUND
           ├─1054 /usr/sbin/httpd -DFOREGROUND
           └─1055 /usr/sbin/httpd -DFOREGROUND

11 March 2:07:46 minion systemd[1]: Starting The Apache HTTP Server...
11 March 2:07:47 minion httpd[957]: AH00558: httpd: Could not reliably determine the server's fu>
11 March 2:07:47 minion systemd[1]: Started The Apache HTTP Server.
11 March 2:07:47 minion httpd[957]: Server configured, listening on: port 80

top file

top file introduction

  • Is it automatic enough to execute sls files directly through commands? The answer is no, because we have to tell a host to perform a task. Automation should be that when we let it work, it knows which host to do. However, executing sls files directly through commands can not achieve this purpose. In order to solve this problem, top file came into being.
  • Top file is an entry. The file name of top file can be found by searching top.sls in the Master configuration file, and this file must be in the base environment. By default, this file must be called top.sls.
  • The function of top file is to tell the corresponding host what to do, such as enabling the web server to start web services, enabling the database server to install mysql, and so on.

top file instance:

[root@master ~]# cd /srv/salt/base/
[root@master base]# vim top.sls
[root@master base]# cat top.sls
base:           
  'minion':  
    - web.apache.install     

//Stop the httpd service of p2
[root@minion ~]# systemctl stop httpd.service 

//The following command will report an error because the minion on the master side does not perform any operation, which will not affect the result
[root@master ~]# salt '*' state.highstate
master:
----------
          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 master
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
Total run time:   0.000 ms
minion:
----------
          ID: httpd-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 03:31:38.909212
    Duration: 604.952 ms
     Changes:   
----------
          ID: httpd-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is running
     Started: 03:31:39.516292
    Duration: 234.593 ms
     Changes:   
              ----------
              httpd:
                  True

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

//View the httpd status of p2 side
[root@minion ~]# 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 Wed 2021-11-03 03:31:39 CST; 2min 6s ago
     Docs: man:httpd.service(8)
 Main PID: 96308 (httpd)
   Status: "Running, listening on: port 80"
    Tasks: 213 (limit: 11300)
   Memory: 27.2M
   CGroup: /system.slice/httpd.service
           ├─96308 /usr/sbin/httpd -DFOREGROUND
           ├─96656 /usr/sbin/httpd -DFOREGROUND
           ├─96657 /usr/sbin/httpd -DFOREGROUND
           ├─96658 /usr/sbin/httpd -DFOREGROUND
           └─96659 /usr/sbin/httpd -DFOREGROUND

11 March 3:31:39 minion systemd[1]: Starting The Apache HTTP Server...
11 March 3:31:39 minion httpd[96308]: AH00558: httpd: Could not reliably determine the server's >
11 March 3:31:39 minion systemd[1]: Started The Apache HTTP Server.
11 March 3:31:49 minion httpd[96308]: Server configured, listening on: port 80


be careful:

  • The above allows everyone to execute the advanced state, but it is generally not used in actual work. In work, it is generally to notify one or some target hosts to execute the advanced state. The specific execution is determined by the top file.
  • If you add the parameter test=True when executing the advanced state, it will tell us what it will do, but it will not really perform this operation.
//Stop the httpd service on minion
[root@minion ~]# systemctl stop httpd.service 
[root@minion ~]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Wed 2021-11-03 03:35:25 CST; 5s ago
     Docs: man:httpd.service(8)
  Process: 96308 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=0/SUCCESS)
 Main PID: 96308 (code=exited, status=0/SUCCESS)
   Status: "Running, listening on: port 80"

11 March 3:31:39 minion systemd[1]: Starting The Apache HTTP Server...
11 March 3:31:39 minion httpd[96308]: AH00558: httpd: Could not reliably determine the server's >
11 March 3:31:39 minion systemd[1]: Started The Apache HTTP Server.
11 March 3:31:49 minion httpd[96308]: Server configured, listening on: port 80
11 March 3:35:24 minion systemd[1]: Stopping The Apache HTTP Server...
11 March 3:35:25 minion systemd[1]: Stopped The Apache HTTP Server.

//Perform advanced status tests on the master
[root@master ~]# salt 'minion' state.highstate test=True
minion:
----------
          ID: httpd-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 03:36:30.711368
    Duration: 695.136 ms
     Changes:   
----------
          ID: httpd-service
    Function: service.running
        Name: httpd
      Result: None
     Comment: Service httpd is set to start
     Started: 03:36:31.408522
    Duration: 53.115 ms
     Changes:   

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


//Check the httpd service on minion
[root@minion ~]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Wed 2021-11-03 03:35:25 CST; 1min 39s ago
     Docs: man:httpd.service(8)
  Process: 96308 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=0/SUCCESS)
 Main PID: 96308 (code=exited, status=0/SUCCESS)
   Status: "Running, listening on: port 80"

11 March 3:31:39 minion systemd[1]: Starting The Apache HTTP Server...
11 March 3:31:39 minion httpd[96308]: AH00558: httpd: Could not reliably determine the server's >
11 March 3:31:39 minion systemd[1]: Started The Apache HTTP Server.
11 March 3:31:49 minion httpd[96308]: Server configured, listening on: port 80
11 March 3:35:24 minion systemd[1]: Stopping The Apache HTTP Server...
11 March 3:35:25 minion systemd[1]: Stopped The Apache HTTP Server.
lines 1-14/14 (END)

Posted by esukf on Tue, 02 Nov 2021 14:03:17 -0700