当前位置:网站首页>Common modules of saltstack

Common modules of saltstack

2022-06-11 06:54:00 Shit in your schoolbag

saltstack Common modules of

List of articles

1. SaltStack Module introduction

Module It's for daily use SaltStack The most contacted component , It is used to manage object operations , This is also SaltStack adopt Push The way to manage , For example, we simply execute commands every day 、 Check the package installation 、 Check the service operation through SaltStack Module To achieve .

When the installation is good Master and Minion After package , Many... Will be installed on the system Module, You can view all supported through the following command Module list :

// View a list of all modules 
[[email protected] ~]# salt 'node1' sys.list_modules 
node1:
    - acl
    - aliases
    - alternatives
    - apache
    - archive
    - artifactory
    - baredoc
    - beacons
    - bigip
    - btrfs
    - buildout
    - chroot
    - cloud
    - cmd
    - composer
    - config
    - consul
........... Omit here 
    
    
// View all of the specified modules function function 
[[email protected] ~]# 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
[[email protected] ~]#


// View the usage document of the specified module 
[[email protected] ~]# 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"}'
................ Omit here 


//SaltStack By default, it also supports multiple execution at a time Module,Module Separated by commas , The default parameters are also separated by commas , It also supports specifying the parameter separator [email protected] that will do 
[[email protected] ~]# salt 'node1' test.echo,cmd.run,service.status hello,hostname,salt-minion
node1:
    ----------
    cmd.run:
        node1
    service.status:
        True
    test.echo:
        hello
[[email protected] ~]# 

2. SaltStack Common modules

2.1 SaltStack One of the common modules network

2.1.1 network.active_tcp

Returns all active tcp Connect

[[email protected] ~]# 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
[[email protected] ~]#

2.1.2 network.calc_net

adopt IP And subnet mask to calculate the network segment

[[email protected] ~]# salt 'node1' network.calc_net 192.168.10.201 255.255.255.0
node1:
    192.168.10.0/24
[[email protected] ~]# salt 'node1' network.calc_net 192.168.10.201 255.255.255.240
node1:
    192.168.10.192/28
[[email protected] ~]#

2.1.3 network.connect

test minion Whether the network to a server is connected

You must specify a port

[[email protected] ~]# 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
[[email protected] ~]# salt 'node1' network.connect baidu.com 443
node1:
    ----------
    comment:
        Successfully connected to baidu.com (220.181.38.251) on tcp port 443
    result:
        True
[[email protected] ~]# 

2.1.4 network.default_route

View the default route

[[email protected] ~]# 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:
[[email protected] ~]#

2.1.5 network.get_fqdn

Check the host's fqdn( Fully qualified domain name )

[[email protected] ~]# salt 'node1' network.get_fqdn 
node1:
    node1
[[email protected] ~]#

2.1.6 network.get_hostname

Get the hostname

[[email protected] ~]# salt 'node1' network.get_hostname 
node1:
    node1
[[email protected] ~]#

2.1.7 network.get_route

Query the routing information of a target network

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

2.1.8 network.hw_addr

Returns the name of the specified network card MAC Address

[[email protected] ~]# salt 'node1' network.hw_addr ens33
node1:
    00:0c:29:21:00:bb
[[email protected] ~]#

2.1.9 network.ifacestartswith

From the specific CIDR Retrieve interface name

[[email protected] ~]# salt 'node1' network.ifacestartswith 192
node1:
    - ens33
[[email protected] ~]# salt 'node1' network.ifacestartswith 127
node1:
    - lo
[[email protected] ~]#

2.1.11 network.interface

Returns the information of the specified network card

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

2.1.13 network.interfaces

Returns the information of all network cards in the current system

[[email protected] ~]# 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
[[email protected] ~]#

2.1.14 network.ip_addrs

Return to one IPv4 List of addresses
This function will ignore 127.0.0.1 The address of

[[email protected] ~]# salt 'node1' network.ip_addrs
node1:
    - 192.168.10.202
[[email protected] ~]#

2.1.15 network.netstat

Return all open ports and status

[[email protected] ~]# 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
[[email protected] ~]#

2.1.16 network.ping

Use ping Command to test connectivity to a host

[[email protected] ~]# 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
[[email protected] ~]#

2.1.17 network.reverse_ip

Returns a specified IP Reverse address of address

[[email protected] ~]# salt 'node1' network.reverse_ip 192.168.10.100
node1:
    100.10.168.192.in-addr.arpa
[[email protected] ~]#

2.2 SaltStack One of the common modules service

2.2.1 service.available

Determine whether the specified service is available

[[email protected] ~]# salt 'node1' service.available httpd
node1:
    True
[[email protected] ~]# salt 'node1' service.available nginx
node1:
    False
[[email protected] ~]#

2.2.2 service.get_all

Get all running services

[[email protected] ~]# salt 'node1' service.get_all 
node1:
    - NetworkManager
    - NetworkManager-dispatcher
    - NetworkManager-wait-online
    - arp-ethers
    - auditd
    - [email protected]
    - basic.target
    - blk-availability
    - bluetooth.target
    - boot-complete.target
    - [email protected]
    - [email protected]
    - chrony-wait
    - chronyd
    - console-getty
    - [email protected]
    - cpupower
    - crond
    - cryptsetup-pre.target
    - cryptsetup.target
    - ctrl-alt-del.target
    - dbus
    - dbus-org.freedesktop.hostname1
............... Omit 

2.2.3 service.disabled

Check whether the specified service is started or not

[[email protected] ~]# salt 'node1' service.disabled httpd
node1:
    False
[[email protected] ~]#

2.2.4 service.enabled

Check whether the specified service starts automatically

[[email protected] ~]# salt 'node1' service.enabled httpd
node1:
    True
[[email protected] ~]#

2.2.5 service.disable

Set the specified service to start automatically

[[email protected] ~]# salt 'node1' service.disable httpd
node1:
    True
[[email protected] ~]# salt 'node1' service.enabled httpd
node1:
    False
[[email protected] ~]#

2.2.6 service.enable

Set the specified service to start automatically

[[email protected] ~]# salt 'node1' service.enable httpd
node1:
    True
[[email protected] ~]# salt 'node1' service.enabled httpd
node1:
    True
[[email protected] ~]#

2.2.7 service.reload

Reload the specified service

[[email protected] ~]# salt 'node1' service.reload httpd
node1:
    True
[[email protected] ~]#

2.2.8 service.stop

Stop the specified service

[[email protected] ~]# salt 'node1' service.stop httpd
node1:
    True
[[email protected] ~]#

2.2.9 service.start

Start the specified service

[[email protected] ~]# salt 'node1' service.start httpd
node1:
    True
[[email protected] ~]#

2.2.10 service.restart

Restart the specified service

[[email protected] ~]# salt 'node1' service.restart httpd
node1:
    True
[[email protected] ~]#

2.2.11 service.status

View the status of the specified service

[[email protected] ~]# salt 'node1' service.status httpd
node1:
    True
[[email protected] ~]# salt 'node1' service.stop httpd
node1:
    True
[[email protected] ~]# salt 'node1' service.status httpd
node1:
    False
[[email protected] ~]#

2.3 SaltStack One of the common modules pkg

2.3.1 pkg.install

Install the software

[[email protected] ~]# salt 'node1' pkg.install wget
node1:
    ----------
    wget:
        ----------
        new:
            1.19.5-10.el8
        old:
[[email protected] ~]#

2.3.2 pkg.download

Only download packages but do not install
This function will download the specified package , But it needs to be in minion End installation yum-utils, have access to cmd.run Remote installation

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

2.3.3 pkg.list_downloaded

List the packages that have been downloaded locally

[[email protected] ~]# salt 'node1' pkg.list_downloaded 
node1:
    ----------
[[email protected] ~]#

2.3.4 pkg.file_list

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

// List the installed apache All files provided by the software package 
[[email protected] ~]# 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
................. Omit 

2.3.5 pkg.group_info

View package group information

[[email protected] ~]# 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
[[email protected] ~]#

2.3.6 pkg.group_list

List all package groups in the system

[[email protected] ~]# 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
[[email protected] ~]#

2.3.7 pkg.list_pkgs

List the currently installed packages in a dictionary

[[email protected] ~]# 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
........... Omit 

2.3.8 pkg.owner

Lists which package the specified file is provided by

[[email protected] ~]# salt 'node1' pkg.owner /usr/sbin/apachectl
node1:
    httpd
[[email protected] ~]# salt 'node1' pkg.owner /usr/sbin/apachectl /etc/httpd/conf/httpd.conf
node1:
    ----------
    /etc/httpd/conf/httpd.conf:
        httpd
    /usr/sbin/apachectl:
        httpd
[[email protected] ~]#

2.3.9 pkg.remove

Uninstall the specified software

[[email protected] ~]# salt 'node1' cmd.run 'rpm -aq|grep wget'
node1:
    wget-1.19.5-10.el8.x86_64
[[email protected] ~]# salt 'node1' pkg.remove wget
node1:
    ----------
    wget:
        ----------
        new:
        old:
            1.19.5-10.el8
[[email protected] ~]#
// To uninstall multiple files , The middle needs to be separated by commas 

2.3.10 pkg.upgrade

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

// If you want to upgrade all software packages in the system, put  name  Remove the parameters 
[[email protected] ~]# 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 
[[email protected] ~]# 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
............. Omit 

2.4 SaltStack One of the common modules state

2.4.1 state.show_highstate

Displays the advanced status of the current system

[[email protected] ~]# 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

Perform advanced state

[[email protected] ~]# 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

[[email protected] ~]# 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

return node1 Will be used for highstate Top level data

[[email protected] ~]# salt 'node1' state.show_top
node1:
    ----------
    base:
        - web.nginx.nginx

2.4.5 state.top

Execute specified top file, Not the default

[[email protected] ~]# 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

Show master Upper specific sls or sls Status data in the file list

[[email protected] ~]# 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 SaltStack One of the common modules user

2.5.1 user.add

user.add: stay node1 Create a user on the client .

usage :salt ‘*’ user.add name

[[email protected] ~]# salt 'node1' user.add tom
node1:
    True
[[email protected] ~]#

2.5.2 user.info

user.info: Return user information .

[[email protected] ~]# 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:
[[email protected] ~]#

2.5.3 user.getent

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

[[email protected] ~]# 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:
    |_
      ----------
................... Omit 

2.5.4 user.list_groups

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

[[email protected] ~]# salt 'node1' user.list_groups tom
node1:
    - tom
[[email protected] ~]#

2.5.5 user.rename

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

[[email protected] ~]# salt 'node1' user.rename tom jerry
node1:
    False			// Although return to False But the operation was successfully completed 
[[email protected] ~]# 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:
[[email protected] ~]#

2.5.6 user.delete

user.delete: stay minion End delete a user

[[email protected] ~]# salt 'node1' user.delete jerry
node1:
    True
[[email protected] ~]# salt 'node1' user.info jerry
node1:
    ----------
[[email protected] ~]#

2.6 SaltStack One of the common modules salt-cp

salt-cp It is very convenient to master Batch transfer of files on to minion On

// Copy a single file to the destination host /usr/src Under the table of contents 
[[email protected] ~]# salt 'node1' cmd.run 'ls /usr/src'
node1:
    debug
    kernels
[[email protected] ~]# salt-cp 'node1' /etc/passwd /usr/src/
node1:
    ----------
    /usr/src/passwd:
        True
[[email protected] ~]# salt 'node1' cmd.run 'ls /usr/src'
node1:
    debug
    kernels
    passwd
[[email protected] ~]#

// Copy multiple files to the destination host /usr/src Under the table of contents 
[[email protected] ~]# salt-cp 'node1' /etc/shadow /etc/group /usr/src
node1:
    ----------
    /usr/src/group:
        True
    /usr/src/shadow:
        True
[[email protected] ~]# salt 'node1' cmd.run 'ls /usr/src'
node1:
    debug
    group
    kernels
    passwd
    shadow
[[email protected] ~]#

2.7 SaltStack One of the common modules file

2.7.1 file.access

Check whether the specified path exists

[[email protected] ~]# salt 'node1' file.access /usr/src/passwd f
node1:
    True
[[email protected] ~]# salt 'node1' file.access /usr/src/tom f
node1:
    False
[[email protected] ~]#

Check the permission information of the specified file

[[email protected] ~]# 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
[[email protected] ~]# salt 'node1' file.access /usr/src/passwd r		 // Whether you have read permission 
node1:
    True
[[email protected] ~]# salt 'node1' file.access /usr/src/passwd w		// Whether you have permission to write 
node1:
    True
[[email protected] ~]# salt 'node1' file.access /usr/src/passwd x		// Whether there is Execution Authority 
node1:
    False
[[email protected] ~]#

2.7.2 file.append

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

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

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

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

2.7.3 file.basename

Gets the base name of the specified path

[[email protected] ~]# salt 'node1' file.basename '/root/abc/test'
node1:
    test
[[email protected] ~]#

2.7.4 file.dirname

Get the directory name of the specified path

[[email protected] ~]# salt 'node1' file.dirname '/root/abc/test'
node1:
    /root/abc
[[email protected] ~]#

2.7.5 file.check_hash

Check the specified file and hash Does the string match , Match returns True Otherwise return to False

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

2.7.6 file.chattr

Modify the properties of the specified file

attribute Meaning to the document What it means to the directory
a You are only allowed to append data after this file , No process is allowed to overwrite or truncate this file Only files can be created and modified in this directory , It's not allowed to delete any files
i No modifications are allowed to this file , Can't delete 、 change 、 Move Any process can only modify files under the directory , Creating and deleting files... Is not allowed

Add attributes to the specified file

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

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

Remove attributes from the specified file

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

2.7.7 file.chown

Sets the owner of the specified file 、 Group information

[email protected] ~]# 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
[[email protected] ~]# salt 'node1' file.chown /root/test tom tom
node1:
    None
[[email protected] ~]# 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
[[email protected] ~]#

2.7.8 file.copy

Copy files or directories on a remote host

Copy files

[[email protected] ~]# salt 'node1' file.copy /root/test /root/test-2
node1:
    True
[[email protected] ~]# 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
[[email protected] ~]

Overwrite and copy directory , The file or directory with the same name will be overwritten

[[email protected] ~]# 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
[[email protected] ~]# salt 'node1' cmd.run 'ls /root/testdir'
node1:
[[email protected] ~]# 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
[[email protected] ~]# salt 'node1' cmd.run 'ls /root/testdir'
node1:
    mysql.sock.lock
[[email protected] ~]# 

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

[[email protected] ~]# 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

[[email protected] ~]# 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

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

[[email protected] ~]# 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

Determine whether the specified directory exists , To be is to return True , Otherwise return to False

Determine whether the specified directory exists , To be is to return True , Otherwise return to False

[[email protected] ~]# 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
[[email protected] ~]# 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... In bytes

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

2.7.11 file.file_exists

Determine whether the specified file exists

[[email protected] ~]# 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
[[email protected] ~]# salt node1 file.file_exists /opt/lcr
node1:
    True
[[email protected]master ~]# salt node1 file.file_exists /opt/luochuran
node1:
    False   ## return False Because abc It's a directory, not a file  

2.7.12 file.find

similar 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( Matches 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 elimination a, b, c Anything other than )
[x-y]            = match chars x through y( Matching character x To y)
[!x-y] or [^x-y] = match anything except chars x through y( Match any character , Except for the characters x To 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( Catalog )
p: FIFO (named pipe)
f: plain file( Ordinary documents )
l: symlink( A 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( The contents of the document md5 Abstract )
mode:  file permissions (as integer)  ( File permissions ( In the form of integers ))
mtime: last modification time (as time_t)  ( Last modification time ( As time_t))
name:  file basename   ( file )
path:  file absolute path  ( The absolute path to the 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 suffix of the matching file is .bak And greater than 10M The file of )

salt '*' file.find /var mtime=+30d size=+10m print=path,size,mtime  ( Match in 30 File modified days ago and greater than 10M 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 of the matching file is number terminated , And in 30 Modified days ago and greater than 10M The file of , And delete them )

2.7.13 file.get_gid

Gets the gid

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

2.7.14 file.get_group

Get the group name of the specified file

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

2.7.15 file.get_hash

Gets the hash value , The value passes sha256 The algorithm comes from

[[email protected] ~]# salt node1 cmd.run 'sha256sum /opt/lcr'
node1:
    487aca02c302a0ac061a3a2cf837e68d006169c3a9501af8b9c56d21ef235a62  /opt/lcr
[[email protected] ~]# salt node1 file.get_hash /opt/lcr
node1:
    487aca02c302a0ac061a3a2cf837e68d006169c3a9501af8b9c56d21ef235a62

2.7.16 file.get_mode

Get the permission of the specified file , To display in digital form

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

2.7.17 file.get_selinux_context

Gets the SELINUX Context information

[[email protected] ~]# salt node1 cmd.run 'ls -Z /opt/lcr'
node1:
    unconfined_u:object_r:usr_t:s0 /opt/lcr
[[email protected] ~]# 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 of the specified file according to the specified algorithm and display , Default sha256 Algorithm .
The algorithm parameters that can be used by this function are :

  • md5
  • sha1
  • sha224
  • sha256 (default)
  • sha384
  • sha512
[[email protected] ~]# salt node1 cmd.run 'sha256sum /opt/lcr'
node1:
    487aca02c302a0ac061a3a2cf837e68d006169c3a9501af8b9c56d21ef235a62  /opt/lcr
[[email protected] ~]# salt node1 file.get_sum /opt/lcr
node1:
    487aca02c302a0ac061a3a2cf837e68d006169c3a9501af8b9c56d21ef235a62
[[email protected] ~]# salt node1 cmd.run 'md5sum /opt/lcr'
node1:
    861b478acdd37e1a909646b04e82290b  /opt/lcr
[[email protected] ~]# 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

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

2.7.20 file.gid_to_group

Specifies the gid Convert to group name and display

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

2.7.21 file.group_to_gid

Converts the specified group name to gid And display

[[email protected] ~]# salt node1 file.group_to_gid liu
node1:
    1000
[[email protected] ~]# salt node1 file.group_to_gid root
node1:
    0

2.7.22 file.grep

Retrieve 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

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

grep Parameters

  • -a or --text : Do not ignore binary data .
  • -A< Display row number > or --after-context=< Display row number > : Except for the column that matches the template style , And display the content after the line .
  • -b or --byte-offset : Before displaying the line in line with the style of , The line marked out a number of characters .
  • -B< Display row number > or --before-context=< Display row number > : Except for the line that matches the style , And display the contents before the line .
  • -c or --count : Calculated in line with the number of columns styles .
  • -C< Display row number > or --context=< Display row number > or -< Display row number > : Except for the line that matches the style , And display the contents before and after the line .
  • -d < action > or --directories=< action > : When you want to find the specified file is a directory rather than , You must use this parameter , otherwise grep The return of instruction information and stop action .
  • -e< Template Styles > or --regexp=< Template Styles > : Specifies a string as a style for finding the contents of a file .
  • -E or --extended-regexp : Use... As an extended regular expression .
  • -f< Rules file > or --file=< Rules file > : Specify rules file , The contents of which contains one or more rule patterns , Give Way grep Find what files that meet the rule conditions , Format for each line of a regular pattern .
  • -F or --fixed-regexp : The style seen as a list of fixed strings .
  • -G or --basic-regexp : It will be treated as ordinary style of notation to use .
  • -h or --no-filename : Before displaying the line in line with the style of , File names do not indicate the row belongs .
  • -H or --with-filename : Before displaying the line in line with the style of , It indicates the file name of the row belongs .
  • -i or --ignore-case : Ignore character case differences .
  • -l or --file-with-matches : Lists the file names whose contents match the specified style .
  • -L or --files-without-match : Lists the file names whose contents do not conform to the specified style .
  • -n or --line-number : Before displaying the line in line with the style of , Mark the number of columns in the row number .
  • -o or --only-matching : Show only match PATTERN part .
  • -q or --quiet or –silent : Does not display any information .
  • -r or --recursive : And the effect of this parameter is specified "-d recurse" Parameters are the same .
  • -s or --no-messages : Do not display error messages .
  • -v or --invert-match : Display does not contain matching text of all lines .
  • -V or --version : Display version information .
  • -w or --word-regexp : Show only in line with the whole-word column .
  • -x --line-regexp : Column column display only full compliance with the .
  • -y : And the effect of this parameter is specified "-i" Parameters are the same .

2.7.23 file.is_blkdev

Determine whether the specified file is a block device file

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

2.7.24 file.lsattr

Check and display the attribute information of the specified file

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

2.7.25 file.mkdir

Create a directory and set the owner 、 Membership group and authority

[[email protected] ~]# 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
[[email protected] ~]# salt node1 file.mkdir /root/test
node1:
    True
[[email protected] ~]# salt node1 cmd.run 'ls -l /root/test'
node1:
    total 0
[[email protected] ~]# 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

[[email protected] ~]# salt node1 file.mkdir /root/test2 lcr lcr 250
node1:
    True
[[email protected] ~]# 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

[[email protected] ~]# 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
[[email protected] ~]# salt node1 file.move /opt/liu /mnt/test
node1:
    ----------
    comment:
        '/opt/liu' moved to '/mnt/test'
    result:
        True
[[email protected] ~]# salt node1 cmd.run 'ls -l /mnt/test'
node1:
    -rw-r--r--. 1 root root 26 Nov  4 03:57 /mnt/test

file.prepend

Insert the text at the beginning of the specified file

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

file.sed

Modify the contents of the text file

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

file.read

Read file contents

[[email protected] ~]# salt node1 cmd.run 'cat /opt/luo'
node1:
    gege
    meme
    me
    is 
    best
[[email protected] ~]# 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

[[email protected] ~]# 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 you give a directory , Delete recursively

[[email protected] ~]# 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
[[email protected] ~]# salt node1 file.remove '/opt/luochuran'
node1:
    True
[[email protected] ~]# 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

[[email protected] ~]# 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
[[email protected] ~]# salt node1 file.rename /opt/liu /opt/ran
node1:
    True
[[email protected] ~]# 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

[[email protected] ~]# 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
[[email protected] ~]# salt node1 file.set_mode /opt/ran 0777
node1:
    0777
[[email protected] ~]# 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

Create a soft link to the specified file

[[email protected] ~]# salt node1 file.symlink /opt/lq /mnt/ltest
node1:
    True
[[email protected] ~]# 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

[[email protected] ~]# 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
[[email protected] ~]# salt node1 file.touch /opt/test
node1:
    True
[[email protected] ~]# 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

Specifies the uid Convert to user name and display

[[email protected] ~]# salt node1 file.uid_to_user 0
node1:
    root
[[email protected] ~]# salt node1 file.uid_to_user 1000
node1:
    tom

file.user_to_uid

Convert the specified user to uid And show it

[[email protected] ~]# salt node1 file.user_to_uid tom
node1:
    1000

file.write

Overwrite and write the specified content to a specified file

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

原网站

版权声明
本文为[Shit in your schoolbag]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110646442466.html