Common modules of saltstack

Keywords: Operation & Maintenance

Common modules of saltstack

1. Introduction to saltstack module

Module is one of the components that SaltStack contacts most in daily use. It is used to manage object operations. It is also the entrance for SaltStack to manage by pushing. For example, our daily simple tasks such as executing commands, viewing package installation and service operation are realized through SaltStack Module.

After installing the Master and Minion packages, many modules will be installed on the system. You can view the list of all supported modules through the following command:

//View a list of all modules
[root@master ~]# salt 'node1' sys.list_modules 
node1:
    - acl
    - aliases
    - alternatives
    - apache
    - archive
    - artifactory
    - baredoc
    - beacons
    - bigip
    - btrfs
    - buildout
    - chroot
    - cloud
    - cmd
    - composer
    - config
    - consul
...........Omitted here
    
    
//View all function functions of the specified module
[root@master ~]# salt 'node1' sys.list_functions cmd
node1:
    - cmd.exec_code
    - cmd.exec_code_all
    - cmd.has_exec
    - cmd.powershell
    - cmd.powershell_all
    - cmd.retcode
    - cmd.run
    - cmd.run_all
    - cmd.run_bg
    - cmd.run_chroot
    - cmd.run_stderr
    - cmd.run_stdout
    - cmd.script
    - cmd.script_retcode
    - cmd.shell
    - cmd.shell_info
    - cmd.shells
    - cmd.tty
    - cmd.which
    - cmd.which_bin
[root@master ~]#


//View the usage document of the specified module
[root@master ~]# salt 'node1' sys.doc cmd
cmd.exec_code:

    Pass in two strings, the first naming the executable language, aka -
    python2, python3, ruby, perl, lua, etc. the second string containing
    the code you wish to execute. The stdout will be returned.

    All parameters from :mod:`cmd.run_all <salt.modules.cmdmod.run_all>` except python_shell can be used.

    CLI Example:

        salt '*' cmd.exec_code ruby 'puts "cheese"'
        salt '*' cmd.exec_code ruby 'puts "cheese"' args='["arg1", "arg2"]' env='{"FOO": "bar"}'
................Omitted here


//SaltStack also supports the execution of multiple modules at a time by default. Module s are separated by commas. By default, parameters are separated by commas. It also supports specifying the parameter separator -- args separator = @
[root@master ~]# salt 'node1' test.echo,cmd.run,service.status hello,hostname,salt-minion
node1:
    ----------
    cmd.run:
        node1
    service.status:
        True
    test.echo:
        hello
[root@master ~]# 

2. Common modules of saltstack

2.1 network of common modules of saltstack

2.1.1 network.active_tcp

Returns all active tcp connections

[root@master ~]# salt 'node1' network.active_tcp 
node1:
    ----------
    0:
        ----------
        local_addr:
            192.168.10.202
        local_port:
            39442
        remote_addr:
            192.168.10.201
        remote_port:
            4505
    1:
        ----------
        local_addr:
            192.168.10.202
        local_port:
            22
        remote_addr:
            192.168.10.1
        remote_port:
            49708
[root@master ~]#

2.1.2 network.calc_net

Calculate the network segment through IP and subnet mask

[root@master ~]# salt 'node1' network.calc_net 192.168.10.201 255.255.255.0
node1:
    192.168.10.0/24
[root@master ~]# salt 'node1' network.calc_net 192.168.10.201 255.255.255.240
node1:
    192.168.10.192/28
[root@master ~]#

2.1.3 network.connect

Test whether the network from minion to a server is connected

You must specify a port

[root@master ~]# salt 'node1' network.connect 192.168.10.201 22
node1:
    ----------
    comment:
        Successfully connected to 192.168.10.201 (192.168.10.201) on tcp port 22
    result:
        True
[root@master ~]# salt 'node1' network.connect baidu.com 443
node1:
    ----------
    comment:
        Successfully connected to baidu.com (220.181.38.251) on tcp port 443
    result:
        True
[root@master ~]# 

2.1.4 network.default_route

View default routes

[root@master ~]# salt 'node1' network.default_route 
node1:
    |_
      ----------
      addr_family:
          inet
      destination:
          0.0.0.0
      flags:
          UG
      gateway:
          192.168.10.2
      interface:
          ens33
      netmask:
          0.0.0.0
    |_
      ----------
      addr_family:
          inet6
      destination:
          ::/0
      flags:
          !n
      gateway:
          ::
      interface:
          lo
      netmask:
    |_
      ----------
      addr_family:
          inet6
      destination:
          ::/0
      flags:
          !n
      gateway:
          ::
      interface:
          lo
      netmask:
[root@master ~]#

2.1.5 network.get_fqdn

View FQDN (fully qualified domain name) of the host

[root@master ~]# salt 'node1' network.get_fqdn 
node1:
    node1
[root@master ~]#

2.1.6 network.get_hostname

Get host name

[root@master ~]# salt 'node1' network.get_hostname 
node1:
    node1
[root@master ~]#

2.1.7 network.get_route

Query the routing information of a target network

[root@master ~]# salt 'node1' network.get_route 192.168.10.201
node1:
    ----------
    destination:
        192.168.10.201
    gateway:
        None
    interface:
        ens33
    source:
        192.168.10.202
[root@master ~]#

2.1.8 network.hw_addr

Returns the MAC address of the specified network card

[root@master ~]# salt 'node1' network.hw_addr ens33
node1:
    00:0c:29:21:00:bb
[root@master ~]#

2.1.9 network.ifacestartswith

Retrieves the interface name from a specific CIDR

[root@master ~]# salt 'node1' network.ifacestartswith 192
node1:
    - ens33
[root@master ~]# salt 'node1' network.ifacestartswith 127
node1:
    - lo
[root@master ~]#

2.1.11 network.interface

Returns the information of the specified network card

[root@master ~]# salt 'node1' network.interface ens33
node1:
    |_
      ----------
      address:
          192.168.10.202
      broadcast:
          192.168.10.255
      label:
          ens33
      netmask:
          255.255.255.0
[root@master ~]#

2.1.13 network.interfaces

Returns all network card information in the current system

[root@master ~]# salt 'node1' network.interfaces
node1:
    ----------
    ens33:
        ----------
        hwaddr:
            00:0c:29:21:00:bb
        inet:
            |_
              ----------
              address:
                  192.168.10.202
              broadcast:
                  192.168.10.255
              label:
                  ens33
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
              address:
                  fe80::20c:29ff:fe21:bb
              prefixlen:
                  64
              scope:
                  link
        up:
            True
    lo:
        ----------
        hwaddr:
            00:00:00:00:00:00
        inet:
            |_
              ----------
              address:
                  127.0.0.1
              broadcast:
                  None
              label:
                  lo
              netmask:
                  255.0.0.0
        inet6:
            |_
              ----------
              address:
                  ::1
              prefixlen:
                  128
              scope:
                  host
        up:
            True
[root@master ~]#

2.1.14 network.ip_addrs

Returns a list of IPv4 addresses
This function will ignore the address of 127.0.0.1

[root@master ~]# salt 'node1' network.ip_addrs
node1:
    - 192.168.10.202
[root@master ~]#

2.1.15 network.netstat

Returns all open ports and status

[root@master ~]# salt 'node1' network.netstat 
node1:
    |_
      ----------
      inode:
          27453
      local-address:
          0.0.0.0:22
      program:
          935/sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          0.0.0.0:*
      send-q:
          0
      state:
          LISTEN
      user:
          0
    |_
      ----------
      inode:
          30265
      local-address:
          192.168.10.202:39442
      program:
          1781/python3.6
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.10.201:4505
      send-q:
          0
      state:
          ESTABLISHED
      user:
          0
    |_
      ----------
      inode:
          58589
      local-address:
          192.168.10.202:22
      program:
          2579/sshd:
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.10.1:49708
      send-q:
          0
      state:
          ESTABLISHED
      user:
          0
    |_
      ----------
      inode:
          0
      local-address:
          192.168.10.202:60926
      program:
          -
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.10.201:4506
      send-q:
          0
      state:
          TIME_WAIT
      user:
          0
    |_
      ----------
      inode:
          29342
      local-address:
          :::3306
      program:
          1193/mysqld
      proto:
          tcp6
      recv-q:
          0
      remote-address:
          :::*
      send-q:
          0
      state:
          LISTEN
      user:
          992
    |_
      ----------
      inode:
          27596
      local-address:
          :::80
      program:
          930/httpd
      proto:
          tcp6
      recv-q:
          0
      remote-address:
          :::*
      send-q:
          0
      state:
          LISTEN
      user:
          0
    |_
      ----------
      inode:
          27464
      local-address:
          :::22
      program:
          935/sshd
      proto:
          tcp6
      recv-q:
          0
      remote-address:
          :::*
      send-q:
          0
      state:
          LISTEN
      user:
          0
    |_
      ----------
      inode:
          25806
      local-address:
          127.0.0.1:323
      program:
          907/chronyd
      proto:
          udp
      recv-q:
          0
      remote-address:
          0.0.0.0:*
      send-q:
          0
      user:
          0
    |_
      ----------
      inode:
          25807
      local-address:
          ::1:323
      program:
          907/chronyd
      proto:
          udp6
      recv-q:
          0
      remote-address:
          :::*
      send-q:
          0
      user:
          0
[root@master ~]#

2.1.16 network.ping

Use the ping command to test connectivity to a host

[root@master ~]# salt 'node1' network.ping 192.168.10.201
node1:
    PING 192.168.10.201 (192.168.10.201) 56(84) bytes of data.
    64 bytes from 192.168.10.201: icmp_seq=1 ttl=64 time=0.270 ms
    64 bytes from 192.168.10.201: icmp_seq=2 ttl=64 time=0.482 ms
    64 bytes from 192.168.10.201: icmp_seq=3 ttl=64 time=0.253 ms
    64 bytes from 192.168.10.201: icmp_seq=4 ttl=64 time=0.524 ms
    
    --- 192.168.10.201 ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3084ms
    rtt min/avg/max/mdev = 0.253/0.382/0.524/0.122 ms
[root@master ~]#

2.1.17 network.reverse_ip

Returns the reverse address of a specified IP address

[root@master ~]# salt 'node1' network.reverse_ip 192.168.10.100
node1:
    100.10.168.192.in-addr.arpa
[root@master ~]#

2.2 service s of common modules of saltstack

2.2.1 service.available

Determine whether the specified service is available

[root@master ~]# salt 'node1' service.available httpd
node1:
    True
[root@master ~]# salt 'node1' service.available nginx
node1:
    False
[root@master ~]#

2.2.2 service.get_all

Get all running services

[root@master ~]# salt 'node1' service.get_all 
node1:
    - NetworkManager
    - NetworkManager-dispatcher
    - NetworkManager-wait-online
    - arp-ethers
    - auditd
    - autovt@
    - basic.target
    - blk-availability
    - bluetooth.target
    - boot-complete.target
    - chrony-dnssrv@
    - chrony-dnssrv@.timer
    - chrony-wait
    - chronyd
    - console-getty
    - container-getty@
    - cpupower
    - crond
    - cryptsetup-pre.target
    - cryptsetup.target
    - ctrl-alt-del.target
    - dbus
    - dbus-org.freedesktop.hostname1
...............ellipsis

2.2.3 service.disabled

Check whether the specified service is started or not

[root@master ~]# salt 'node1' service.disabled httpd
node1:
    False
[root@master ~]#

2.2.4 service.enabled

Check whether the specified service starts automatically

[root@master ~]# salt 'node1' service.enabled httpd
node1:
    True
[root@master ~]#

2.2.5 service.disable

Set the specified service to start automatically

[root@master ~]# salt 'node1' service.disable httpd
node1:
    True
[root@master ~]# salt 'node1' service.enabled httpd
node1:
    False
[root@master ~]#

2.2.6 service.enable

Set the specified service to start automatically

[root@master ~]# salt 'node1' service.enable httpd
node1:
    True
[root@master ~]# salt 'node1' service.enabled httpd
node1:
    True
[root@master ~]#

2.2.7 service.reload

Reload the specified service

[root@master ~]# salt 'node1' service.reload httpd
node1:
    True
[root@master ~]#

2.2.8 service.stop

Stop the specified service

[root@master ~]# salt 'node1' service.stop httpd
node1:
    True
[root@master ~]#

2.2.9 service.start

Start the specified service

[root@master ~]# salt 'node1' service.start httpd
node1:
    True
[root@master ~]#

2.2.10 service.restart

Restart the specified service

[root@master ~]# salt 'node1' service.restart httpd
node1:
    True
[root@master ~]#

2.2.11 service.status

View the status of the specified service

[root@master ~]# salt 'node1' service.status httpd
node1:
    True
[root@master ~]# salt 'node1' service.stop httpd
node1:
    True
[root@master ~]# salt 'node1' service.status httpd
node1:
    False
[root@master ~]#

2.3 pkg of common modules of saltstack

2.3.1 pkg.install

Install software

[root@master ~]# salt 'node1' pkg.install wget
node1:
    ----------
    wget:
        ----------
        new:
            1.19.5-10.el8
        old:
[root@master ~]#

2.3.2 pkg.download

Only download packages but do not install
This function will download the specified software package, but you need to install Yum utils on the minion side. You can use cmd.run for remote installation

[root@master ~]# salt 'node1' pkg.download wget
node1:
    ----------
    wget:
        /var/cache/yum/packages/wget-1.19.5-10.el8.x86_64.rpm
[root@master ~]# salt 'node1' cmd.run 'ls /var/cache/yum/packages/'
node1:
    wget-1.19.5-10.el8.x86_64.rpm
[root@master ~]#

2.3.3 pkg.list_downloaded

Lists the packages that have been downloaded locally

[root@master ~]# salt 'node1' pkg.list_downloaded 
node1:
    ----------
[root@master ~]#

2.3.4 pkg.file_list

Lists the files for the specified package or all packages installed on the system

//Lists all files provided by the installed apache package
[root@master ~]# salt 'node1' pkg.file_list httpd
node1:
    ----------
    errors:
    files:
        - /etc/httpd/conf
        - /etc/httpd/conf.d/autoindex.conf
        - /etc/httpd/conf.d/userdir.conf
        - /etc/httpd/conf.d/welcome.conf
        - /etc/httpd/conf.modules.d
        - /etc/httpd/conf.modules.d/00-base.conf
        - /etc/httpd/conf.modules.d/00-dav.conf
        - /etc/httpd/conf.modules.d/00-lua.conf
        - /etc/httpd/conf.modules.d/00-mpm.conf
        - /etc/httpd/conf.modules.d/00-optional.conf
        - /etc/httpd/conf.modules.d/00-proxy.conf
        - /etc/httpd/conf.modules.d/00-systemd.conf
        - /etc/httpd/conf.modules.d/01-cgi.conf
        - /etc/httpd/conf.modules.d/README
        - /etc/httpd/conf/httpd.conf
.................ellipsis

2.3.5 pkg.group_info

View package group information

[root@master ~]# salt 'node1' pkg.group_info 'Development Tools'
node1:
    ----------
    conditional:
    default:
        - asciidoc
        - byacc
        - ctags
        - diffstat
        - elfutils-libelf-devel
        - git
        - intltool
        - jna
        - ltrace
        - patchutils
        - perl-Fedora-VSP
        - perl-Sys-Syslog
        - perl-generators
        - pesign
        - source-highlight
        - systemtap
        - valgrind
        - valgrind-devel
    description:
        A basic development environment.
    group:
        Development Tools
    id:
        None
    mandatory:
        - autoconf
        - automake
        - binutils
        - bison
        - flex
        - gcc
        - gcc-c++
        - gdb
        - glibc-devel
        - libtool
        - make
        - pkgconf
        - pkgconf-m4
        - pkgconf-pkg-config
        - redhat-rpm-config
        - rpm-build
        - rpm-sign
        - strace
    optional:
        - cmake
        - expect
        - rpmdevtools
        - rpmlint
    type:
        package group
[root@master ~]#

2.3.6 pkg.group_list

Lists all package groups in the system

[root@master ~]# salt 'node1' pkg.group_list 
node1:
    ----------
    available:
        - Conflicts BaseOS
        - Dial-up Networking Support
        - Hardware Monitoring Utilities
        - Hardware Support
        - Large Systems Performance
        - Legacy UNIX Compatibility
        - Python Web
        - Server product core
        - Windows File Server
        - Additional Development
        - Anaconda tools
        - Backup Client
        - Base
        - base-x
        - Conflicts AppStream
        - Container Management
        - Debugging Tools
        - Desktop Debugging and Performance Tools
        - .NET Core Development
        - File and Storage Server
        - Fonts
        - FTP Server
        - GNOME Applications
        - GNOME
        - Graphical Administration Tools
        - Graphics Creation Tools
        - Guest Agents
        - Guest Desktop Agents
        - Headless Management
        - Infiniband Support
        - Input Methods
        - Internet Applications
        - Internet Browser
        - Java Platform
        - Legacy X Window System Compatibility
        - Mail Server
        - Mainframe Access
        - Multimedia
        - Network File System Client
        - Network Servers
        - Networking Tools
        - Common NetworkManager submodules
        - Office Suite and Productivity
        - Atomic Host ostree support
        - Performance Tools
        - Platform Development
        - KVM platform specific packages
        - Hyper-v platform specific packages
        - Printing Client
        - Remote Desktop Clients
        - Remote Management for Linux
        - RPM Development Tools
        - Scientific Support
        - Security Tools
        - Smart Card Support
        - Standard
        - System Tools
        - TeX formatting system
        - Virtualization Client
        - Virtualization Hypervisor
        - Virtualization Platform
        - Virtualization Tools
        - Basic Web Server
        - Workstation product core
    available environments:
        - Server with GUI
        - Server
        - Workstation
        - Custom Operating System
        - Virtualization Host
    available languages:
        ----------
    installed:
        - Core
        - Development Tools
        - VMware platform specific packages
    installed environments:
        - Minimal Install
[root@master ~]#

2.3.7 pkg.list_pkgs

Lists the currently installed packages as a dictionary

[root@master ~]# salt 'node1' pkg.list_pkgs 
node1:
    ----------
    NetworkManager:
        1:1.30.0-0.3.el8
    NetworkManager-libnm:
        1:1.30.0-0.3.el8
    NetworkManager-team:
        1:1.30.0-0.3.el8
    NetworkManager-tui:
        1:1.30.0-0.3.el8
    abattis-cantarell-fonts:
        0.0.25-4.el8
    acl:
        2.2.53-1.el8
    adwaita-cursor-theme:
        3.28.0-2.el8
    adwaita-icon-theme:
        3.28.0-2.el8
    apr:
        1.6.3-11.el8
    apr-util:
        1.6.1-6.el8
    apr-util-bdb:
        1.6.1-6.el8
    apr-util-openssl:
        1.6.1-6.el8
    at-spi2-atk:
        2.26.2-1.el8
...........ellipsis

2.3.8 pkg.owner

Lists which package provides the specified file

[root@master ~]# salt 'node1' pkg.owner /usr/sbin/apachectl
node1:
    httpd
[root@master ~]# salt 'node1' pkg.owner /usr/sbin/apachectl /etc/httpd/conf/httpd.conf
node1:
    ----------
    /etc/httpd/conf/httpd.conf:
        httpd
    /usr/sbin/apachectl:
        httpd
[root@master ~]#

2.3.9 pkg.remove

Uninstall the specified software

[root@master ~]# salt 'node1' cmd.run 'rpm -aq|grep wget'
node1:
    wget-1.19.5-10.el8.x86_64
[root@master ~]# salt 'node1' pkg.remove wget
node1:
    ----------
    wget:
        ----------
        new:
        old:
            1.19.5-10.el8
[root@master ~]#
//To unload multiple files, separate them with commas

2.3.10 pkg.upgrade

Upgrade all software packages in the system or upgrade the specified software packages

//If you want to upgrade all software packages in the system, remove the name parameter
[root@master ~]# salt 'node1' pkg.upgrade name=openssl
node1:
    ----------
    openssl:
        ----------
        new:
            1:1.1.1k-4.el8
        old:
            1:1.1.1g-11.el8
    openssl-libs:
        ----------
        new:
            1:1.1.1k-4.el8
        old:
            1:1.1.1g-11.el8

//Upgrade all
[root@master ~]# salt 'node1' pkg.upgrade
node1:
    ----------
    NetworkManager:
        ----------
        new:
            1:1.30.0-10.el8_4
        old:
            1:1.30.0-0.3.el8
    NetworkManager-libnm:
        ----------
        new:
            1:1.30.0-10.el8_4
        old:
            1:1.30.0-0.3.el8
    NetworkManager-team:
        ----------
        new:
            1:1.30.0-10.el8_4
        old:
            1:1.30.0-0.3.el8
    NetworkManager-tui:
        ----------
        new:
            1:1.30.0-10.el8_4
        old:
            1:1.30.0-0.3.el8
    abattis-cantarell-fonts:
        ----------
        new:
            0.0.25-6.el8
        old:
            0.0.25-4.el8
.............ellipsis

2.4 state of common modules of saltstack

2.4.1 state.show_highstate

Displays the advanced status of the current system

[root@master ~]# salt 'node1' state.show_highstate
node1:
    ----------
    nginx-install:
        ----------
        __env__:
            base
        __sls__:
            web.nginx.nginx
        pkg:
            |_
              ----------
              name:
                  nginx
            - installed
            |_
              ----------
              order:
                  10000
    nginx-service:
        ----------
        __env__:
            base
        __sls__:
            web.nginx.nginx
        service:
            |_
              ----------
              name:
                  nginx
            |_
              ----------
              enable:
                  True
            - running
            |_
              ----------
              order:
                  10001

2.4.2 state.highstate

Execute advanced status

[root@master ~]# salt 'node1' state.highstate web.nginx.nginx
node1:
----------
          ID: nginx-install
    Function: pkg.installed
        Name: nginx
      Result: None
     Comment: The following packages would be installed/updated: nginx
     Started: 04:11:38.008551
    Duration: 1031.626 ms
     Changes:   
              ----------
              installed:
                  ----------
                  nginx:
                      ----------
                      new:
                          installed
                      old:
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: None
     Comment: Service nginx not present; if created in this state run, it would have been started
     Started: 04:11:39.065791
    Duration: 33.262 ms
     Changes:   

Summary for node1
------------
Succeeded: 2 (unchanged=2, changed=1)
Failed:    0
------------
Total states run:     2
Total run time:   1.065 s

2.4.3 state.show_state_usage

Displays the execution of advanced status in the current system

[root@master ~]# salt 'node1' state.show_state_usage
node1:
    ----------
    base:
        ----------
        count_all:
            2
        count_unused:
            1
        count_used:
            1
        unused:
            - top
        used:
            - web.nginx.nginx
    dev:
        ----------
        count_all:
            0
        count_unused:
            0
        count_used:
            0
        unused:
        used:
    prod:
        ----------
        count_all:
            0
        count_unused:
            0
        count_used:
            0
        unused:
        used:
    test:
        ----------
        count_all:
            0
        count_unused:
            0
        count_used:
            0
        unused:
        used:

2.4.4 state.show_top

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

[root@master ~]# salt 'node1' state.show_top
node1:
    ----------
    base:
        - web.nginx.nginx

2.4.5 state.top

Execute the specified top file instead of the default

[root@master ~]# salt 'node1' state.top top.sls
node1:
----------
          ID: nginx-install
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: The following packages were installed/updated: nginx
     Started: 04:21:12.353629
    Duration: 17337.336 ms
     Changes:   
              ----------
              gd:
                  ----------
                  new:
                      2.2.5-7.el8
                  old:
              libXpm:
                  ----------
                  new:
                      3.5.12-8.el8
                  old:
              libwebp:
                  ----------
                  new:
                      1.0.0-5.el8
                  old:
              nginx:
                  ----------
                  new:
                      1:1.14.1-9.module_el8.0.0+184+e34fea82
                  old:
              nginx-all-modules:
                  ----------
                  new:
                      1:1.14.1-9.module_el8.0.0+184+e34fea82
                  old:
              nginx-filesystem:
                  ----------
                  new:
                      1:1.14.1-9.module_el8.0.0+184+e34fea82
                  old:
              nginx-mod-http-image-filter:
                  ----------
                  new:
                      1:1.14.1-9.module_el8.0.0+184+e34fea82
                  old:
              nginx-mod-http-perl:
                  ----------
                  new:
                      1:1.14.1-9.module_el8.0.0+184+e34fea82
                  old:
              nginx-mod-http-xslt-filter:
                  ----------
                  new:
                      1:1.14.1-9.module_el8.0.0+184+e34fea82
                  old:
              nginx-mod-mail:
                  ----------
                  new:
                      1:1.14.1-9.module_el8.0.0+184+e34fea82
                  old:
              nginx-mod-stream:
                  ----------
                  new:
                      1:1.14.1-9.module_el8.0.0+184+e34fea82
                  old:
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: True
     Comment: Service nginx has been enabled, and is running
     Started: 04:21:29.710267
    Duration: 363.483 ms
     Changes:   
              ----------
              nginx:
                  True

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

2.4.6 state.show_sls

Displays the status data in a specific sls or sls file list on the master

[root@master ~]# salt 'node1' state.show_sls web.nginx.nginx
node1:
    ----------
    nginx-install:
        ----------
        __env__:
            base
        __sls__:
            web.nginx.nginx
        pkg:
            |_
              ----------
              name:
                  nginx
            - installed
            |_
              ----------
              order:
                  10000
    nginx-service:
        ----------
        __env__:
            base
        __sls__:
            web.nginx.nginx
        service:
            |_
              ----------
              name:
                  nginx
            |_
              ----------
              enable:
                  True
            - running
            |_
              ----------
              order:
                  10001

2.5 common modules of saltstack

2.5.1 user.add

user.add: create a user on node1.

Usage: salt '*' user.add name

[root@master ~]# salt 'node1' user.add tom
node1:
    True
[root@master ~]#

2.5.2 user.info

user.info: returns user information.

[root@master ~]# salt 'node1' user.info tom
node1:
    ----------
    fullname:
    gid:
        1001
    groups:
        - tom
    home:
        /home/tom
    homephone:
    name:
        tom
    other:
    passwd:
        x
    roomnumber:
    shell:
        /bin/bash
    uid:
        1001
    workphone:
[root@master ~]#

2.5.3 user.getent

user.getent: returns a list of all system user information

[root@master ~]# salt 'node1' user.getent 
node1:
    |_
      ----------
      fullname:
          root
      gid:
          0
      groups:
          - root
      home:
          /root
      homephone:
      name:
          root
      other:
      passwd:
          x
      roomnumber:
      shell:
          /bin/bash
      uid:
          0
      workphone:
    |_
      ----------
...................ellipsis

2.5.4 user.list_groups

user.list_groups: lists the groups to which the specified user belongs.

[root@master ~]# salt 'node1' user.list_groups tom
node1:
    - tom
[root@master ~]#

2.5.5 user.rename

user.rename: modify the user name of the specified user

[root@master ~]# salt 'node1' user.rename tom jerry
node1:
    False			//False was returned, but the operation completed successfully
[root@master ~]# salt 'node1' user.info jerry
node1:
    ----------
    fullname:
    gid:
        1001
    groups:
        - tom
    home:
        /home/tom
    homephone:
    name:
        jerry
    other:
    passwd:
        x
    roomnumber:
    shell:
        /bin/bash
    uid:
        1001
    workphone:
[root@master ~]#

2.5.6 user.delete

user.delete: delete a user on the minion side

[root@master ~]# salt 'node1' user.delete jerry
node1:
    True
[root@master ~]# salt 'node1' user.info jerry
node1:
    ----------
[root@master ~]#

2.6 salt CP of common saltstack modules

Salt CP can easily transfer files from the master to minion in batches

//Copy a single file to the / usr/src directory of the target host
[root@master ~]# salt 'node1' cmd.run 'ls /usr/src'
node1:
    debug
    kernels
[root@master ~]# salt-cp 'node1' /etc/passwd /usr/src/
node1:
    ----------
    /usr/src/passwd:
        True
[root@master ~]# salt 'node1' cmd.run 'ls /usr/src'
node1:
    debug
    kernels
    passwd
[root@master ~]#

//Copy multiple files to the / usr/src directory of the target host
[root@master ~]# salt-cp 'node1' /etc/shadow /etc/group /usr/src
node1:
    ----------
    /usr/src/group:
        True
    /usr/src/shadow:
        True
[root@master ~]# salt 'node1' cmd.run 'ls /usr/src'
node1:
    debug
    group
    kernels
    passwd
    shadow
[root@master ~]#

2.7 file of common modules of saltstack

2.7.1 file.access

Check whether the specified path exists

[root@master ~]# salt 'node1' file.access /usr/src/passwd f
node1:
    True
[root@master ~]# salt 'node1' file.access /usr/src/tom f
node1:
    False
[root@master ~]#

Check the permission information of the specified file

[root@master ~]# salt 'node1' cmd.run 'ls -l /usr/src'
node1:
    total 12
    drwxr-xr-x. 2 root root    6 May 18  2020 debug
    -rw-r--r--  1 root root  524 Nov  4 03:38 group
    drwxr-xr-x. 2 root root    6 May 18  2020 kernels
    -rw-r--r--  1 root root 1155 Nov  4 03:37 passwd
    -rw-r--r--  1 root root  800 Nov  4 03:38 shadow
[root@master ~]# salt 'node1' file.access /usr/src/passwd r 		 // Do you have read permission
node1:
    True
[root@master ~]# salt 'node1' file.access /usr/src/passwd w 		// Do you have write permission
node1:
    True
[root@master ~]# salt 'node1' file.access /usr/src/passwd x 		// Do you have execution permission
node1:
    False
[root@master ~]#

2.7.2 file.append

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

[root@master ~]# salt 'node1' cmd.run 'ls -l /root/test'
node1:
    -rw-r--r-- 1 root root 0 Nov  4 03:42 /root/test

//Add multiple rows
[root@master ~]# salt 'node1' file.append /root/test "nohao" "tom" "jerry"
node1:
    Wrote 3 lines to "/root/test"
[root@master ~]# salt 'node1' cmd.run 'cat /root/test'
node1:
    nohao
    tom
    jerry
    

//Add single line
[root@master ~]# salt 'node1' file.append /root/test "nohao tom jerry"
node1:
    Wrote 1 lines to "/root/test"
[root@master ~]# salt 'node1' cmd.run 'cat /root/test'
node1:
    nohao
    tom
    jerry
    nohao tom jerry
[root@master ~]#

2.7.3 file.basename

Gets the base name of the specified path

[root@master ~]# salt 'node1' file.basename '/root/abc/test'
node1:
    test
[root@master ~]#

2.7.4 file.dirname

Gets the directory name of the specified path

[root@master ~]# salt 'node1' file.dirname '/root/abc/test'
node1:
    /root/abc
[root@master ~]#

2.7.5 file.check_hash

Check whether the specified file matches the hash string. If it matches, it returns True. Otherwise, it returns False

[root@master ~]# salt 'node1' cmd.run 'md5sum /etc/passwd'
node1:
    9e90a725a51cc5954da67e36e26c2d19  /etc/passwd
[root@master ~]# salt 'node1' file.check_hash /etc/passwd 9e90a725a51cc5954da67e36e26c2d19
node1:
    True
[root@master ~]#

2.7.6 file.chattr

Modify the properties of the specified file

attributeSignificance to documentsMeaning to directory
aIt is only allowed to append data after this file, and no process is allowed to overwrite or truncate this fileOnly files can be created and modified in this directory, and no files can be deleted
iThis file cannot be modified, deleted, changed or movedAny process can only modify the files under the directory, and it is not allowed to create or delete files

Adds attributes to the specified file

//View current properties
[root@master ~]# salt 'node1' cmd.run 'lsattr /root'
node1:
    -------------------- /root/anaconda-ks.cfg
    -------------------- /root/test

//Add attribute
[root@master ~]# salt 'node1' file.chattr /root/test operator=add attributes=ai
node1:
    True
[root@master ~]# salt 'node1' cmd.run 'lsattr /root'
node1:
    -------------------- /root/anaconda-ks.cfg
    ----ia-------------- /root/test
[root@master ~]#

Removes attributes from the specified file

[root@master ~]# salt 'node1' file.chattr /root/test operator=remove attributes=a
node1:
    True
[root@master ~]# salt 'node1' cmd.run 'lsattr /root'
node1:
    -------------------- /root/anaconda-ks.cfg
    ----i--------------- /root/test
[root@master ~]#

2.7.7 file.chown

Set the owner and group information of the specified file

root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 8
    -rw-------. 1 root root 1181 Jul 17 10:59 anaconda-ks.cfg
    -rw-r--r--  1 root root   32 Nov  4 03:44 test
[root@master ~]# salt 'node1' file.chown /root/test tom tom
node1:
    None
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 8
    -rw-------. 1 root root 1181 Jul 17 10:59 anaconda-ks.cfg
    -rw-r--r--  1 tom  tom    32 Nov  4 03:44 test
[root@master ~]#

2.7.8 file.copy

Copying files or directories on a remote host

Copy file

[root@master ~]# salt 'node1' file.copy /root/test /root/test-2
node1:
    True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 12
    -rw-------. 1 root root 1181 Jul 17 10:59 anaconda-ks.cfg
    -rw-r--r--  1 tom  tom    32 Nov  4 03:44 test
    -rw-r--r--  1 tom  tom    32 Nov  4 04:00 test-2
[root@master ~]

Overwriting and copying a directory will overwrite a file or directory with the same name

[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 8
    -rw-------. 1 root root 1181 Jul 17 10:59 anaconda-ks.cfg
    -rw-r--r--  1 tom  tom    32 Nov  4 03:44 test
    drwxr-xr-x  2 root root    6 Nov  4 04:07 testdir
[root@master ~]# salt 'node1' cmd.run 'ls /root/testdir'
node1:
[root@master ~]# salt 'node1' file.copy /tmp /root/testdir recurse=true
node1:
    ERROR: Could not copy '/tmp' to '/root/testdir'
ERROR: Minions returned with non-zero exit code
[root@master ~]# salt 'node1' cmd.run 'ls /root/testdir'
node1:
    mysql.sock.lock
[root@master ~]# 

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

[root@master ~]# salt node1 cmd.run 'ls -l /opt/test'
node1:
    total 0
    -rw-r--r--. 1 root root 0 Nov  4 03:16 12
    -rw-r--r--. 1 root root 0 Nov  4 03:16 dei
    -rw-r--r--. 1 root root 0 Nov  4 03:16 deidq
    -rw-r--r--. 1 root root 0 Nov  4 03:16 olp

[root@master ~]# salt node1 cmd.run 'ls -l /etc/test2'
node1:
    total 0
    -rw-r--r--. 1 root root 0 Nov  4 03:17 123
    -rw-r--r--. 1 root root 0 Nov  4 03:17 456

[root@master ~]# salt node1 file.copy /opt/test /etc/test2 recurse=true remove_existing=true
node1:
    True

[root@master ~]# salt node1 cmd.run 'ls -l /etc/test2'node1:
    total 0
    -rw-r--r--. 1 root root 0 Nov  4 03:16 12
    -rw-r--r--. 1 root root 0 Nov  4 03:16 dei
    -rw-r--r--. 1 root root 0 Nov  4 03:16 deidq
    -rw-r--r--. 1 root root 0 Nov  4 03:16 olp

2.7.9 file.ditectory_exists

Judge whether the specified directory exists. If it exists, it returns True; otherwise, it returns False

Judge whether the specified directory exists. If it exists, it returns True; otherwise, it returns False

[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 64
    -rw-r--r--. 1 root root    11 Nov  4 03:11 luo
    drwxr-xr-x. 2 root root    51 Nov  4 03:16 luochuran
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lcr
    -rw-r-----. 1 root root 52909 Nov  2 03:00 master
    -rw-r--r--. 1 root root  1118 Nov  4 02:39 passwd
[root@master ~]# salt node1 file.directory_exists /opt/lcr
node1:
    False

2.7.10 file.diskusage

Recursively calculates the disk usage of the specified path and returns it in bytes

[root@master ~]# salt node1 cmd.run 'du -sb /opt/'
node1:
    54171       /opt/
[root@master ~]# salt node1 file.diskusage /opt
node1:
    54049

2.7.11 file.file_exists

Determine whether the specified file exists

[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 64
    -rw-r--r--. 1 root root    11 Nov  4 03:11 luo
    drwxr-xr-x. 2 root root    51 Nov  4 03:16 luochuran
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lcr
    -rw-r-----. 1 root root 52909 Nov  2 03:00 master
    -rw-r--r--. 1 root root  1118 Nov  4 02:39 passwd
[root@master ~]# salt node1 file.file_exists /opt/lcr
node1:
    True
[root@master ~]# salt node1 file.file_exists /opt/luochuran
node1:
    False   ##False is returned because abc is a directory rather than a file 

2.7.12 file.find

Similar to the find command and returns a list of paths that meet the specified conditions

The options include match criteria:

**

name    = path-glob                 # case sensitive
iname   = path-glob                 # case insensitive
regex   = path-regex                # case sensitive
iregex  = path-regex                # case insensitive
type    = file-types                # match any listed type
user    = users                     # match any listed user
group   = groups                    # match any listed group
size    = [+-]number[size-unit]     # default unit = byte
mtime   = interval                  # modified since date
grep    = regex                     # search file contents

and/or actions:

delete [= file-types]               # default type = 'f'
exec    = command [arg ...]         # where {} is replaced by pathname
print  [= print-opts]

and/or depth criteria:

maxdepth = maximum depth to transverse in path
mindepth = minimum depth to transverse before checking files or directories

The default action is print=path

path-glob:

*                = match zero or more chars(Match zero or more characters)
?                = match any char(Match any character)
[abc]            = match a, b, or c(matching a, b or c)
[!abc] or [^abc] = match anything except a, b, and c(Matching Division a, b, c Anything other than)
[x-y]            = match chars x through y(Match character x reach y)
[!x-y] or [^x-y] = match anything except chars x through y(Matches any character except x reach y)
{a,b,c}          = match a or b or c(matching a or b or c)

path-regex: a Python Regex (regular expression) pattern to match pathnames

file-types: a string of one or more of the following:

a: all file types(All file types)
b: block device(Block device)
c: character device(Character device)
d: directory(catalogue)
p: FIFO (named pipe)
f: plain file(Ordinary file)
l: symlink((symbolic link)
s: socket(Socket)

users: a space and/or comma separated list of user names and/or uids

groups: a space and/or comma separated list of group names and/or gids

size-unit:

b: bytes
k: kilobytes
m: megabytes
g: gigabytes
t: terabytes

interval:

[<num>w] [<num>d] [<num>h] [<num>m] [<num>s]

where:
    w: week
    d: day
    h: hour
    m: minute
    s: second

print-opts: a comma and/or space separated list of one or more of the following:

group: group name (Group name)
md5:   MD5 digest of file contents(Of file contents md5 (summary)
mode:  file permissions (as integer)  (file right(In integer form))
mtime: last modification time (as time_t)  (Last modification time(As time_t))
name:  file basename   (file)
path:  file absolute path  ((absolute path to file)
size:  file size in bytes  (File size in bytes)
type:  file type   ((file type)
user:  user name   (User name)

Example:

salt '*' file.find / type=f name=\*.bak size=+10m   (The matching file suffix is.bak And greater than 10 M (file of)

salt '*' file.find /var mtime=+30d size=+10m print=path,size,mtime  (Match files modified 30 days ago and greater than 10 M The path and size of the file and the time of the last modification)

salt '*' file.find /var/log name=\*.[0-9] mtime=+30d size=+10m delete (The suffix name of the matching file ends with a number and was modified 30 days ago and is greater than 10 M And delete them)

2.7.13 file.get_gid

Gets the gid of the specified file

[root@master ~]# salt node1 cmd.run 'ls -l /opt/lcr'
node1:
    -rw-r--r--. 1 root root 11 Nov  4 02:54 /opt/lcr
[root@master ~]# salt node1 file.get_gid /opt/lcr
node1:
    0

2.7.14 file.get_group

Gets the group name of the specified file

[root@master ~]# salt node1 cmd.run 'ls -l /opt/lcr'
node1:
    -rw-r--r--. 1 root root 11 Nov  4 02:54 /opt/lcr
[root@master ~]# salt node1 file.get_group /opt/lcr
node1:
    root

2.7.15 file.get_hash

Gets the hash value of the specified file, which is obtained through the sha256 algorithm

[root@master ~]# salt node1 cmd.run 'sha256sum /opt/lcr'
node1:
    487aca02c302a0ac061a3a2cf837e68d006169c3a9501af8b9c56d21ef235a62  /opt/lcr
[root@master ~]# salt node1 file.get_hash /opt/lcr
node1:
    487aca02c302a0ac061a3a2cf837e68d006169c3a9501af8b9c56d21ef235a62

2.7.16 file.get_mode

Gets the permission of the specified file and displays it in digital form

[root@master ~]# salt node1 cmd.run 'ls -l /opt/lcr'
node1:
    -rw-r--r--. 1 root root 11 Nov  4 02:54 /opt/lcr
[root@master ~]# salt node1 file.get_mode /opt/lcr
node1:
    0644

2.7.17 file.get_selinux_context

Gets the SELINUX context information of the specified file

[root@master ~]# salt node1 cmd.run 'ls -Z /opt/lcr'
node1:
    unconfined_u:object_r:usr_t:s0 /opt/lcr
[root@master ~]# salt node1 file.get_selinux_context /opt/lcr
node1:
    unconfined_u:object_r:usr_t:s0

2.7.18 file.get_sum

Calculate the signature code of the specified file according to the specified algorithm and display the sha256 algorithm used by default.
The algorithm parameters that can be used by this function are:

  • md5
  • sha1
  • sha224
  • sha256 (default)
  • sha384
  • sha512
[root@master ~]# salt node1 cmd.run 'sha256sum /opt/lcr'
node1:
    487aca02c302a0ac061a3a2cf837e68d006169c3a9501af8b9c56d21ef235a62  /opt/lcr
[root@master ~]# salt node1 file.get_sum /opt/lcr
node1:
    487aca02c302a0ac061a3a2cf837e68d006169c3a9501af8b9c56d21ef235a62
[root@master ~]# salt node1 cmd.run 'md5sum /opt/lcr'
node1:
    861b478acdd37e1a909646b04e82290b  /opt/lcr
[root@master ~]# salt node1 file.get_sum /opt/lcr md5
node1:
    861b478acdd37e1a909646b04e82290b

2.7.19 file.get_uid and file.get_user

Gets the uid or user name of the specified file

[root@master ~]# salt node1 cmd.run 'ls -l /opt/lcr'
node1:
    -rw-r--r--. 1 root root 11 Nov  4 02:54 /opt/lcr
[root@master ~]# salt node1 file.get_uid /opt/lcr 
node1:
    0
[root@master ~]# salt node1 file.get_user /opt/lcr 
node1:
    root

2.7.20 file.gid_to_group

Converts the specified gid to a group name and displays it

[root@master ~]# salt node1 file.gid_to_group 1000
node1:
    luo
[root@master ~]# salt node1 file.gid_to_group 1001
node1:
    lcr
[root@master ~]# salt node1 file.gid_to_group 0
node1:
    root

2.7.21 file.group_to_gid

Converts the specified group name to gid and displays it

[root@master ~]# salt node1 file.group_to_gid liu
node1:
    1000
[root@master ~]# salt node1 file.group_to_gid root
node1:
    0

2.7.22 file.grep

Retrieves the specified content from the specified file
This function supports wildcards. If wildcards are used in the specified path, they must be enclosed in double quotes

[root@master ~]# salt node1 file.grep /opt/luo service
node1:
    ----------
    pid:
        176727
    retcode:
        0
    stderr:
    stdout:
        service
[root@master ~]# salt node1 file.grep /opt/luo service -- -i
node1:
    ----------
    pid:
        177132
    retcode:
        0
    stderr:
    stdout:
        service
[root@master ~]# salt node1 file.grep /opt/luo service -- -i -B2
node1:
    ----------
    pid:
        177707
    retcode:
        0
    stderr:
    stdout:
        best
        master
        service
[root@master ~]# salt node1 file.grep /opt/luo service -- -i -l
node1:
    ----------
    pid:
        177997
    retcode:
        0
    stderr:
    stdout:
        /opt/liu

grep parameter:

  • -a or -- text: do not ignore binary data.
  • -A < display rows > or -- after context = < display rows >: displays the contents after the row in addition to the column conforming to the template style.
  • -b or -- byte offset: the number of the first character of the line is marked before the line conforming to the style is displayed.
  • -B < number of display lines > or -- before context = < number of display lines >: in addition to the line that conforms to the style, the content before the line is displayed.
  • -c or -- count: calculate the number of columns that conform to the style.
  • -C < number of display lines > or -- context = < number of display lines > or - < number of display lines >: in addition to the line that conforms to the style, the contents before and after the line are displayed.
  • -D < action > or -- directories = < action >: this parameter must be used when you specify that you want to find a directory rather than a file, otherwise the grep instruction will report information and stop the action.
  • -E < template style > or -- regexp = < template style >: specify the string as the style to find the contents of the file.
  • -E or -- extended regexp: use regular expressions with extended style.
  • -F < rule file > or -- file = < rule file >: specify a rule file whose content contains one or more rule styles, and let grep find the file content that meets the rule conditions. The format is one rule style per line.
  • -F or -- Fixed regexp: a list that treats styles as fixed strings.
  • -G or -- Basic regexp: use the style as a common notation.
  • -h or -- no filename: the file name to which the line belongs is not marked before the line conforming to the style is displayed.
  • -H or -- with filename: indicates the name of the file to which the line belongs before the line matching the style is displayed.
  • -i or -- ignore case: ignore the case difference of characters.
  • -l or -- file with matches: lists the file names whose file contents conform to the specified style.
  • -L or -- files without match: lists the file names whose file contents do not conform to the specified style.
  • -n or -- line number: indicates the column number of the row before displaying the row that conforms to the style.
  • -o or -- only matching: only the matching PATTERN is displayed.
  • -q or -- quiet or – silent: no information is displayed.
  • -r or -- recursive: this parameter has the same effect as specifying the "- D recursive" parameter.
  • -s or -- no messages: no error messages are displayed.
  • -v or -- convert match: displays all lines that do not contain matching text.
  • -V or -- version: displays version information.
  • -w or -- word regexp: only columns that match the whole word are displayed.
  • -X -- line regexp: only columns that match all columns are displayed.
  • -y: this parameter has the same effect as specifying the "- i" parameter.

2.7.23 file.is_blkdev

Determines whether the specified file is a block device file

[root@master ~]# salt node1 cmd.run 'ls -l /dev/sda'
node1:
    brw-rw----. 1 root disk 8, 0 Nov  4 02:17 /dev/sda
[root@master ~]# salt node1 file.is_blkdev /dev/sda
node1:
    True

2.7.24 file.lsattr

Check and display the attribute information of the specified file

[root@master ~]# salt node1 cmd.run 'lsattr /opt/lcr'
node1:
    -------------------- /opt/lq
[root@master ~]# salt node1 cmd.run 'chattr +i /opt/lcr'
node1:
[root@master ~]# salt node1 file.lsattr /opt/lcr
node1:
    ----------
    /opt/lcr:
        - i

2.7.25 file.mkdir

Create directory and set owner, group and permission

[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
    total 60
    -rw-------. 1 root root  1023 Jul 16 07:36 anaconda-ks.cfg
    -rw-r--r--. 1 root root     7 Nov  4 02:36 luo.sh
[root@master ~]# salt node1 file.mkdir /root/test
node1:
    True
[root@master ~]# salt node1 cmd.run 'ls -l /root/test'
node1:
    total 0
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
    total 60
    -rw-------. 1 root root  1023 Jul 16 07:36 anaconda-ks.cfg
    drwxr-xr-x. 2 root root     6 Nov  4 04:09 test
    -rw-r--r--. 1 root root     7 Nov  4 02:36 liu.sh

[root@master ~]# salt node1 file.mkdir /root/test2 lcr lcr 250
node1:
    True
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
    total 60
    -rw-------. 1 root root  1023 Jul 16 07:36 anaconda-ks.cfg
    drwxr-xr-x. 2 root root     6 Nov  4 04:09 test
    -rw-r--r--. 1 root root     7 Nov  4 02:36 liu.sh
    d-w-r-x---. 2 lq   lq       6 Nov  4 04:10 test2

file.move

Move or rename

[root@master ~]# salt node1 cmd.run 'ls -l /opt'
node1:
    total 64
    -rw-r--r--. 1 root root    26 Nov  4 03:57 luo
    drwxr-xr-x. 2 root root    51 Nov  4 03:16 luochuran
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lcr
    -rw-r-----. 1 root root 52909 Nov  2 03:00 master
    -rw-r--r--. 1 root root  1118 Nov  4 02:39 passwd
[root@master ~]# salt node1 file.move /opt/liu /mnt/test
node1:
    ----------
    comment:
        '/opt/liu' moved to '/mnt/test'
    result:
        True
[root@master ~]# salt node1 cmd.run 'ls -l /mnt/test'
node1:
    -rw-r--r--. 1 root root 26 Nov  4 03:57 /mnt/test

file.prepend

Inserts text at the beginning of the specified file

[root@master ~]# salt node1 cmd.run 'cat /opt/lcr'
node1:
    me
    is
    best
[root@master ~]# salt node1 cmd.run 'cat /opt/luo'
node1:
    me
    is 
    best
[root@master ~]# salt node1 file.prepend /opt/luo "dd" "meme" 
node1:
    Prepended 2 lines to "/opt/luo"
[root@master ~]# salt node1 cmd.run 'cat /opt/luo'node1:
    dd
    meme
    me
    is 
    best

file.sed

Modify the contents of the text file

[root@master ~]# salt node1 cmd.run 'cat /opt/luo'node1:
    dd
    meme
    me
    is 
    best
[root@master ~]# salt node1 file.sed /opt/luo "dd" "gege"node1:
    ----------
    pid:
        359178
    retcode:
        0
    stderr:
    stdout:
[root@master ~]# salt node1 cmd.run 'cat /opt/luo'
node1:
    gege
    meme
    me
    is 
    best

file.read

Read file contents

[root@master ~]# salt node1 cmd.run 'cat /opt/luo'
node1:
    gege
    meme
    me
    is 
    best
[root@master ~]# salt node1 file.read /opt/luo
node1:
    gege
    meme
    me
    is 
    best

file.readdir

Lists all files or directories in the specified directory, including hidden files

[root@master ~]# salt node1 file.readdir /root
node1:
    - .
    - ..
    - .bash_logout
    - .bash_profile
    - .bashrc
    - .cshrc
    - .tcshrc
    - anaconda-ks.cfg
    - .bash_history
    - .config
    - !
    - luo.sh
    - best
    - you
    - .mysql_history
    - 1
    - .viminfo

file.remove

Delete the specified file or directory. If the directory is given, it will be deleted recursively

[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 68
    -rw-r--r--. 1 root root    22 Nov  7 02:03 luo
    drwxr-xr-x. 2 root root    51 Nov  4 03:16 luochuran
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lcr
    -rw-r-----. 1 root root 52909 Nov  2 03:00 master
    -rw-r--r--. 1 root root  1118 Nov  4 02:39 passwd
[root@master ~]# salt node1 file.remove '/opt/luochuran'
node1:
    True
[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 68
    -rw-r--r--. 1 root root    22 Nov  7 02:03 luo
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lcr
    -rw-r-----. 1 root root 52909 Nov  2 03:00 master
    -rw-r--r--. 1 root root  1118 Nov  4 02:39 passwd

file.rename

Rename a file or directory

[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 68
    -rw-r--r--. 1 root root    22 Nov  7 02:03 luo
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lcr
    -rw-r-----. 1 root root 52909 Nov  2 03:00 master
    -rw-r--r--. 1 root root  1118 Nov  4 02:39 passwd
[root@master ~]# salt node1 file.rename /opt/liu /opt/ran
node1:
    True
[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 68
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lcr
    -rw-r-----. 1 root root 52909 Nov  2 03:00 master
    -rw-r--r--. 1 root root  1118 Nov  4 02:39 passwd
    -rw-r--r--. 1 root root    22 Nov  7 02:03 ran

file.set_mode

Set permissions for the specified file

[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 68
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lcr
    -rw-r-----. 1 root root 52909 Nov  2 03:00 master
    -rw-r--r--. 1 root root  1118 Nov  4 02:39 passwd
    -rw-r--r--. 1 root root    22 Nov  7 02:03 ran
[root@master ~]# salt node1 file.set_mode /opt/ran 0777
node1:
    0777
[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 68
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lcr
    -rw-r-----. 1 root root 52909 Nov  2 03:00 master
    -rw-r--r--. 1 root root  1118 Nov  4 02:39 passwd
    -rwxrwxrwx. 1 root root    22 Nov  7 02:03 ran

file.symlink

Creates a soft link to the specified file

[root@master ~]# salt node1 file.symlink /opt/lq /mnt/ltest
node1:
    True
[root@master ~]# salt node1 cmd.run 'ls -l /opt/;ls -l /mnt'
node1:
    total 68
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lcr
    -rw-r-----. 1 root root 52909 Nov  2 03:00 master
    -rw-r--r--. 1 root root  1118 Nov  4 02:39 passwd
    -rwxrwxrwx. 1 root root    22 Nov  7 02:03 ran
    total 8
    drwxr-xr-x. 2 root root  6 Jul 16 07:33 hgfs
    lrwxrwxrwx. 1 root root  7 Nov  7 02:11 test -> /opt/lq
    -rw-r--r--. 1 root root 11 Nov  4 02:54 lcr

file.touch

Create an empty file or update the timestamp

[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 68
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lcr
    -rw-r-----. 1 root root 52909 Nov  2 03:00 master
    -rw-r--r--. 1 root root  1118 Nov  4 02:39 passwd
    -rwxrwxrwx. 1 root root    22 Nov  7 02:03 ran
[root@master ~]# salt node1 file.touch /opt/test
node1:
    True
[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 68
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lq
    -rw-r--r--. 1 root root     0 Nov  7 02:13 test
    -rw-r-----. 1 root root 52909 Nov  2 03:00 master
    -rw-r--r--. 1 root root  1118 Nov  4 02:39 passwd
    -rwxrwxrwx. 1 root root    22 Nov  7 02:03 ran

file.uid_to_user

Convert the specified uid into user name and display it

[root@master ~]# salt node1 file.uid_to_user 0
node1:
    root
[root@master ~]# salt node1 file.uid_to_user 1000
node1:
    tom

file.user_to_uid

Convert the specified user into uid and display it

[root@master ~]# salt node1 file.user_to_uid tom
node1:
    1000

file.write

Overwrite and write the specified content to a specified file

[root@master ~]# salt node1 cmd.run 'cat /opt/test'
node1:
    hello
[root@master ~]# salt 'node1' file.write /opt/test "hello" "haha" "xixi"
node1:
    Wrote 3 lines to "/opt/liu"
[root@master ~]# salt node1 cmd.run 'cat /opt/test'node1:
    hello
    haha
    xixi

Posted by dfarrell on Mon, 08 Nov 2021 10:06:28 -0800