SaltStack common module 2

SaltStack common modules

Article directory

state of SaltStack common modules

state.show_highstate

Display the advanced status in the current system

[root@node1 ~]# salt 'node1*' state.show_highstate
node1.example.com:
    ----------
    apache-install:
        ----------
        __env__:
            base
        __sls__:
            web.apache.apache
        pkg:
            |_
              ----------
              name:
                  httpd
            - installed
            |_
              ----------
              order:
                  10000
    apache-service:
        ----------
        __env__:
            base
        __sls__:
            web.apache.apache
        service:
            |_
              ----------
              name:
                  httpd
            |_
              ----------
              enable:
                  True
            - running
            |_
              ----------
              order:
                  10001
state.highstate

Execute advanced status

[root@node1 ~]# salt 'node1*' state.highstate saltenv=base
node1.example.com:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 17:19:05.033013
    Duration: 691.467 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd has been enabled, and is in the desired state
     Started: 17:19:05.725272
    Duration: 133.283 ms
     Changes:   
              ----------
              httpd:
                  True

Summary for node1.example.com
------------
Succeeded: 2 (changed=1)
Failed:    0
------------
Total states run:     2
Total run time: 824.750 ms

If you add the parameter test=True to the advanced state, it will tell us what it will do, but it will not really do it.

[root@node1 ~]# salt 'node1*' state.highstate saltenv=base test=Ture
node1.example.com:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 17:24:37.071740
    Duration: 934.737 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 17:24:38.007305
    Duration: 27.28 ms
     Changes:   

Summary for node1.example.com
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
Total run time: 962.017 ms
state.show_state_usage

Display the execution of advanced status in the current system

[root@node1 ~]# salt 'node1*' state.show_state_usage
node1.example.com:
    ----------
    base:
        ----------
        count_all:
            3
        count_unused:
            2
        count_used:
            1
        unused:
            - share.samba.samba
            - top
        used:
            - web.apache.apache
state.show_top

Returns the top-level data that minion will use for highstate

[root@node1 ~]# salt 'node1*' state.show_top
node1.example.com:
    ----------
    base:
        - web.apache.apache
    prod:
        - web.apache.apache
state.top

Execute the specified top file instead of the default

[root@node1 base]# mv top.sls tom.sls
[root@node1 base]# ls
share  tom.sls  web
[root@node1 base]# salt 'node1*' state.top tom.sls saltenv=base
node1.example.com:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 17:29:49.116860
    Duration: 650.902 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 17:29:49.768549
    Duration: 28.61 ms
     Changes:   

Summary for node1.example.com
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
Total run time: 679.512 ms

Salt CP of SaltStack common modules

Salt CP can easily transfer files on the master to minion in batches (scp needs mutual trust, which is not needed)

Copy a single file to the/usr/src Directory
[root@node1 base]# salt 'node2*' cmd.run 'ls /usr/src/'
node2.example.com:
    debug
    kernels
[root@node1 base]# salt-cp 'node2*' /etc/passwd /usr/src/
node2.example.com:
    ----------
    /usr/src/passwd:
        True
[root@node1 base]# salt 'node2*' cmd.run 'ls /usr/src'
node2.example.com:
    debug
    kernels
    passwd
//Copy multiple files to the / usr/src directory of the target host  
[root@node1 base]# salt-cp 'node2*' /etc/shadow /etc/group /usr/src
node2.example.com:
    ----------
    /usr/src/group:
        True
    /usr/src/shadow:
        True
[root@node1 base]# salt 'node2*' cmd.run 'ls /usr/src'
node2.example.com:
    debug
    group
    kernels
    passwd
    shadow
//Copy the directory to the / usr/src directory of the target host
[root@node1 ~]# mkdir tom
[root@node1 ~]# salt-cp 'node2*' /root/tom /usr/src/ --chunked
node2.example.com:
    ----------
    /usr/src/tom:
        True
[root@node1 ~]# salt 'node2*' cmd.run 'file /usr/src/tom'
node2.example.com:
    /usr/src/tom: directory

file of SaltStack common modules

file.access

Check if the specified path exists

[root@node1 ~]# salt '*' file.access /root/anaconda-ks.cfg f
node2.example.com:
    True
node1.example.com:
    True

Check the permission information of the specified file

Check if you have read permission
[root@node1 ~]#  salt '*' file.access /root/anaconda-ks.cfg r
node1.example.com:
    True
node2.example.com:
    True
//Check if you have write permission
[root@node1 ~]#  salt '*' file.access /root/anaconda-ks.cfg w
node1.example.com:
    True
node2.example.com:
    True
//Check whether there is execution permission
[root@node1 ~]#  salt '*' file.access /root/anaconda-ks.cfg x
node1.example.com:
    False
node2.example.com:
    False
file.append

Add content to a file. If the file does not exist, an exception will be reported

[root@node1 ~]# salt '*' cmd.run 'ls -l /opt/abc'
node1.example.com:
    -rw-r--r-- 1 root root 0 Feb 23 15:31 /opt/abc
node2.example.com:
    -rw-r--r-- 1 root root 0 Feb 23 15:31 /opt/abc
[root@node1 ~]# salt '*' file.append /opt/abc "tom" "jerry"

node1.example.com:
    Wrote 2 lines to "/opt/abc"
node2.example.com:
    Wrote 2 lines to "/opt/abc"
[root@node1 ~]# salt "*" cmd.run 'cat /opt/abc'
node1.example.com:
    tom
    jerry
node2.example.com:
    tom
    jerry
file.basename

Gets the base name of the specified path

[root@node1 ~]# salt 'node1*' file.basename '/a/b/c/d/tom'
node1.example.com:
    tom
file.dirname

Gets the directory name of the specified path

[root@node1 ~]# salt 'node1*' file.dirname '/a/b/c/d/tom'
node1.example.com:
    /a/b/c/d
file.check_hash

Check whether the specified file matches the hash string, return True if it matches, otherwise return False

[root@node1 ~]# salt 'node1*' cmd.run 'md5sum /etc/passwd'
node1.example.com:
    f06a7ddaf0b71e98e73a96a2b54785b3  /etc/passwd
[root@node1 ~]# salt 'node1*' file.check_hash /etc/passwd f06a7ddaf0b71e98e73a96a2b54785b3
node1.example.com:
    True
file.chattr

chatter usage:
Usage: chattr [-RVf] [-+=aAcCdDeijsStTu] [-v version] files...

attribute Significance to documents Meaning to the catalogue
a Only append data after this file is allowed. No process is allowed to overwrite or truncate this file Only files can be created and modified in this directory, and no files can be deleted
i You are not allowed to make any changes to this file. You cannot delete, change or move it Any process can only modify files under the directory, and cannot create or delete files
[root@node1 ~]# chattr +a /opt/abc
[root@node1 ~]# lsattr /opt/abc
-----a---------- /opt/abc
[root@node1 ~]# echo "qwe" >> /opt/abc
[root@node1 ~]# cat /opt/abc
tom
jerry
qwe
[root@node1 ~]# chattr +i /opt/abc
[root@node1 ~]# lsattr /opt/abc
----ia---------- /opt/abc
[root@node1 ~]# mv /opt/abc .
mv: Unable to"/opt/abc" Move to"./abc": Operation not allowed

Remove attributes from specified files

[root@node1 ~]# salt 'node1*' file.chattr /opt/abc operator=remove attributes=ai
node1.example.com:
    True
[root@node1 ~]# lsattr /opt/abc
---------------- /opt/abc

Add attribute

[root@node1 ~]# salt 'node1*' file.chattr /opt/abc operator=add attributes=ai
node1.example.com:
    True

View current properties

[root@node1 ~]# lsattr /opt/abc
----ia---------- /opt/abc
file.copy

Copying files or directories on a remote host
(ps: salt CP is copied from the master to minion. file.copy is to copy files from the minio local machine to another place on the local machine.)

Copy file

[root@node1 ~]# salt 'node1*' cmd.run 'ls -l /root'
node1.example.com:
    total 4
    -rw-r--r-- 1 root root    0 Feb 23 16:13 ab
    -rw------- 1 root root 1451 Feb 21 10:37 anaconda-ks.cfg
    drwxr-xr-x 2 root root    6 Feb 23 15:06 tom
[root@node1 ~]# salt '*' file.copy /root/ab /root/cc
node1.example.com:
    True
[root@node1 ~]# salt 'node1*' cmd.run 'ls -l /root'
node1.example.com:
    total 4
    -rw-r--r-- 1 root root    0 Feb 23 16:13 ab
    -rw------- 1 root root 1451 Feb 21 10:37 anaconda-ks.cfg
    -rw-r--r-- 1 root root    0 Feb 23 16:14 cc
    drwxr-xr-x 2 root root    6 Feb 23 15:06 tom

Overwrite and copy directory will overwrite the file or directory with the same name

[root@node1 ~]# salt 'node1*' file.copy /tmp /root/abc recurse=True
node1.example.com:
    True
[root@node1 ~]# salt 'node1*' cmd.run 'ls -l /root'
node1.example.com:
    total 12
    -rw-r--r--  1 root root    0 Feb 23 16:13 ab
    drwxr-xr-x 50 root root 4096 Feb 23 16:20 abc
    -rw-------  1 root root 1451 Feb 21 10:37 anaconda-ks.cfg
    -rw-r--r--  1 root root    0 Feb 23 16:14 cc

Delete the file or directory with the same name in the target directory and copy the new content to it

[root@node1 ~]# salt 'node1*' cmd.run 'ls -l /root/abc'
node1.example.com:
    total 4
    -rw-r--r-- 1 root root  14 Feb 23 16:20 abc
    drwxr-xr-x 9 root root 254 Feb 23 16:22 myrepo
    drwxr-xr-x 2 root root   6 Feb 23 16:20 systemd-private-0d611d1b61504acd914248c8f8921ad4-chronyd.service-pYx5aQ
    drwxr-xr-x 2 root root   6 Feb 23 16:20 systemd-private-0d611d1b61504acd914248c8f8921ad4-httpd.service-Z16BP8
    drwxr-xr-x 2 root root   6 Feb 23 16:20 systemd-private-0d611d1b61504acd914248c8f8921ad4-vgauthd.service-V5EACp
[root@node1 ~]# salt 'node1*' cmd.run 'ls -l /opt/myrepo'
node1.example.com:
    total 396
    dr-xr-xr-x. 3 root root     18 Sep 28 15:53 EFI
    -r--r--r--. 1 root root   8266 Sep 28 15:53 EULA
    -r--r--r--. 1 root root  18092 Sep 28 15:53 GPL
    dr-xr-xr-x. 2 root root     43 Sep 28 15:53 LiveOS
    dr-xr-xr-x. 2 root root 270336 Sep 28 15:55 Packages
    -r--r--r--. 1 root root   3375 Sep 28 15:55 RPM-GPG-KEY-redhat-beta
[root@node1 ~]# salt 'node1*' file.copy /opt/myrepo/ /root/abc/ recurse=True remove_existing=True
node1.example.com:
    True
[root@node1 ~]# salt 'node1*' cmd.run 'ls -l /root/abc'
node1.example.com:
    total 396
    dr-xr-xr-x 3 root root     18 Sep 28 15:53 EFI
    -r--r--r-- 1 root root   8266 Sep 28 15:53 EULA
    -r--r--r-- 1 root root  18092 Sep 28 15:53 GPL
    dr-xr-xr-x 2 root root     43 Sep 28 15:53 LiveOS
    dr-xr-xr-x 2 root root 270336 Sep 28 15:55 Packages
    -r--r--r-- 1 root root   3375 Sep 28 15:55 RPM-GPG-KEY-redhat-beta
    -r--r--r-- 1 root root   3211 Sep 28 15:55 RPM-GPG-KEY-redhat-release
file.file_exists

Determine whether the specified file exists (directory does not count)

[root@node1 ~]# salt 'node1*' file.file_exists /root/anaconda-ks.cfg 
node1.example.com:
    True
file.find
[root@node1 ~]# salt 'node1*' file.find / name=anaconda-ks.cfg 
node1.example.com:
    - /root/anaconda-ks.cfg
file.get_gid

Get the gid of the specified file

[root@node1 ~]# salt 'node1*' file.get_gid /root/ab
node1.example.com:
    0
file.get_hash

Get the hash value of the specified file, which is obtained by sha256 algorithm

[root@node1 ~]# salt 'node1*' cmd.run 'sha256sum /root/ab'
node1.example.com:
    e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855  /root/ab
[root@node1 ~]# salt 'node1*' file.get_hash /root/ab
node1.example.com:
    e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
file.get_mode

Get the permission of the specified file, and display it digitally

[root@node1 ~]# salt 'node1*' cmd.run 'ls -l /root/ab'
node1.example.com:
    -rw-r--r-- 1 root root 0 Feb 23 16:13 /root/ab
[root@node1 ~]# salt 'node1*' file.get_mode /root/ab
node1.example.com:
    0644
file.get_selinux_context

Get SELINUX context information of the specified file

[root@node1 ~]# salt 'node1*' cmd.run 'ls -Z /etc/passwd'
node1.example.com:
    -rw-r--r--. root root system_u:object_r:passwd_file_t:s0 /etc/passwd
[root@node1 ~]# salt 'node1*' file.get_selinux_context /etc/passwd
node1.example.com:
    system_u:object_r:passwd_file_t:s0
26 original articles published, praised 0, visitors 428
Private letter follow

Tags: Apache Anaconda saltstack RPM

Posted on Sun, 23 Feb 2020 04:15:22 -0500 by Eugene