Common modules of SaltStack

Posted by tsukushi on Fri, 05 Nov 2021 23:31:02 +0100

Common modules of SaltStack

1. SaltSack module introduction:

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 module s
[root@master ~]# salt 'master' sys.list_modules
master:
    - acl
    - aliases
    - alternatives
    - apache
    - archive
    - artifactory
    - baredoc
    - beacons
    - bigip
    - btrfs
    - buildout
    - chroot
    - cloud
    - cmd
    - composer
    - config
    - consul
    - container_resource
    - cp
    - cron
    - cryptdev
    - data
    - defaults
    - devinfo
 .....................
 
# View all functions of the specified module
[root@master ~]# salt 'master' sys.list_functions user
master:
    - user.add
    - user.chfullname
    - user.chgid
    - user.chgroups
    - user.chhome
    - user.chhomephone
    - user.chloginclass
    - user.chother
    - user.chroomnumber
    - user.chshell
    - user.chuid
    - user.chworkphone
    - user.delete
    - user.get_loginclass
    - user.getent
    - user.info
    - user.list_groups
    - user.list_users
    - user.primary_group
    - user.rename
    
# View the usage of the specified module (b)
[root@master ~]# salt 'master' sys.doc user |less
user.add:

    Add a user to the minion

    name
        Username LOGIN to add

    uid
        User ID of the new account

    gid
        Name or ID of the primary group of the new account

    groups
        List of supplementary groups of the new account

    home
        Home directory of the new account

    shell
        Login shell of the new accoun
     ...................................   
     
# 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,date,sshd
node1:
    ----------
    cmd.run:
        Wed Nov  3 06:01:25 EDT 2021
    service.status:
        True
    test.echo:
        hello

2. Common modules of saltstack:

2.1 network of common modules

2.1.1 network.active_tcp

Returns all active tcp connections (similar to the ss -ant command)

[root@master ~]# salt 'node1' network.active_tcp
node1:
    ----------
    0:
        ----------
        local_addr:
            192.168.220.10
        local_port:
            22
        remote_addr:
            192.168.220.1
        remote_port:
            62157
    1:
        ----------
        local_addr:
            192.168.220.10
        local_port:
            49776
        remote_addr:
            192.168.220.9
        remote_port:
            4505
    2:
        ----------
        local_addr:
            192.168.220.10
        local_port:
            22
        remote_addr:
            192.168.220.1
        remote_port:
            62158
2.1.2 nwetwork.calc_net

Calculate the network segment through IP and subnet mask

[root@master ~]# salt 'node1' network.calc_net 192.168.220.10 255.255.255.0
node1:
    192.168.220.0/24

[root@master ~]# salt 'node1' network.calc_net 192.168.1.10 255.255.240.0
node1:
    192.168.0.0/20
    
[root@master ~]# salt 'node1' network.calc_net 172.15.1.3 255.255.0.0
node1:
    172.15.0.0/16   
2.1.3 network.connect

Test whether the network from minion to a server is connected

[root@master ~]# salt 'node1' network.connect www.baidu.com 80
node1:
    ----------
    comment:
        Successfully connected to www.baidu.com (182.61.200.7) on tcp port 80
    result:
        True
        
 [root@master ~]# salt 'node1' network.connect 192.168.220.9 22
node1:
    ----------
    comment:
        Successfully connected to 192.168.220.9 (192.168.220.9) on tcp port 22
    result:
        True       
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.220.2
      interface:
          ens33
      netmask:
          0.0.0.0       
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 ~]# salt 'master' network.get_fqdn
master:
    master
2.1.6 network.get_hostname

Get host name

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

Query the routing information of a target network

[root@master ~]# salt 'node1' network.get_route 192.168.220.9
node1:
    ----------
    destination:
        192.168.220.9
    gateway:
        None
    interface:
        ens33
    source:
        192.168.220.10
        
[root@master ~]# salt 'node1' network.get_route 192.168.220.10
node1:
    ----------
    destination:
        192.168.220.10
    gateway:
        None
    interface:
        lo
    source:
        192.168.220.10      
        
[root@master ~]# salt 'node1' network.get_route 192.168.162.109
node1:
    ----------
    destination:
        192.168.162.109
    gateway:
        192.168.220.2
    interface:
        ens33
    source:
        192.168.220.10        
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:05:72:c7
    
[root@master ~]# salt 'node1' network.hw_addr lo
node1:
    00:00:00:00:00:00
    
[root@master ~]# salt 'master' network.hw_addr lo
master:
    00:00:00:00:00:00
    
[root@master ~]# salt 'master' network.hw_addr ens33
master:
    00:0c:29:34:91:07
2.1.9 network.ifacestartswith

Retrieves the interface name from a specific network

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

Judge whether the current host is in a network segment

[root@master ~]# salt 'node1' network.in_subnet 192.168.220.0/24
node1:
    True
    
[root@master ~]# salt 'node1' network.in_subnet 172.16.0.0/16
node1:
    False
2.1.11 network.interface

Returns the information of the specified network card

[root@master ~]# salt 'node1' network.interface ens33
node1:
    |_
      ----------
      address:
          192.168.220.10
      broadcast:
          192.168.220.255
      label:
          ens33
      netmask:
          255.255.255.0
         
[root@master ~]# salt 'node1' network.interface lo
node1:
    |_
      ----------
      address:
          127.0.0.1
      broadcast:
          None
      label:
          lo
      netmask:
          255.0.0.0         
2.1.12 network.interface_ip

Returns the IP address of the specified network card

[root@master ~]# salt 'node1' network.interface_ip ens33
node1:
    192.168.220.10
    
[root@master ~]# salt 'node1' network.interface_ip lo
node1:
    127.0.0.1
    
[root@master ~]# salt 'master' network.interface_ip lo
master:
    127.0.0.1
    
[root@master ~]# salt 'master' network.interface_ip ens33
master:
    192.168.220.9
2.1.13 network.interfaces

Returns all network card information in the current system

[root@master ~]# salt '*' network.interfaces
node1:
    ----------
    ens33:
        ----------
        hwaddr:
            00:0c:29:05:72:c7
        inet:
            |_
              ----------
              address:
                  192.168.220.10
              broadcast:
                  192.168.220.255
              label:
                  ens33
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
              address:
                  fe80::2166:5bdf:402b:32c9
              prefixlen:
                  64
              scope:
                  link
            |_
              ----------
              address:
                  fe80::cc61:eeb0:86a:e547
              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
master:
    ----------
    ens33:
        ----------
        hwaddr:
            00:0c:29:34:91:07
        inet:
            |_
              ----------
              address:
                  192.168.220.9
              broadcast:
                  192.168.220.255
              label:
                  ens33
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
              address:
                  fe80::2166:5bdf:402b:32c9
              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
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 '*' network.ip_addrs
node1:
    - 192.168.220.10
master:
    - 192.168.220.9
    
# Add a temporary IP address to node1 and use the command to obtain it
[root@node1 ~]# ip addr add 192.168.220.11/24 dev ens33
[root@node1 ~]# ip a
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:05:72:c7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.220.10/24 brd 192.168.220.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.220.11/24 scope global secondary ens33
    
[root@master ~]# salt '*' network.ip_addrs
node1:
    - 192.168.220.10
    - 192.168.220.11
master:
    - 192.168.220.9
    
2.1.15 network.netstat

Returns all open ports and status

[root@master ~]# salt 'node1' network.netstat
node1:
    |_
      ----------
      inode:
          26453
      local-address:
          127.0.0.1:323
      program:
          chronyd
      proto:
          udp
      recv-q:
          0
      remote-address:
          0.0.0.0:*
      send-q:
          0
      user:
          0
    |_
      ----------
      inode:
          26454
      local-address:
          [::1]:323
      program:
          chronyd
      proto:
          udp
      recv-q:
          0
      remote-address:
          [::]:*
      send-q:
          0
      user:
          0
    |_
      ----------
      inode:
          28031
      local-address:
          0.0.0.0:8080
      program:
          nginx
      proto:
          tcp
      recv-q:
          0
      remote-address:
          0.0.0.0:*
      send-q:
          128
      state:
          LISTEN
      user:
          0
    |_
      ----------
      inode:
          27842
      local-address:
          0.0.0.0:22
      program:
          sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          0.0.0.0:*
      send-q:
          128
      state:
          LISTEN
      user:
          0
    |_
      ----------
      inode:
          0
      local-address:
          192.168.220.10:48016
      program:
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.220.9:4506
      send-q:
          0
      state:
          TIME-WAIT
      user:
          0
    |_
      ----------
      inode:
          28366
      local-address:
          192.168.220.10:22
      program:
          sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.220.1:62157
      send-q:
          0
      state:
          ESTABLISHED
      user:
          0
    |_
      ----------
      inode:
          37896
      local-address:
          192.168.220.10:49776
      program:
          salt-minion
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.220.9:4505
      send-q:
          0
      state:
          ESTABLISHED
      user:
          0
    |_
      ----------
      inode:
          31023
      local-address:
          192.168.220.10:22
      program:
          sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.220.1:62158
      send-q:
          0
      state:
          ESTABLISHED
      user:
          0
    |_
      ----------
      inode:
          27977
      local-address:
          *:80
      program:
          httpd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          *:*
      send-q:
          128
      state:
          LISTEN
      user:
          0
    |_
      ----------
      inode:
          27844
      local-address:
          [::]:22
      program:
          sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          [::]:*
      send-q:
          128
      state:
          LISTEN
      user:
          0
2.1.16 network.ping

Use the ping command to test connectivity to a host

[root@master ~]# salt 'node1' network.ping baidu.com
node1:
    PING baidu.com (220.181.38.251) 56(84) bytes of data.
    64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=1 ttl=128 time=67.4 ms
    64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=2 ttl=128 time=46.7 ms
    64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=3 ttl=128 time=80.0 ms
    64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=4 ttl=128 time=63.2 ms
    
    --- baidu.com ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3006ms
    rtt min/avg/max/mdev = 46.656/64.318/80.004/11.929 ms
    
    
[root@master ~]# salt 'node1' network.ping 192.168.220.9
node1:
    PING 192.168.220.9 (192.168.220.9) 56(84) bytes of data.
    64 bytes from 192.168.220.9: icmp_seq=1 ttl=64 time=0.334 ms
    64 bytes from 192.168.220.9: icmp_seq=2 ttl=64 time=0.170 ms
    64 bytes from 192.168.220.9: icmp_seq=3 ttl=64 time=0.236 ms
    64 bytes from 192.168.220.9: icmp_seq=4 ttl=64 time=0.233 ms
    
    --- 192.168.220.9 ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3054ms
    rtt min/avg/max/mdev = 0.170/0.243/0.334/0.059 ms
2.1.17 network.reverse_ip

Returns the reverse address of a specified IP address

[root@master ~]# salt 'node1' network.reverse_ip 192.168.220.9
node1:
    9.220.168.192.in-addr.arpa
    
[root@master ~]# salt 'node1' network.reverse_ip 172.16.22.5
node1:
    5.22.16.172.in-addr.arpa
2.1.18 network.convert_cidr

Calculate the subnet mask that can be set

[root@master ~]# salt 'node1' network.convert_cidr 192.168.220.9/24
node1:
    ----------
    broadcast:
        192.168.220.255
    netmask:
        255.255.255.0
    network:
        192.168.220.0
[root@master ~]# salt 'node1' network.convert_cidr 172.16.2.10/24
node1:
    ----------
    broadcast:
        172.16.2.255
    netmask:
        255.255.255.0
    network:
        172.16.2.0

2.2 service s of common modules of saltstack

2.2.1 service.available

Judge whether the specified service is available (see yes, whether it is installed)

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

Get all running services

[root@master ~]# salt 'node1' service.get_all
node1:
    - NetworkManager
    - NetworkManager-dispatcher
    - NetworkManager-wait-online
    - auditd
    - autovt@
    - basic.target
    - blk-availability
    - bluetooth.target
    - boot-complete.target
    - chrony-dnssrv@
    - chrony-dnssrv@.timer
    - chrony-wait
    - chronyd
    - dbus-org.freedesktop.timedate1
    - dbus.socket
    - debug-shell
    - default.target
    - initrd.target
    - network.target
    - nftables
    - nginx
    - nis-domainname
    - nss-lookup.target
    - nss-user-lookup.target
    - paths.target
    - plymouth-halt
    - plymouth-kexec
    - plymouth-poweroff
    - plymouth-quit
    - poweroff.target
.....................................
    - remote-fs.target
    - rescue
    - rescue.target
    - rhsm
    - rhsm-facts
    - rhsmcertd
    - rngd
    - rngd-wake-threshold
    - rpcbind.target
    - rsyslog
    - run-vmblock\x2dfuse.mount
    - runlevel0.target
    - runlevel1.target
    - runlevel2.target
    - runlevel3.target
    - shutdown.target
    - sigpwr.target
    - sleep.target
    - slices.target
    - smartcard.target
    - sssd-pam
    - sssd-pam-priv.socket
    - sssd-pam.socket
    - sssd-ssh
    - sssd-ssh.socket
    - systemd-update-utmp-runlevel
    - systemd-user-sessions
    - systemd-vconsole-setup
    - user@
    - vgauthd
    - vmtoolsd
2.2.3 service.disabled

Check whether the specified service is in the state of startup without self startup

[root@master ~]# salt 'node1' service.disabled nginx
node1:
    True
    
[root@master ~]# salt 'master' service.disabled httpd
master:
    True
  
[root@master ~]# systemctl status httpd   # Check whether the httpd status on the master is on but not on
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled(It is the state that the power on does not start automatically); vendor>
   Active: active (running) since Tue 2021-11-02 20:53:50 EDT; 6h ago
     Docs: man:httpd.service(8)
 Main PID: 896 (httpd)
   Status: "Running, listening on: port 80"
    Tasks: 213 (limit: 11201)
   Memory: 45.1M
   CGroup: /system.slice/httpd.service
           ├─896 /usr/sbin/httpd -DFOREGROUND
           ├─939 /usr/sbin/httpd -DFOREGROUND  
2.2.4 service.enabled

Check whether the specified service is self started

[root@master ~]# salt 'node1' service.enabled nginx
node1:
    False
    
[root@master ~]# salt 'master' service.enabled httpd
master:
    False
   
[root@master ~]# salt 'node1' service.enabled sshd
node1:
    True
    
    
# Check whether the sshd on node1 is in the auto start state
[root@node1 ~]# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled(Is the state of startup and self startup); vendor p>
   Active: active (running) since Wed 2021-11-03 01:01:55 EDT; 2h 3min ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 911 (sshd)
    Tasks: 1 (limit: 11201)
   Memory: 8.0M
   CGroup: /system.slice/sshd.service
           └─911 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha
2.2.5 service.disable

Set the specified service not to start automatically

[root@master ~]# salt '*' service.disable httpd
master:
    True
node1:
    True
    
# Check that the httpd setting on the master is successfully set to no self startup (disabled)
[root@master ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled(Successfully set); vendor >
   Active: active (running) since Tue 2021-11-02 20:53:50 EDT; 6h ago
     Docs: man:httpd.service(8)
 Main PID: 896 (httpd)
   Status: "Running, listening on: port 80"
    Tasks: 213 (limit: 11201)
   Memory: 45.1M
   CGroup: /system.slice/httpd.service
           ├─896 /usr/sbin/httpd -DFOREGROUND
           ├─939 /usr/sbin/httpd -DFOREGROUND
2.2.6 service enable

Set the specified service to start automatically

[root@master ~]# salt '*' service.enable httpd
node1:
    True
master:
    True
  
    
# Check whether the httpd on the master is successfully set to enabled
[root@master ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled(Successfully set); vendor >
   Active: active (running) since Tue 2021-11-02 20:53:50 EDT; 6h ago
     Docs: man:httpd.service(8)
 Main PID: 896 (httpd)
   Status: "Running, listening on: port 80"
    Tasks: 213 (limit: 11201)
   Memory: 45.1M
   CGroup: /system.slice/httpd.service
           ├─896 /usr/sbin/httpd -DFOREGROUND
           ├─939 /usr/sbin/httpd -DFOREGROUND
2.2.7 service.reload

Reload the specified service (the service must be started to reload)

[root@master ~]# salt '*' service.reload httpd
node1:
    True
master:
    True
    
# Close the httpd service on the master and then execute the reload command
[root@master ~]# systemctl stop httpd

[root@master ~]# salt '*' service.reload httpd
master:   
    ERROR: httpd.service is not active, cannot reload.   # Error: httpd. The service is not activated and cannot be reloaded.
node1:  
    True
ERROR: Minions returned with non-zero exit code
2.2.8 service.stop

Stop the specified service

[root@master ~]# salt 'master' service.stop httpd
master:
    True
    
 # Check whether the httpd on the master is stopped
 [root@master ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor>
   Active: inactive (dead)(Dead)
     Docs: man:httpd.service(8)

Nov 02 20:53:50 master systemd[1]: Started The Apache HTTP Server.
Nov 02 20:53:51 master httpd[896]: Server configured, listening on: port 80
Nov 03 03:15:54 master systemd[1]: Reloading The Apache HTTP Server.
Nov 03 03:15:55 master httpd[891467]: AH00558: httpd: Could not reliably d>
Nov 03 03:15:55 master systemd[1]: Reloaded The Apache HTTP Server.
Nov 03 03:15:55 master httpd[896]: Server configured, listening on: port 80
2.2.9 service.start

Start the specified service

[root@master ~]# salt 'master' service.start httpd
master:
    True
    
# Check whether httpd on the master is started
[root@master ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor>
   Active: active (running)(function) since Wed 2021-11-03 03:22:02 EDT; 13s ago
     Docs: man:httpd.service(8)
 Main PID: 915048 (httpd)
   Status: "Running, listening on: port 80"
    Tasks: 213 (limit: 11201)
   Memory: 27.5M
   CGroup: /system.slice/httpd.service
           ├─915048 /usr/sbin/httpd -DFOREGROUND
           ├─915058 /usr/sbin/httpd -DFOREGROUND
2.2.10 service.restart

Restart the specified service

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

View the status of the specified service

[root@master ~]# salt '*' service.status httpd
master:
    True
node1:
    True
  
# Stop httpd on the master and check again
[root@master ~]# systemctl stop httpd

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

The opposite of service. Returns true if the named service is not available.

[root@master ~]# salt '*' service.missing nginx
node1:
    False
master:
    True
    
# There is nginx on node1, which returns False. There is no nginx on master, which returns True    

2.3 pkg of common modules of saltstack

2.3.1 pkg.download

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

# Check to see if Yum Utila is installed on node1        
[root@node1 ~]# rpm -qa |grep yum-utils
yum-utils-4.0.21-3.el8.noarch

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

# View wget downloaded from node1
[root@node1 ~]# ls /var/cache/yum/packages/
wget-1.19.5-10.el8.x86_64.rpm
2.3.2 pkg.file_list

Lists the files for the specified package or all packages installed on the system (same as rpm -ql)

# Lists all files provided by the installed apache package
[root@master ~]# salt 'node1' pkg.file_list nginx
node1:
    ----------
    errors:
    files:
        - /etc/logrotate.d/nginx
        - /etc/nginx/fastcgi.conf
        - /etc/nginx/fastcgi.conf.default
        - /etc/nginx/fastcgi_params
        - /etc/nginx/fastcgi_params.default
        - /etc/nginx/koi-utf
   .....................................     
   
2.3.3 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
2.3.4 pkg.group_list

Lists all package groups in the system

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

Install software

[root@master ~]# salt 'node1' pkg.install make
node1:
    ----------
    make:
        ----------
        new:
            1:4.2.1-11.el8
        old:
2.3.6 pkg.list_downladed

Lists the packages that have been downloaded locally (to the current host, not the controlled host)

[root@master ~]# salt '*' pkg.list_downloaded
node1:
    ----------
master:
    ----------
    
# No packages directory
[root@master ~]# ls /var/cache/dnf/
appstream-773ef6463612e8e2    extras-filenames.solvx
appstream-filenames.solvx     extras.solv
appstream.solv                last_makecache
baseos-055ffcb2ec25a27f       packages.db
baseos-filenames.solvx        salt-latest-repo-c9e9d9520ddd8763
baseos.solv                   salt-latest-repo-filenames.solvx
commandline-a76fe31ae310b0c7  salt-latest-repo.solv
expired_repos.json            tempfiles.json
extras-9705a089504ff150
2.3.7 pkg.list_pkgs

Lists the currently installed packages as a dictionary

[root@master ~]# salt '*' 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
        ................
2.3.8 pkg.owner

Lists which package the specified file is provided by (files that must already exist)

[root@master ~]# salt '*' pkg.owner /etc/passwd
node1:
    setup
master:
    setup
    
[root@master ~]# salt '*' pkg.owner /usr/bin/vim
node1:
    vim-enhanced
master:
    vim-enhanced    
2.3.9 pkg.remove

Uninstall the specified software

[root@master ~]# salt 'node1' cmd.run 'rpm -qa |grep make'  # Check that make is installed on node1
node1:
    make-4.2.1-11.el8.x86_64
    
[root@master ~]# salt 'node1' pkg.remove make  # Uninstall make
node1:
    ----------
    make:
        ----------
        new:
        old:
            1:4.2.1-11.el8    
            
[root@master ~]# salt 'node1' cmd.run 'rpm -qa |grep make'  #  # Check that make on node1 has been uninstalled
node1:
ERROR: Minions returned with non-zero exit code  

# 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

# Download the WGet to the real machine and upload it to node1 	 http://mirror.centos.org/centos/7/os/x86_64/Packages/wget-1.14-18.el7_6.1.x86_64.rpm

# Then install the wget package on node1
[root@node1 ~]# yum -y install wget-1.14-18.el7_6.1.x86_64.rpm

[root@node1 ~]# rpm -qa |grep wget
wget-1.14-18.el7_6.1.x86_64

# Update wget on node1 on master

[root@master ~]# salt 'node1' pkg.upgrade wget
node1:
    ----------
    wget:
        ----------
        new:
            1.19.5-10.el8
        old:
            1.14-18.el7_6.1
            
[root@master ~]# salt 'node1' cmd.run 'rpm -qa |grep wget'
node1:
    wget-1.19.5-10.el8.x86_64            
2.3.11 pkg.version

View version

[root@master ]# salt '*' pkg.version httpd
node1:
    2.4.37-40.module_el8.5.0+852+0aafc63b
master:
    2.4.37-40.module_el8.5.0+852+0aafc63b

2.4 state of common modules of saltstack

2.4.1 state.show_highstate

Displays the advanced status of the current system

[root@master base]# pwd
/srv/salt/base
[root@master base]# cat web/apache/install.sls
apache-install:
  pkg.installed:
    - name: httpd
    
apache-service:
  service.running:
    - name: httpd
    - enable: True


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

Execute advanced status

[root@master base]# salt '*' state.highstate web.apache.install
node1:
----------
          ID: apache-install
    Function: pkg.installed
        Name: apache
      Result: None
     Comment: The following packages would be installed/updated: apache
     Started: 04:49:24.807275
    Duration: 773.049 ms
     Changes:   
              ----------
              installed:
                  ----------
                  apache:
                      ----------
                      new:
                          installed
                      old:
----------
          ID: apache-service
    Function: service.running
        Name: apache
      Result: None
     Comment: Service apache not present; if created in this state run, it would have been started
     Started: 04:49:25.590649
    Duration: 18.878 ms
     Changes:   

Summary for node1
------------
Succeeded: 2 (unchanged=2, changed=1)
Failed:    0
------------
Total states run:     2
Total run time: 791.927 ms
master:
----------
          ID: apache-install
    Function: pkg.installed
        Name: apache
      Result: None
     Comment: The following packages would be installed/updated: apache
     Started: 04:49:24.896324
    Duration: 739.798 ms
     Changes:   
              ----------
              installed:
                  ----------
                  apache:
                      ----------
                      new:
                          installed
                      old:
----------
          ID: apache-service
    Function: service.running
        Name: apache
      Result: None
     Comment: Service apache not present; if created in this state run, it would have been started
     Started: 04:49:25.647616
    Duration: 30.177 ms
     Changes:   

Summary for master
------------
Succeeded: 2 (unchanged=2, changed=1)
Failed:    0
------------
Total states run:     2
Total run time: 769.975 ms
2.4.3 state.show_state_usage

Displays the execution of advanced status in the current system

[root@master base]# salt '*' state.show_state_usage
master:
    ----------
    base:
        ----------
        count_all:        # Total execution status
            3
        count_unused:     # Number of unused
            2
        count_used:       # Number of used
            1
        unused:           # What is not used is
            - top
            - web.nginx.install
        used:             # Using
            - web.apache.install
node1:
    ----------
    base:
        ----------
        count_all:
            3
        count_unused:
            2
        count_used:
            1
        unused:
            - top
            - web.nginx.install
        used:
            - web.apache.install
2.4.4 state.show_top

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

[root@master ~]# salt '*' state.show_top
node1:
    ----------
    base:
        - web.apache.install
master:
    ----------
    base:
        - web.apache.install
2.4.5 state.top

Execute the specified top file instead of the default. state.highstat executes the default (top.sls)

[root@master ~]# tree /srv/salt/base/
/srv/salt/base/
|-- runinstall.sls(top file file)
`-- web
    |-- apache
    |   `-- install.sls
    `-- nginx
        `-- install.sls
 
 
 # top file contents
 [root@master ~]# cat /srv/salt/base/runinstall.sls 
base:
  'node1':
    - web.apache.install
    
# Status file
[root@master ~]# cat /srv/salt/base/web/apache/install.sls 
apache-install:
  pkg.installed:
    - name: httpd
    
apache-service:
  service.running:
    - name: httpd
    - enable: True
 
# Run the specified top file
[root@master ~]# salt 'node1' state.top  runinstall.sls
node1:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 04:58:46.494489
    Duration: 596.382 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: 04:58:47.092281
    Duration: 170.791 ms
     Changes:   
              ----------
              httpd:
                  True

Summary for node1
------------
Succeeded: 2 (changed=1)
Failed:    0
------------
Total states run:     2
Total run time: 767.173 ms
2.4.6 state.show_sls

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

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

2.5 common modules of saltstack

Syntax:

CLI Example:
salt '*' user.add name <uid> <gid> <groups> <home> <shell>
2.5.1 user.add

Create user

# Check whether there are Tom and Harry users in node1
[root@master ~]# salt 'node1' cmd.run 'id tom'
node1:
    id: 'tom': no such user
ERROR: Minions returned with non-zero exit code

[root@master ~]# salt 'node1' cmd.run 'id harry'
node1:
    id: 'harry': no such user
ERROR: Minions returned with non-zero exit code

# Create tom,harry user on node1
[root@master ~]# salt 'node1' user.add tom
node1:
    True
    
[root@master ~]# salt 'node1' user.add harry
node1:
    True
    
# see    
[root@master ~]# salt 'node1' cmd.run 'id tom'
node1:
    uid=1000(tom) gid=1000(tom) groups=1000(tom)
    
[root@master ~]# salt 'node1' cmd.run 'id harry'
node1:
    uid=1112(harry) gid=1112(harry) groups=1112(harry)    
2.5.2 user.chfullname

Change the user's full name (described by user information)

[root@master ~]# salt 'node1' cmd.run 'cat /etc/passwd | tail -1'
node1:
    tom:x:1000:1000::/home/tom:/bin/bash
    
# Change view
[root@master ~]# salt 'node1' user.chfullname tom "tom tur"
node1:
    True
    
[root@master ~]# salt 'node1' cmd.run 'cat /etc/passwd | tail -1'
node1:
    tom:x:1000:1000:tom tur:/home/tom:/bin/bash

2.5.3 user.chgid

To modify the user gid, you must have this group before modifying the group gid

[root@master ~]# salt 'node1' cmd.run 'id tom'
node1:
    uid=1000(tom) gid=1000(tom) groups=1000(tom)
    
# Modify view
[root@master ~]# salt 'node1' user.chgid 'tom' 999
node1:
    True
    
[root@master ~]# salt 'node1' cmd.run 'id tom'
node1:
    uid=1000(tom) gid=999(input) groups=999(input)
2.5.4 user.chuid

Modify user uid

# see
[root@master ~]# salt 'node1' cmd.run 'id tom'
node1:
    uid=1000(tom) gid=999(input) groups=999(input)
  
# Modify view
[root@master ~]# salt 'node1' user.chuid tom 1111
node1:
    True
    
[root@master ~]# salt 'node1' cmd.run 'id tom'
node1:
    uid=1111(tom) gid=999(input) groups=999(input)
2.5.5 user.chgroups

Add affiliate group

# see
[root@master ~]# salt 'node1' cmd.run 'id tom'
node1:
    uid=1111(tom) gid=999(input) groups=999(input)
    
# Modify view
[root@master ~]# salt 'node1' user.chgroups tom root,harry True
node1:
    True
[root@master ~]# salt 'node1' cmd.run 'id tom'
node1:
    uid=1111(tom) gid=999(input) groups=999(input),1112(harry),0(root)
2.5.6 user.info

Return user details

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

Return all user information

[root@master ~]# salt 'node1' user.getent
node1:
    |_
      ----------
      fullname:
          root
      gid:
          0
      groups:
          - root
      home:
          /root
      homephone:
      name:
          root
      other:
      ..............
2.5.8 user.rename

Modify user name

# see
[root@master ~]# salt 'node1' cmd.run 'id harry'
node1:
    uid=1112(harry) gid=1112(harry) groups=1112(harry)
    
# Modify user name view
[root@master ~]# salt 'node1' user.rename harry alice
node1:
    False

[root@master ~]# salt 'node1' cmd.run 'id alice'
node1:
    uid=1112(alice) gid=1112(harry) groups=1112(harry)
2.5.9 user.list_users

Returns a list of all users

[root@master ~]# salt 'node1' user.list_users
node1:
    - adm
    - alice
    - apache
    - bin
    - chrony
    - daemon
    - dbus
    - ftp
    - games
    - halt
    - lp
    - mail
    - nginx
    - nobody
    - operator
    - polkitd
    - rngd
    - root
    - shutdown
    - sshd
    - sssd
    - sync
    - systemd-coredump
    - systemd-resolve
    - tom
    - tss
    - unbound
2.5.10 user.list_groups

Returns the list of groups to which the specified user belongs

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

Modify the user's home directory

#  see
[root@master ~]# salt 'node1' cmd.run 'cat /etc/passwd |grep tom'
node1:
    tom:x:1111:999:tom tur:/home/tom(Home directory location):/bin/bash
    
# Create a new home directory    
[root@master ~]# salt 'node1' cmd.run 'mkdir /home/users'
node1:
[root@master ~]# salt 'node1' cmd.run 'ls /home'
node1:
    harry
    tom
    users    
    
# Modify and view
[root@master ~]# salt 'node1' user.chhome tom /home/users True
node1:
    True
ERROR: Minions returned with non-zero exit code

[root@master ~]# salt 'node1' cmd.run 'cat /etc/passwd |grep tom'
node1:
    tom:x:1111:999:tom tur:/home/users(New home directory location):/bin/bash

2.5.12 passwd

Set password for user

[root@master ~]# salt 'node1' cmd.run 'id alice'
node1:
    uid=1112(alice) gid=1112(harry) groups=1112(harry)
    
[root@master ~]# salt 'node1' cmd.run 'echo "redhat" |passwd --stdin alice'
node1:
    Changing password for user alice.
    passwd: all authentication tokens updated successfully.    
2.5.13 user.delete

delete user

# View before deleting
[root@master ~]# salt 'node1' cmd.run 'id tom'
node1:
    uid=1111(tom) gid=999(input) groups=999(input),1112(harry),0(root)

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

# View after deletion 
[root@master ~]# salt 'node1' cmd.run 'id tom'
node1:
    id: 'tom': no such user
ERROR: Minions returned with non-zero exit code

2.6 salt CP of saltstack

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

# Copy a single file to the specified directory of the target host
[root@master ~]# cat /root/test.sh 
#!/bin/bish

echo "hello world"

# Check whether there is a test.sh file in the / opt directory on node1
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt'
node1:
    total 0
    
# Copy / root/test.sh on the master to the / opt directory on node1
[root@master ~]# salt-cp 'node1' /root/test.sh /opt/
node1:
    ----------
    /opt/test.sh:
        True
        
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt'
node1:
    total 4
    -rw-r--r-- 1 root root 32 Nov  4 00:51 test.sh
    
# Execute the script file just copied on node1
[root@master ~]# salt 'node1' cmd.run 'bash /opt/test.sh'
node1:
    hello world
    
    
# Copy multiple files to the / opt / directory of the target host
[root@master ~]# salt-cp 'node1' /root/anaconda-ks.cfg /etc/passwd /opt/
node1:
    ----------
    /opt/anaconda-ks.cfg:
        True
    /opt/passwd:
        True
# see
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
    total 12
    -rw-r--r-- 1 root root 1023 Nov  4 00:54 anaconda-ks.cfg
    -rw-r--r-- 1 root root 1218 Nov  4 00:54 passwd
    -rw-r--r-- 1 root root   32 Nov  4 00:51 test.sh

2.7 file of common modules of saltstack

2.7.1 file.access

Check whether the specified path (file) exists, unable to check the directory

# Check whether the specified file under the / opt / directory on node1 exists. If it exists, return True and if not, return False
[root@master ~]# salt 'node1' file.access /opt/test.sh f
node1:
    True
 
[root@master ~]# salt 'node1' file.access /opt/123 f
node1:
    False 
 
 
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/test.sh'
node1:
    -rw-r--r-- 1 root root 32 Nov  4 00:51 /opt/test.sh

Check the permission information of the specified file (no matter where it is, it returns true as long as there is corresponding rwx permission)

[root@master ~]# salt 'node1' file.access /opt/test.sh r  # Do you have read permission
node1:
    True
[root@master ~]# salt 'node1' file.access /opt/test.sh w  # Do you have write permission
node1:
    True
[root@master ~]# salt 'node1' file.access /opt/test.sh x # Do you have execution permission
node1:
    False
    
# see    
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/test.sh'
node1:
    -rw-r--r-- 1 root root 32 Nov  4 00:51 /opt/test.sh    
2.7.2 file.append

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

# see
[root@master ~]# salt 'node1' cmd.run 'cat /opt/test.sh'
node1:
    #!/bin/bish
    
    echo "hello world"
    
# Append view (multiple quotes)
[root@master ~]# salt 'node1' file.append /opt/test.sh "xixi" "hehe" 
node1:
    Wrote 2 lines to "/opt/test.sh"
    
[root@master ~]# salt 'node1' cmd.run 'cat /opt/test.sh'node1:
    #!/bin/bish
    
    echo "hello world"
    xixi   # Append to multiple lines
    hehe

# Append view (one quotation mark)
[root@master ~]# salt 'node1' file.append /opt/test.sh "jjyy lo"
node1:
    Wrote 1 lines to "/opt/test.sh"
    
[root@master ~]# salt 'node1' cmd.run 'cat /opt/test.sh'
node1:
    #!/bin/bish
    
    echo "hello world"
    xixi
    hehe
    jjyy lo   # Append to one line
    
# Append view (without quotation marks)
[root@master ~]# salt 'node1' file.append /opt/test.sh look o oooopqqp
node1:
    Wrote 3 lines to "/opt/test.sh"
    
[root@master ~]# salt 'node1' cmd.run 'cat /opt/test.sh'node1:
    #!/bin/bish
    
    echo "hello world"
    xixi
    hehe
    jjyy lo
    look   # Space is one line
    o
    oooopqqp
2.7.3 file.basename

Gets the base name of the specified path

[root@master ~]# salt 'node1' file.basename /etc/httpd/conf/httpd.conf
node1:
    httpd.conf  # Take the last one
2.7.4 file.dirname

Gets the directory name of the specified path

[root@master ~]# salt 'node1' file.dirname /etc/httpd/conf/httpd.conf
node1:
    /etc/httpd/conf  # Take all before the last one
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 /opt/test.sh'node1:
    3126f013f12a40de8ff032a739eff170  /opt/test.sh
    
# Compare the md5 value of / opt/test.sh with the file. If it matches, it returns True. Otherwise, it returns False    
[root@master ~]# salt 'node1' file.check_hash /opt/test.sh 3126f013f12a40de8ff032a739eff170
node1:
    True    
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 file properties
[root@master ~]# salt 'node1' cmd.run "lsattr -d /opt/test.sh"
node1:
    -------------------- /opt/test.sh
    
    
#  Adds attributes to the specified file
[root@master ~]# Salt 'node1' file.chattr/opt/test.sh operator = add attributes = ai
node1:
    True
    
[root@master ~]# salt 'node1' cmd.run "lsattr -d /opt/test.sh"node1:
    ----ia-------------- /opt/test.sh
    
# Attempt to overwrite the contents of the / opt/test.sh file
[root@master ~]# salt 'node1' cmd.run 'echo "123" > /opt/test.sh'
node1:
    /bin/sh: /opt/test.sh: Operation not permitted(operation not permitted)
ERROR: Minions returned with non-zero exit code

# Try deleting the / opt/test.sh file
[root@master ~]# salt 'node1' cmd.run 'rm -f /opt/test.sh'
node1:
    rm: cannot remove '/opt/test.sh': Operation not permitted(operation not permitted)
ERROR: Minions returned with non-zero exit code

Add properties to the directory

# View the properties of the directory    
[root@master ~]# salt 'node1' cmd.run "lsattr -d /opt/"
node1:
    -------------------- /opt/

# Add attribute
[root@master ~]# Salt 'node1' file.chattr / opt / operator = add attributes = ai
node1:
    True
    
[root@master ~]# salt 'node1' cmd.run 'lsattr -d  /opt/'node1:
    ----ia-------------- /opt/
    
# Attempt to delete directory / opt/
[root@master ~]# salt 'node1' cmd.run 'rm -r /opt/'
node1:
    rm: cannot remove '/opt/anaconda-ks.cfg': Operation not permitted
    rm: cannot remove '/opt/passwd': Operation not permitted
    rm: cannot remove '/opt/test.sh': Operation not permitted
ERROR: Minions returned with non-zero exit code

Removes attributes from the specified file

[root@master ~]# salt 'node1' cmd.run 'lsattr /opt/test.sh'
node1:
    ----ia-------------- /opt/test.sh
    
[root@master ~]# Salt 'node1' file. Chattr / opt / test. Sh operator = Remove attributes=ai
node1:
    True

[root@master ~]# salt 'node1' cmd.run 'lsattr /opt/test.sh'node1:
    -------------------- /opt/test.sh

Removes attributes from the specified directory

[root@master ~]# Salt 'node1' file. Chattr / opt / operator = Remove attributes=ai
node1:
    True
[root@master ~]# salt 'node1' cmd.run 'lsattr -d /opt/'node1:
    -------------------- /opt/

Operate under the current directory

[root@master ~]# lsattr anaconda-ks.cfg 
-----a-------------- anaconda-ks.cfg

[root@master ~]# chattr +i anaconda-ks.cfg 
[root@master ~]# lsattr anaconda-ks.cfg 
----ia-------------- anaconda-ks.cfg

[root@master ~]# chattr -i anaconda-ks.cfg 
[root@master ~]# lsattr anaconda-ks.cfg 
-----a-------------- anaconda-ks.cfg
2.7.7 file.chown

Set the primary group of the specified file. Both must be specified

# see
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/test.sh'
node1:
    -rw-r--r-- 1 root root 66 Nov  4 01:15 /opt/test.sh
    
# Set view
[root@master ~]# Salt 'node1' file.chown/opt/test.sh Alice (user) Harry (Group)
node1:
    None
    
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/test.sh'
node1:
    -rw-r--r-- 1 alice(user) harry(group) 66 Nov  4 01:15 /opt/test.sh
    
# reduction
[root@master ~]# salt 'node1' file.chown /opt/test.sh root root
node1:
    None
    
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/test.sh'
node1:
    -rw-r--r-- 1 root root 66 Nov  4 01:15 /opt/test.sh
2.7.8 file.copy

Copy files directly on remote host

# see
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 4
    -rw-------. 1 root root 1023 Sep 24 04:52 anaconda-ks.cfg
 
 
# Copy the / opt/test.sh file to the / root directory, also known as test.sh
[root@master ~]# salt 'node1' file.copy /opt/test.sh /root/test.sh
node1:
    True

[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 8
    -rw-------. 1 root root 1023 Sep 24 04:52 anaconda-ks.cfg
    -rw-r--r--  1 root root   66 Nov  4 01:55 test.sh

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

# see
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 8
    -rw-------. 1 root root 1023 Sep 24 04:52 anaconda-ks.cfg
    -rw-r--r--  1 root root   66 Nov  4 01:55 test.sh
    
# Copy the / usr/src directory to the / root directory
[root@master ~]# salt 'node1' file.copy /usr/src/ /root/123 recurse=True
node1:
    True
    
# see
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/'
node1:
    total 8
    drwxr-xr-x  4 root root   34 Nov  4 02:09 123
    -rw-------. 1 root root 1023 Nov  4 01:58 anaconda-ks.cfg
    -rw-r--r--  1 root root   66 Nov  4 01:58 test.sh
   
 # View the contents of / root/123  
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/123'
node1:
    total 0
    drwxr-xr-x 2 root root 6 Nov  4 02:09 debug
    drwxr-xr-x 2 root root 6 Nov  4 02:09 kernels
   
   
# Copy the / etc/pam.d directory to / root and call 123. Check / root/123 and find that the contents of the previous 123 directory have been overwritten   
[root@master ~]# salt 'node1' file.copy /etc/pam.d/ /root/123 recurse=True
node1:
    True
    
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/123'
node1:
    total 96
    -rw-r--r-- 1 root root 232 Nov  4 02:12 config-util
    -rw-r--r-- 1 root root 328 Nov  4 02:12 crond
    drwxr-xr-x 2 root root   6 Nov  4 02:09 debug
    -rw-r--r-- 1 root root 701 Nov  4 02:12 fingerprint-auth   
    ...............................

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

# View the contents of the / root/123 directory
[root@master ~]# salt 'node1' cmd.run 'ls /root/123'
node1:
    gfgdfgdf
    gg[gfg
    gjkfgd
    xixi
    
# View the contents of the / opt/123 directory
[root@master ~]# salt 'node1' cmd.run 'ls /opt/123'
node1:
    gg[gfg
    gjkfgd
   
# Copy the / opt/123 directory to the / root/123 directory and view it
[root@master ~]# salt 'node1' file.copy /opt/123 /root/123 recurse=True remove_existing=True
node1:
    True
    
[root@master ~]# salt 'node1' cmd.run 'ls /root/123'node1:
    gg[gfg
    gjkfgd
2.7.9 file.directory_exists

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

[root@master ~]# salt 'node1' file.directory_exists /root/123
node1:
    True

[root@master ~]# salt 'node1' cmd.run 'ls -l /root/'
node1:
    total 8
    drwxr-xr-x  3 root root   34 Nov  4 02:19 123
    -rw-------. 1 root root 1023 Nov  4 01:58 anaconda-ks.cfg
    -rw-r--r--  1 root root   66 Nov  4 01:58 test.sh

2.7.10 file.diskusage

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

[root@master ~]# salt 'node1' file.diskusage /root/
node1:
    16072
    
[root@master ~]# salt 'node1' cmd.run 'du -sb /root'
node1:
    16330       /root
    
[root@master ~]# salt 'node1' cmd.run 'du -sh /root'
node1:
    48K /root    
2.7.11 file.file_exists

Determine whether the specified file exists

# Determine whether the / opt/test.sh file exists
[root@master ~]# salt 'node1' file.file_exists  /opt/test.sh
node1:
    False

# The view does not exist
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
    total 4
    drwxr-xr-x 3 root root   34 Nov  4 02:19 123
    -rw-r--r-- 1 root root 1023 Nov  4 00:54 anaconda-ks.cfg
2.7.12 file.find

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

Options include matching criteria:

name    = path-glob                 # Case sensitive
iname   = path-glob                 # Case insensitive
regex   = path-regex                # Case sensitive
iregex  = path-regex                # Case insensitive
type    = file-types                # Matches any of the listed types
user    = users                     # Match any listed users
group   = groups                    # Match any listed groups
size    = [+-]number[size-unit]     # Default unit = bytes
mtime   = interval                  # Modify from date
grep    = regex                     # Search file contents

and/oractions :

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

and/or depth criteria:

maxdepth = maximum depth to transverse in path
mindepth = minimum depth to transverse before checking files or directories
 
# Maximum depth = maximum depth of lateral path
# Minimum lateral depth before checking a file or directory

The default operation is print=path

path-glob:

*                = Matches zero or more characters
?                = Match any character
[abc]            = matching a, b or c
[!abc] or [^abc] = Matching Division a, b, c Anything other than
[x-y]            = Match character x reach y
[!x-y] or [^x-y] = Match except characters x reach y Any character other than
{a,b,c}          = matching a or b or c

Path regex: Python regular expression pattern for matching path names

File types: a string consisting of one or more of the following characters:

a: All file types
b: Block device
c: Character device
d: catalogue
p: FIFO(name pipes)
f: Ordinary file
l: Symbolic link
s: socket

users: a list of user names and / or UIDs separated by spaces and / or commas

groups: a list of group names and / or gids separated by spaces and / or commas

size-unit:

b: bytes byte
k: kilobytes  kb
m: megabytes  mb
g: gigabytes  gb
t: terabytes  tb

interval:

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

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

Print opts: one or more of the following lists separated by commas and / or spaces:

group: :Group name
md5:   Of file contents md5 abstract
mode:  file right(In integer form)
mtime: Last modification time(as time_t)
name:  file basename
path:  Absolute path to the file
size:  File size in bytes
type:  file type
user:  user name

Example:

[root@master ~]# salt 'node1' file.find / type=f name=\*.bak size=+10m
[root@master ~]# Salt 'node1' file.find / var mtime = + 30d (the time when the file was modified 30 days ago) size=+10m print=path,size,mtime
[root@master ~]# Salt 'node1' file.find / var / log name = \ *. [0-9] mtime = + 30d (the time when the file was modified 30 days ago) size=+10m delete


# Find files under the root that end in. bak
[root@master ~]# salt 'node1' file.find / type=f name=\*.bak
node1:
    - /etc/nsswitch.conf.bak

# Print users larger than 10M in the directory / var, size and modification time
[root@master ~]# salt 'node1' file.find /var size=+10m print=user,size,mtime
node1:
    |_
      - root
      - 12904035
      - 1635745024
    |_
      - root
      - 13311037
      - 1635745021
    |_
      - root
      - 61124608
      - 1635928208
    |_
      - sssd
      - 11567160
      - 1636002001
      
# Delete the files with *. [0-9].log in the / var/log / directory      
[root@master ~]# salt 'node1' file.find /var/log name=\*.[0-9].log delete
node1:
    - /var/log/fdf.1.log
    - /var/log/vmware-network.1.log
    - /var/log/vmware-network.2.log
    - /var/log/vmware-network.3.log
    - /var/log/vmware-network.4.log
    - /var/log/vmware-network.5.log
    - /var/log/vmware-network.6.log
    - /var/log/vmware-network.7.log
    - /var/log/vmware-network.8.log
    - /var/log/vmware-network.9.log      
2.7.13 file.get_gid

Gets the gid of the specified file

[root@master ~]# salt 'node1' file.chown /root/test.sh alice harry
node1:
    None
    
[root@master ~]# salt 'node1' cmd.run 'id alice'
node1:
    uid=1112(alice) gid=1112(harry) groups=1112(harry)
    
# obtain
[root@master ~]# salt 'node1' file.get_gid /root/test.sh
node1:
    1112
2.7.14 file.get_group

Gets the group name of the specified file

[root@master ~]# salt 'node1' file.get_group /root/test.sh
node1:
    harry
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' file.get_hash /root/test.sh
node1:
    0acf7a6315c70ed00f6c7db6afb65748471b9fb267088f5eb539037a129ea711

[root@master ~]# salt 'node1' cmd.run 'sha256sum /root/test.sh'
node1:
    0acf7a6315c70ed00f6c7db6afb65748471b9fb267088f5eb539037a129ea711  /root/test.sh
    
2.7.16 file.get_mode

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

# The permissions of the / opt/anaconda-ks.cfg file are displayed numerically
[root@master ~]# salt 'node1' file.get_mode /opt/anaconda-ks.cfg
node1:
    0644
    
# Displays the permissions of the / var/log directory in numbers   
[root@master ~]# salt 'node1' file.get_mode /var/log
node1:
    0755    
2.7.17 file.get_selinux_context

Gets the SELINUX context information of the specified file

# Gets the context of the / varlog directory
[root@master ~]# salt 'node1' file.get_selinux_context /var/log
node1:
    system_u:object_r:var_log_t:s0
  
# Gets the context of the / root / directory  
[root@master ~]# salt 'node1' file.get_selinux_context /root/
node1:
    system_u:object_r:admin_home_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
# View default signature (sha256)
[root@master ~]# salt 'node1' file.get_sum /etc/httpd/conf/httpd.conf
node1:
    6ffbc88668a8cd9f5576bc0e9a686b88a2c23eb89194ecceb5dfd11c81120966
    
# Specify view signature MD5
[root@master ~]# salt 'node1' file.get_sum /etc/httpd/conf/httpd.conf md5
node1:
    d659bccae84073a2fa24fa8c7cbc9774
    
# Specifies the view signature
[root@master ~]# salt 'node1' file.get_sum /etc/httpd/conf/httpd.conf sha512
node1:       acc464ad461721927b36ee377067ef040bfa5b521385fb7ab87a5492f71de93c640cf614b8201fc4c09c342a2f231c7acaee686e6f64b662ea306a277f183912

# Specify view signature sha224
[root@master ~]# salt 'node1' file.get_sum /etc/httpd/conf/httpd.conf sha224
node1:
    ac823792c431691a4dd809693afbce98e838c8b222367ab4f8e4978e
2.7.19 file.get_uid and file.get_user

Gets the uid or user name of the specified file

# Get uid of / root/test.sh
[root@master ~]# salt 'node1' file.get_uid /root/test.sh
node1:
    1112
   
# Get the user of / root/test.sh 
[root@master ~]# salt 'node1' file.get_user /root/test.sh
node1:
    alice
   
# Get gid of / root/test.sh   
[root@master ~]# salt 'node1' file.get_gid /root/test.sh
node1:
    1112
  
# Get the group of / root/test.sh 
[root@master ~]# salt 'node1' file.get_group /root/test.sh
node1:
    harry
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 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 input
node1:
    999
2.7.22 file.user_to_uid

Converts the specified user to uid display

[root@master ~]# salt 'node1' file.user_to_uid alice
node1:
    1112
2.7.23 file.uid_to_user

Converts the specified uid to user display

[root@master ~]# salt 'node1' file.uid_to_user 0
node1:
    root
    
[root@master ~]# salt 'node1' file.uid_to_user 998
node1:
    polkitd
2.7.24 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 /etc/passwd "^root"  # Look in the / etc/passwd file for files that start with root
node1:
    ----------
    pid:
        320915
    retcode:
        0
    stderr:
    stdout:
        root:x:0:0:root:/root:/bin/bash
    
# Finds ipaddr in the specified file, ignoring case
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i
node1:
    ----------
    pid:
        323676
    retcode:
        0
    stderr:
    stdout:
        IPADDR=192.168.220.10
        
# Find ipaddr in the specified file, ignore case, more detailed display        
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -il
node1:
    ----------
    pid:
        324061
    retcode:
        0
    stderr:
    stdout:
        /etc/sysconfig/network-scripts/ifcfg-ens33  
        
  
 # Finds ipaddr in the specified file, ignores case, and displays the last two lines
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i -A2
node1:
    ----------
    pid:
        326627
    retcode:
        0
    stderr:
    stdout:
        IPADDR=192.168.220.10
        GATEWAY=192.168.220.2
        NETMASK=255.255.255.0
 
 # Finds ipaddr in the specified file, ignores case, and displays the first two lines
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i -B2
node1:
    ----------
    pid:
        327128
    retcode:
        0
    stderr:
    stdout:
        DEVICE=ens33
        ONBOOT=yes
        IPADDR=192.168.220.10
        
# Finds ipaddr in the specified file, ignores case, and displays the first and second lines  
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i -C2
node1:
    ----------
    pid:
        327683
    retcode:
        0
    stderr:
    stdout:
        DEVICE=ens33
        ONBOOT=yes
        IPADDR=192.168.220.10
        GATEWAY=192.168.220.2
        NETMASK=255.255.255.0    
2.7.25 file.is_blkdev

Determines whether the specified file is a block device file

# Check whether / dev/sr0 is a block device file
[root@master ~]# salt 'node1' file.is_blkdev /dev/sr0
node1:
    True

# see  
[root@master ~]# salt 'node1' cmd.run 'ls -l /dev/sr0'
node1:
    brw-rw---- 1 root cdrom 11, 0 Nov  3 21:27 /dev/sr0
    
2.7.26 file.lsattr

Check and display the attribute information of the specified file

# View properties
[root@master ~]# salt 'node1' cmd.run 'lsattr /etc/passwd'
node1:
    -------------------- /etc/passwd

# Add attribute a
[root@master ~]# salt 'node1'  cmd.run 'chattr +a /etc/passwd'
node1:

# View add results
[root@master ~]# salt 'node1' cmd.run 'lsattr /etc/passwd'
node1:
    -----a-------------- /etc/passwd
    
# Delete added attribute a
[root@master ~]# salt 'node1'  cmd.run 'chattr -a /etc/passwd'
node1:

[root@master ~]# salt 'node1' cmd.run 'lsattr /etc/passwd'
node1:
    -------------------- /etc/passwd    
2.7.27 file.mkdir

Create directory and set owner, group and permission

# Create directory / opt/hehe. The owner is root. The owner group is alice. The permission is 755
[root@master ~]# salt 'node1' file.mkdir /opt/hehe root alice 755
node1:
    True
    
# View the created / opt/hehe
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
    total 4
    drwxr-xr-x 3 root root   34 Nov  4 02:19 123
    -rw-r--r-- 1 root root 1023 Nov  4 00:54 anaconda-ks.cfg
    drwxr-xr-x 2 root root    6 Nov  4 03:48 hehe       
2.7.28 file.move

Move or rename

# Do not move location rename

[root@master ~]# salt 'node1' cmd.run 'ls /root'
node1:
    123
    anaconda-ks.cfg
    test.sh

# Move / root/123 to / root/1234
[root@master ~]# salt 'node1' file.move /root/123 /root/1234
node1:
    ----------
    comment:
        '/root/123' moved to '/root/1234'
    result:
        True
        
[root@master ~]# salt 'node1' cmd.run 'ls /root'
node1:
    1234
    anaconda-ks.cfg
    test.sh
    
# Move to another location and rename  
[root@master ~]# salt 'node1' cmd.run 'ls /opt'
node1:
    anaconda-ks.cfg
    hehe
    
# Move / root/1234 to / opt / and call jyy    
[root@master ~]# salt 'node1' file.move /root/1234 /opt/jjyy
node1:
    ----------
    comment:
        '/root/1234' moved to '/opt/jjyy'
    result:
        True
[root@master ~]# salt 'node1' cmd.run 'ls /opt'
node1:
    anaconda-ks.cfg
    hehe
    jjyy    
2.7.29 file.prepend

Insert the text at the beginning of the specified file and execute it again and again

# View the text content of / root/test.sh
[root@master ~]# salt 'node1' cmd.run 'cat /root/test.sh'
node1:   
    echo "hello world"
    xixi
    hehe
    
# Add content view before / root/tests.h /
[root@master ~]# salt 'node1' file.prepend /root/test.sh xixi hehe
node1:
    Prepended 2 lines to "/root/test.sh"

[root@master ~]# salt 'node1' cmd.run 'cat /root/test.sh'
node1:
    xixi
    hehe    
    echo "hello world"
    xixi
    hehe
2.7.30 file.sed

Modify the content of the text file and modify it globally

# View, change xixi to jjyy
[root@master ~]# salt 'node1' cmd.run 'cat /root/test.sh'
node1:
    xixi
    hehe    
    echo "hello world"
    xixi
    hehe
    
# Execute and view    
[root@master ~]# salt 'node1' file.sed /root/test.sh xixi jjyy
node1:
    ----------
    pid:
        31777
    retcode:
        0
    stderr:
    stdout:
    
[root@master ~]# salt 'node1' cmd.run 'cat /root/test.sh'
node1:
    jjyy
    hehe
    echo "hello world"
    jjyy
    hehe    
    
# Specify modification
[root@master ~]# salt 'node1' file.sed /root/test.sh jjyy xixi flags=1
node1:
    ----------
    pid:
        42402
    retcode:
        0
    stderr:
    stdout:

# see    
[root@master ~]# salt 'node1' cmd.run 'cat /root/test.sh'
node1:
    xixi
    hehe
    echo "hello world"
    xixi
    hehe    
2.7.31 file.read

Read the contents of the file, just like the cat command

[root@master ~]# salt 'node1' file.read '/root/test.sh'
node1:
    xixi
    hehe
    echo "hello world"
    xixi
    hehe
2.7.32 file.readdir

Lists all files or directories in the specified directory, including hidden files (the same as ls -a)

[root@master ~]# salt 'node1' file.readdir '/root/'
node1:
    - .
    - ..
    - .bash_logout
    - .bash_profile
    - .bashrc
    - .cshrc
    - .tcshrc
    - anaconda-ks.cfg
    - .config
    - .bash_history
    - test.sh.bak
    - .viminfo
    - test.sh
2.7.33 file.remove

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

# Create recursive directory view
[root@master ~]# salt 'node1' cmd.run 'mkdir -p a/b/c/d/e/f'
node1:

[root@master ~]# salt 'node1' cmd.run 'tree a'
node1:
    a
    `-- b
        `-- c
            `-- d
                `-- e
                    `-- f
    
    5 directories, 0 files
   
# Execute delete
[root@master ~]# salt 'node1' file.remove /root/a
node1:
    True

[root@master ~]# salt 'node1' cmd.run 'ls /root'
node1:
    anaconda-ks.cfg
    test.sh
    
# Delete after creating file
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 12
    -rw-------. 1 root  root  1023 Nov  4 01:58 anaconda-ks.cfg
    -rw-r--r--  1 root  root     0 Nov  5 04:15 hello
    -rw-r--r--  1 alice harry   39 Nov  5 04:07 test.sh
    
[root@master ~]# salt 'node1' file.remove /root/hello
node1:
    True
    
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 12
    -rw-------. 1 root  root  1023 Nov  4 01:58 anaconda-ks.cfg
    -rw-r--r--  1 alice harry   39 Nov  5 04:07 test.sh   
2.7.34 rename

Rename a file or directory

# see
[root@master ~]# salt 'node1' cmd.run 'ls /root'
node1:
    anaconda-ks.cfg
    test.sh
    
# Rename view
[root@master ~]# salt 'node1' file.rename /root/test.sh /root/hehe.sh
node1:
    True
    
[root@master ~]# salt 'node1' cmd.run 'ls /root'
node1:
    anaconda-ks.cfg
    hehe.sh

2.7.35 file.set_mode

Set permissions for the specified file

# see
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 8
    -rw-------. 1 root  root  1023 Nov  4 01:58 anaconda-ks.cfg
    -rw-r--r--  1 alice harry   39 Nov  5 04:07 hehe.sh
    
# Set permissions
[root@master ~]# salt 'node1' file.set_mode /root/hehe.sh 755
node1:
    0755
    
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 8
    -rw-------. 1 root  root  1023 Nov  4 01:58 anaconda-ks.cfg
    -rwxr-xr-x  1 alice harry   39 Nov  5 04:07 hehe.sh
2.7.36 file.symlink

Creates a soft link to the specified file

# see
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 8
    -rw-------. 1 root  root  1023 Nov  4 01:58 anaconda-ks.cfg
    -rwxr-xr-x  1 alice harry   39 Nov  5 04:07 hehe.sh
    
# Create a soft connection view for hehe.sh
[root@master ~]# salt 'node1' file.symlink /root/hehe.sh /root/hello
node1:
    True
    
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 8
    -rw-------. 1 root  root  1023 Nov  4 01:58 anaconda-ks.cfg
    -rwxr-xr-x  1 alice harry   39 Nov  5 04:07 hehe.sh
    lrwxrwxrwx  1 root  root    13 Nov  5 04:24 hello -> /root/hehe.sh
2.7.37 file.touch

Create an empty file or update the timestamp

# View and create a file under / root
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 8
    -rw-------. 1 root  root  1023 Nov  4 01:58 anaconda-ks.cfg
    -rwxr-xr-x  1 alice harry   39 Nov  5 04:07 hehe.sh
    lrwxrwxrwx  1 root  root    13 Nov  5 04:24 hello -> /root/hehe.sh
 
# Create view 
 [root@master ~]# salt 'node1' file.touch /root/word
node1:
    True
    
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 8
    -rw-------. 1 root  root  1023 Nov  4 01:58 anaconda-ks.cfg
    -rwxr-xr-x  1 alice harry   39 Nov  5 04:07 hehe.sh
    lrwxrwxrwx  1 root  root    13 Nov  5 04:24 hello -> /root/hehe.sh
    -rw-r--r--  1 root  root     0 Nov  5 04:27 word   
    
 # Update timestamp for an existing file
 # View time
 [root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 8
    -rw-------. 1 root  root  1023 Nov  4 01:58 anaconda-ks.cfg
    -rwxr-xr-x  1 alice harry   39 Nov  5 04:07(time) hehe.sh
    lrwxrwxrwx  1 root  root    13 Nov  5 04:24 hello -> /root/hehe.sh
    -rw-r--r--  1 root  root     0 Nov  5 04:27 word
  
# Update timestamp / root/hehe.sh  
[root@master ~]# salt 'node1' file.touch /root/hehe.sh
node1:
    True
 
# see 
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 8
    -rw-------. 1 root  root  1023 Nov  4 01:58 anaconda-ks.cfg
    -rwxr-xr-x  1 alice harry   39 Nov  5 04:29(Time update) hehe.sh
    lrwxrwxrwx  1 root  root    13 Nov  5 04:24 hello -> /root/hehe.sh
    -rw-r--r--  1 root  root     0 Nov  5 04:27 word
2.7.38 file.write

Overwrite and write the specified content to a specified file. If the file does not exist, create a file and write it

# see
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 8
    -rw-------. 1 root  root  1023 Nov  4 01:58 anaconda-ks.cfg
    -rwxr-xr-x  1 alice harry   39 Nov  5 04:29 hehe.sh
    lrwxrwxrwx  1 root  root    13 Nov  5 04:24 hello -> /root/hehe.sh
    -rw-r--r--  1 root  root     0 Nov  5 04:27 word
    
# Create and write
[root@master ~]# salt 'node1' file.write /root/aaa "hehe" "hhyy" "fdf fdf"
node1:
    Wrote 3 lines to "/root/aaa"
    
# Check whether the file was created successfully 
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 12
    -rw-r--r--  1 root  root    18 Nov  5 04:31 aaa
    -rw-------. 1 root  root  1023 Nov  4 01:58 anaconda-ks.cfg
    -rwxr-xr-x  1 alice harry   39 Nov  5 04:29 hehe.sh
    lrwxrwxrwx  1 root  root    13 Nov  5 04:24 hello -> /root/hehe.sh
    -rw-r--r--  1 root  root     0 Nov  5 04:27 word

# View the contents of the created file     
[root@master ~]# salt 'node1' file.read  '/root/aaa'
node1:
    hehe
    hhyy
    fdf fdf
    
 # Overwrite and write the newly created / root/aaa file
 [root@master ~]# salt 'node1' file.write /root/aaa "bagayalu"
node1:
    Wrote 1 lines to "/root/aaa"
    
# see    
[root@master ~]# salt 'node1' file.read  '/root/aaa'
node1:
    bagayalu

Topics: Linux Operation & Maintenance