SaltStack常用的模块

SaltStack常用的模块

文章目录

SaltStack模块介绍

Module是日常使用SaltStack接触最多的一个组件,其用于管理对象操作,这也是SaltStack通过Push的方式进行管理的入口,比如我们日常简单的执行命令、查看包安装情况、查看服务运行情况等工作都是通过SaltStack Module来实现的。

当安装好Master和Minion包后,系统上会安装很多Module,大家可以通过以下命令查看支持的所有Module列表:

[root@master ~]# salt node1 sys.list_modules
node1:
    - acl
    - aliases
    - alternatives
    - apache
    - archive
    - artifactory
    - baredoc
    - beacons
    - bigip
    - btrfs
    - buildout
    - chroot
    - cloud
    - cmd
    - composer
    - config
    - consul
    - container_resource
    - cp
    - cron
    - cryptdev
·····省略部分·····

##查看指定module的所有function
[root@master ~]# salt node1 sys.list_functions cmd
node1:
    - cmd.exec_code
    - cmd.exec_code_all
    - cmd.has_exec
    - cmd.powershell
    - cmd.powershell_all
    - cmd.retcode
    - cmd.run
    - cmd.run_all
    - cmd.run_bg
    - cmd.run_chroot
    - cmd.run_stderr
    - cmd.run_stdout
    - cmd.script
    - cmd.script_retcode
    - cmd.shell
    - cmd.shell_info
    - cmd.shells
    - cmd.tty
    - cmd.which
    - cmd.which_bin
    
##查看指定module的用法
[root@master ~]# salt node1 sys.doc cmd
cmd.exec_code:

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

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

    CLI Example:

        salt '*' cmd.exec_code ruby 'puts "cheese"'
        salt '*' cmd.exec_code ruby 'puts "cheese"' args='["arg1", "arg2"]' env='{"FOO": "bar"}'
·····省略部分·····

##SaltStack默认也支持一次执行多个Module,Module之间通过逗号隔开,默认传参之间也是用逗号分隔,也支持指定传参分隔符号--args-separator=@即可
[root@master ~]# salt node1 test.echo,cmd.run,service.status hello,hostname,salt-minion
node1:
    ----------
    cmd.run:
        node1
    service.status:
        True
    test.echo:
        hello

SaltStack常用模块

SaltStack常用模块之network

network.active_tcp

返回所有活动的tcp连接

[root@master ~]# salt node1 network.active_tcp
node1:
    ----------
    0:
        ----------
        local_addr:
            192.168.240.60
        local_port:
            45882
        remote_addr:
            192.168.240.50
        remote_port:
            4505
    1:
        ----------
        local_addr:
            192.168.240.60
        local_port:
            22
        remote_addr:
            192.168.240.1
        remote_port:
            56441

network.calc_net

通过IP和子网掩码计算出网段

[root@master ~]# salt node1 network.calc_net 192.168.240.60 255.255.255.0
node1:
    192.168.240.0/24
[root@master ~]# salt node1 network.calc_net 192.168.240.60 255.255.255.240
node1:
    192.168.240.48/28
[root@master ~]# salt node1 network.calc_net 192.168.240.60 255.255.255.255
node1:
    192.168.240.60/32
network.connect

测试node1至某一台服务器的网络是否连通

[root@master ~]# salt node1 network.connect baidu.com 80
node1:
    ----------
    comment:
        Successfully connected to baidu.com (220.181.38.251) on tcp port 80
    result:
        Tru
network.default_route

查看默认路由

[root@master ~]# salt node1 network.default_route
node1:
    |_
      ----------
      addr_family:
          inet
      destination:
          0.0.0.0
      flags:
          UG
      gateway:
          192.168.240.2
      interface:
          ens33
      netmask:
          0.0.0.0

network.get_fqdn

查看主机的fqdn(完全限定域名)

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

获取主机名

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

查询到一个目标网络的路由信息

[root@master ~]# salt node1 network.get_route 192.168.240.60
node1:
    ----------
    destination:
        192.168.240.60
    gateway:
        None
    interface:
        lo
    source:
        192.168.240.60
network.hw_addr

返回指定网卡的MAC地址

[root@master ~]# salt node1 network.hw_addr ens33
node1:
    00:0c:29:11:4d:9c
network.ifacestartswith

从特定CIDR检索接口名称

[root@master ~]# salt node1 network.ifacestartswith 192.168
node1:
    - ens33
network.in_subnet

判断当前主机是否在某一个网段内

[root@master ~]# salt node1 network.in_subnet 192.168.240.0/24
node1:
    True
[root@master ~]# salt node1 network.in_subnet 192.168.260.0/24
node1:
    False
network.interface

返回指定网卡的信息

[root@master ~]# salt node1 network.interface ens33
node1:
    |_
      ----------
      address:
          192.168.240.60
      broadcast:
          192.168.240.255
      label:
          ens33
      netmask:
          255.255.255.0
network.interface_ip

返回指定网卡的IP地址

[root@master ~]# salt node1 network.interface_ip ens33
node1:
    192.168.240.60
network.interfaces

返回当前系统中所有的网卡信息

[root@master ~]# salt node1 network.interfaces
node1:
    ----------
    ens33:
        ----------
        hwaddr:
            00:0c:29:11:4d:9c
        inet:
            |_
              ----------
              address:
                  192.168.240.60
              broadcast:
                  192.168.240.255
              label:
                  ens33
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
              address:
                  fe80::fabc:12fd:b92d:c0a
              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
network.ip_addrs

返回一个IPv4的地址列表
该函数将会忽略掉127.0.0.1的地址

[root@master ~]# salt node1 network.ip_addrs
node1:
    - 192.168.240.60
network.netstat

返回所有打开的端口和状态

[root@master ~]# salt node1 network.netstat
node1:
    |_
      ----------
      inode:
          27022
      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:
          27023
      local-address:
          [::1]:323
      program:
          chronyd
      proto:
          udp
      recv-q:
          0
      remote-address:
          [::]:*
      send-q:
          0
      user:
          0
    |_
      ----------
      inode:
          26542
      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:
          31768
      local-address:
          192.168.240.60:45882
      program:
          salt-minion
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.240.50:4505
      send-q:
          0
      state:
          ESTABLISHED
      user:
          0
    |_
      ----------
      inode:
          32830
      local-address:
          192.168.240.60:22
      program:
          sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.240.1:56441
      send-q:
          52
      state:
          ESTABLISHED
      user:
          0
    |_
      ----------
      inode:
          0
      local-address:
          192.168.240.60:47916
      program:
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.240.50:4506
      send-q:
          0
      state:
          TIME-WAIT
      user:
          0
    |_
      ----------
      inode:
          27829
      local-address:
          *:80
      program:
          httpd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          *:*
      send-q:
          128
      state:
          LISTEN
      user:
          0
    |_
      ----------
      inode:
          26544
      local-address:
          [::]:22
      program:
          sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          [::]:*
      send-q:
          128
      state:
          LISTEN
      user:
          0
network.ping

使用ping命令测试到某主机的连通性

[root@master ~]# salt node1 network.ping 163.com
node1:
    PING 163.com (123.58.180.7) 56(84) bytes of data.
    64 bytes from 123.58.180.7 (123.58.180.7): icmp_seq=1 ttl=128 time=64.9 ms
    64 bytes from 123.58.180.7 (123.58.180.7): icmp_seq=2 ttl=128 time=44.8 ms
    64 bytes from 123.58.180.7 (123.58.180.7): icmp_seq=3 ttl=128 time=62.3 ms
    64 bytes from 123.58.180.7 (123.58.180.7): icmp_seq=4 ttl=128 time=54.2 ms
    
    --- 163.com ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3006ms
    rtt min/avg/max/mdev = 44.784/56.542/64.943/7.863 ms
network.reverse_ip

返回一个指定的IP地址的反向地址

[root@master ~]# salt node1 network.reverse_ip 192.168.240.34
node1:
    34.240.168.192.in-addr.arpa
network.convert_cidr

算出能够设置的子网掩码

[root@master ~]#  salt 'node1' network.convert_cidr 192.168.240.34/24
node1:
    ----------
    broadcast:
        192.168.240.255
    netmask:
        255.255.255.0
    network:
        192.168.240.0
[root@master ~]#  salt 'node1' network.convert_cidr 192.168.240.36/24
node1:
    ----------
    broadcast:
        192.168.240.255
    netmask:
        255.255.255.0
    network:
        192.168.240.0

SaltStack常用模块之service

service.available

判断指定的服务是否可用

[root@master ~]# salt node1 service.available sshd
node1:
    True
[root@master ~]# salt node1 service.available vsftp
node1:
    False
service.get_all

获取所有正在运行的服务

[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
    - console-getty
    - container-getty@
    - cpupower
    - crond
    - cryptsetup-pre.target
    - cryptsetup.target
    - ctrl-alt-del.target
    - dbus
    - dbus-org.freedesktop.hostname1
    - dbus-org.freedesktop.locale1
    - dbus-org.freedesktop.login1
    - dbus-org.freedesktop.nm-dispatcher
    - dbus-org.freedesktop.portable1
    - dbus-org.freedesktop.timedate1
    - dbus.socket
    - debug-shell
​```省略部分``````
service.enabled

检查指定服务是否开机自动启动

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

设置指定服务开机不自动启动

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

设置指定服务开机自动启动

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

重新加载指定服务

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

停止指定服务

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

启动指定服务

[root@master ~]# salt node1 service.restart nginx
node1:
    True
service.status

查看指定服务的状态

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

服务的反面。如果命名的服务不可用,则返回真实。

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

SaltStack常用模块之pkg

pkg.download

只下载软件包但不安装
此功能将会下载指定的软件包,但是需要在minion端安装yum-utils,可以使用 cmd.run 进行远程安装

[root@master ~]# salt node1 pkg.download nginx
node1:
    ----------
    nginx:
        /var/cache/yum/packages/nginx-1.14.1-9.module_el8.0.0+184+e34fea82.x86_64.rpm   ##下载好的软件放在这里
pkg.file_list

列出指定包或系统中已安装的所有包的文件

##列出已安装的nginx软件包提供的所有文件
[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
        - /etc/nginx/koi-win
        - /etc/nginx/mime.types
        - /etc/nginx/mime.types.default
        - /etc/nginx/nginx.conf
        - /etc/nginx/nginx.conf.default
        - /etc/nginx/scgi_params
        - /etc/nginx/scgi_params.default
        - /etc/nginx/uwsgi_params
        - /etc/nginx/uwsgi_params.default
        - /etc/nginx/win-utf
        - /usr/bin/nginx-upgrade
        - /usr/lib/.build-id
        - /usr/lib/.build-id/2d
        - /usr/lib/.build-id/2d/da6018ae12edb856ad3d2cf61bf586b6b4873c
        - /usr/lib/systemd/system/nginx.service
        - /usr/lib64/nginx/modules
        - /usr/sbin/nginx
        - /usr/share/doc/nginx
        - /usr/share/doc/nginx/CHANGES
        - /usr/share/doc/nginx/README
        - /usr/share/doc/nginx/README.dynamic
        - /usr/share/licenses/nginx
        - /usr/share/licenses/nginx/LICENSE
        - /usr/share/man/man3/nginx.3pm.gz
        - /usr/share/man/man8/nginx-upgrade.8.gz
        - /usr/share/man/man8/nginx.8.gz
        - /usr/share/nginx/html/404.html
        - /usr/share/nginx/html/50x.html
        - /usr/share/nginx/html/index.html
        - /usr/share/nginx/html/nginx-logo.png
        - /usr/share/nginx/html/poweredby.png
        - /usr/share/vim/vimfiles/ftdetect/nginx.vim
        - /usr/share/vim/vimfiles/indent/nginx.vim
        - /usr/share/vim/vimfiles/syntax/nginx.vim
        - /var/lib/nginx
        - /var/lib/nginx/tmp
        - /var/log/nginx
pkg.group_info

查看包组的信息

[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
pkg.group_list

列出系统中所有的包组

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

安装软件

[root@master ~]# salt node1 pkg.install nginx
node1:
    ----------
    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:
pkg.list_downloaded

列出已下载到本地的软件包

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

以字典的方式列出当前已安装的软件包

[root@master ~]# salt node1 pkg.list_pkgs
node1:
    ----------
    NetworkManager:
        1:1.30.0-0.3.el8
    NetworkManager-libnm:
        1:1.30.0-0.3.el8
    NetworkManager-team:
        1:1.30.0-0.3.el8
    NetworkManager-tui:
        1:1.30.0-0.3.el8
    abattis-cantarell-fonts:
        0.0.25-4.el8
    acl:
        2.2.53-1.el8
    adwaita-cursor-theme:
        3.28.0-2.el8
    adwaita-icon-theme:
        3.28.0-2.el8
    apr:
        1.6.3-12.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
    at-spi2-core:
        2.28.0-1.el8
    atk:
        2.28.1-1.el8
    audit:
        3.0-0.17.20191104git1c2f876.el8
    audit-libs:
        3.0-0.17.20191104git1c2f876.el8
    authselect:
        1.2.2-1.el8
​````省略部分····
pkg.owner

列出指定文件是由哪个包提供的

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

卸载指定软件

[root@master ~]# salt 'node1' cmd.run 'rpm -qa|grep nginx'
node1:
    nginx-mod-stream-1.14.1-9.module_el8.0.0+184+e34fea82.x86_64
    nginx-filesystem-1.14.1-9.module_el8.0.0+184+e34fea82.noarch
    nginx-mod-http-perl-1.14.1-9.module_el8.0.0+184+e34fea82.x86_64
    nginx-mod-mail-1.14.1-9.module_el8.0.0+184+e34fea82.x86_64
    nginx-1.14.1-9.module_el8.0.0+184+e34fea82.x86_64
    nginx-all-modules-1.14.1-9.module_el8.0.0+184+e34fea82.noarch
    nginx-mod-http-xslt-filter-1.14.1-9.module_el8.0.0+184+e34fea82.x86_64
    nginx-mod-http-image-filter-1.14.1-9.module_el8.0.0+184+e34fea82.x86_64

##若要卸载多个文件,中间需要用逗号隔开
[root@master ~]# salt 'node1' pkg.remove nginx
node1:
    ----------
    gd:
        ----------
        new:
        old:
            2.2.5-7.el8
    libXpm:
        ----------
        new:
        old:
            3.5.12-8.el8
    libwebp:
        ----------
        new:
        old:
            1.0.0-5.el8
    nginx:
        ----------
        new:
        old:
            1:1.14.1-9.module_el8.0.0+184+e34fea82
    nginx-all-modules:
        ----------
        new:
        old:
            1:1.14.1-9.module_el8.0.0+184+e34fea82
    nginx-filesystem:
        ----------
        new:
        old:
            1:1.14.1-9.module_el8.0.0+184+e34fea82
    nginx-mod-http-image-filter:
        ----------
        new:
        old:
            1:1.14.1-9.module_el8.0.0+184+e34fea82
    nginx-mod-http-perl:
        ----------
        new:
        old:
            1:1.14.1-9.module_el8.0.0+184+e34fea82
    nginx-mod-http-xslt-filter:
        ----------
        new:
        old:
            1:1.14.1-9.module_el8.0.0+184+e34fea82
    nginx-mod-mail:
        ----------
        new:
        old:
            1:1.14.1-9.module_el8.0.0+184+e34fea82
    nginx-mod-stream:
        ----------
        new:
        old:
            1:1.14.1-9.module_el8.0.0+184+e34fea82
pkg.upgrade

升级系统中所有的软件包或升级指定的软件包

##若想升级系统中所有的软件包则把 name 参数去掉即可
[root@master ~]# salt 'node1' pkg.upgrade name=openssl
node1:
    ----------
    openssl:
        ----------
        new:
            1:1.1.1k-4.el8
        old:
            1:1.1.1g-11.el8
    openssl-libs:
        ----------
        new:
            1:1.1.1k-4.el8
        old:
            1:1.1.1g-11.el8

pkg.version

查看版本

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

SaltStack常用模块之state

state.show_highstate

显示当前系统中有哪些高级状态

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

执行高级状态

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

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

显示当前系统中的高级状态执行情况

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

返回node1将用于highstate的顶级数据

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

执行指定的top file,而不是默认的

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

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

显示 master 上特定sls或sls文件列表中的状态数据

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

SaltStack常用模块之user

user.add

user.add:在node1端上创建一个用户。

用法:salt ‘*’ user.add name

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

user.info:返回用户信息。

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

user.getent:返回所有系统用户信息的列表

[root@master ~]# salt 'node1' user.getent 
node1:
    |_
      ----------
      fullname:
          root
      gid:
          0
      groups:
          - root
      home:
          /root
      homephone:
      name:
          root
      other:
      passwd:
          x
      roomnumber:
      shell:
          /bin/bash
      uid:
          0
      workphone:
    |_
      ----------
      fullname:
          bin
      gid:
          1
      groups:
          - bin
      home:
          /bin
      homephone:
      name:
          bin
      other:
      passwd:
          x
      roomnumber:
​````省略部分```
      ----------
      fullname:
      gid:
          1000
      groups:
          - liu
      home:
          /home/liu
      homephone:
      name:
          liu
      other:
      passwd:
          x
      roomnumber:
      shell:
          /bin/bash
      uid:
          1000
      workphone:

user.chgid

user.chgid:修改用户Gid

[root@master ~]# salt 'node1' cmd.run 'id liuq'
node1:
    uid=1001(liuq) gid=1002(liuq) groups=1002(liuq)
[root@master ~]# salt 'node1' user.chgid 'liuq' 1000
node1:
    True
[root@master ~]# salt 'node1' cmd.run 'id liuq'
node1:
    uid=1001(liuq) gid=1000(liu) groups=1000(liu)   
user.chuid

user.chuid:修改用户UID

[root@master ~]# salt 'node1' user.chuid 'liuq' 1024
node1:
    True
[root@master ~]# salt 'node1' cmd.run 'id liuq'
node1:
    uid=1024(liuq) gid=1000(liu) groups=1000(liu)
user.list_groups

user.list_groups:列出指定用户所属组的列表。

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

user.rename:修改指定用户的用户名

[root@master ~]# salt 'node1' user.rename liu lq
node1:
    False   ##虽然返回False但是操作是成功完成了的
[root@master ~]# salt 'node1' user.info lq
node1:
    ----------
    fullname:
    gid:
        1000
    groups:
        - liu
    home:
        /home/liu
    homephone:
    name:
        lq
    other:
    passwd:
        x
    roomnumber:
    shell:
        /bin/bash
    uid:
        1000
    workphone:

user.delete

user.delete:在minion端删除一个用户

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

SaltStack常用模块之salt-cp

salt-cp能够很方便的把 master 上的文件批量传到 minion上

##拷贝单个文件到目标主机的/usr/src目录下
[root@master ~]# salt node1 cmd.run 'ls /root/'
node1:
    anaconda-ks.cfg
    liu.sh
    !

[root@master ~]# salt-cp 'node1' /root/liu.sh /opt/
node1:
    ----------
    /opt/liu.sh:
        True
##拷贝多个文件到目标主机的/usr/src目录下
[root@master ~]# salt-cp 'node1' /opt/liu /opt/qiang /usr/src
node1:
    ----------
    /usr/src/liu:
        True
    /usr/src/qiang:
        True
        
[root@master ~]# salt 'node1' cmd.run 'ls /usr/src/'
node1:
    debug
    kernels
    liu
    passwd
    qiang

SaltStack常用模块之file

file.access

检查指定路径是否存在

[root@master ~]# salt 'node1' cmd.run 'ls /usr/src/'
node1:
    debug
    kernels
    liu
    passwd
    qian
    
[root@master ~]# salt 'node1' file.access /usr/src/liu f
node1:
    True

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

检查指定文件的权限信息

[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 60
    -rw-r--r--. 1 root root    13 Nov  4 02:42 liu.sh
    -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     0 Nov  4 02:27 qiang
[root@master ~]# salt node1 file.access  /opt/qiang r
node1:
    True
[root@master ~]# salt node1 file.access  /opt/qiang w
node1:
    True
[root@master ~]# salt node1 file.access  /opt/qiang x
node1:
    False

file.append

往一个文件里追加内容,若此文件不存在则会报异常

[root@master ~]# salt node1 cmd.run 'ls -l /opt/lq'
node1:
    -rw-r--r--. 1 root root 0 Nov  4 02:52 /opt/lq
[root@master ~]# salt node1 file.append /opt/lq "me"  "is" "best" 
node1:
    Wrote 3 lines to "/opt/lq"
[root@master ~]# salt node1 cmd.run 'cat /opt/lq'node1:
    me
    is
    best
file.basename

获取指定路径的基名

[root@master ~]# salt node1 file.basename '/opt/lq'
node1:
    lq
file.dirname

获取指定路径的目录名

[root@master ~]# salt node1 file.dirname '/opt/lq'
node1:
    /opt
file.check_hash

检查指定的文件与hash字符串是否匹配,匹配则返回 True 否则返回 False

[root@master ~]# salt node1 cmd.run 'md5sum /opt/lq'
node1:
    861b478acdd37e1a909646b04e82290b  /opt/lq
    
[root@master ~]# salt node1 file.check_hash /opt/lq 861b478acdd37e1a909646b04e82290b
node1:
    True
file.chattr

修改指定文件的属性

属性对文件的意义对目录的意义
a只允许在这个文件之后追加数据, 不允许任何进程覆盖或截断这个文件只允许在这个目录下建立和修改文件, 而不允许删除任何文件
i不允许对这个文件进行任何的修改, 不能删除、更改、移动任何的进程只能修改目录之下的文件, 不允许建立和删除文件

给指定文件添加属性

[root@master ~]# salt node1 cmd.run 'lsattr /opt/'node1:
    -------------------- /opt/master
    -------------------- /opt/passwd
    -------------------- /opt/lq
[root@master ~]# salt node1 file.chattr /opt/lq operator=add attributes=ia
node1:
    True
[root@master ~]# salt node1 cmd.run 'lsattr /opt/'node1:
    -------------------- /opt/master
    -------------------- /opt/passwd
    ----ia-------------- /opt/lq

给指定文件去除属性

[root@master ~]# salt node1 file.chattr /opt/lq operator=remove attributes=ia
node1:
    True
[root@master ~]# salt node1 cmd.run 'lsattr /opt/'node1:
    -------------------- /opt/master
    -------------------- /opt/passwd
    -------------------- /opt/lq
file.chown

设置指定文件的属主、属组信息

[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 60
    -rw-r--r--. 1 lq   lq      11 Nov  4 02:54 lq
    -rw-r-----. 1 root root 52909 Nov  2 03:00 master
    -rw-r--r--. 1 root root  1118 Nov  4 02:39 passwd
[root@master ~]# salt node1 file.chown /opt/lq root root
node1:
    None
[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 60
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lq
    -rw-r-----. 1 root root 52909 Nov  2 03:00 master
    -rw-r--r--. 1 root root  1118 Nov  4 02:39 passwd

file.copy

在远程主机上复制文件或目录

拷贝文件

[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 60
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lq
    -rw-r-----. 1 root root 52909 Nov  2 03:00 master
    -rw-r--r--. 1 root root  1118 Nov  4 02:39 passwd
[root@master ~]# salt node1 file.copy /opt/lq /opt/liu
node1:
    True
[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 64
    -rw-r--r--. 1 root root    11 Nov  4 03:11 liu
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lq
    -rw-r-----. 1 root root 52909 Nov  2 03:00 master
    -rw-r--r--. 1 root root  1118 Nov  4 02:39 passwd

覆盖并拷贝目录,将会覆盖同名文件或目录

[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 64
    -rw-r--r--. 1 root root    11 Nov  4 03:11 liu
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lq
    -rw-r-----. 1 root root 52909 Nov  2 03:00 master
    -rw-r--r--. 1 root root  1118 Nov  4 02:39 passwd
[root@master ~]# salt node1 file.copy /etc/jkjk/ /opt/liuqiang recurse=true
node1:
    True
[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 64
    -rw-r--r--. 1 root root    11 Nov  4 03:11 liu
    drwxr-xr-x. 2 root root     6 Nov  4 03:15 liuqiang
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lq
    -rw-r-----. 1 root root 52909 Nov  2 03:00 master
    -rw-r--r--. 1 root root  1118 Nov  4 02:39 passwd

删除目标目录中同名的文件或目录并拷贝新内容至其中

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

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

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

[root@master ~]# salt node1 cmd.run 'ls -l /etc/jkjk'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
file.ditectory_exists

判断指定目录是否存在,存在则返回 True ,否则返回 False

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

递归计算指定路径的磁盘使用情况并以字节为单位返回

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

判断指定文件是否存在

[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 64
    -rw-r--r--. 1 root root    11 Nov  4 03:11 liu
    drwxr-xr-x. 2 root root    51 Nov  4 03:16 liuqiang
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lq
    -rw-r-----. 1 root root 52909 Nov  2 03:00 master
    -rw-r--r--. 1 root root  1118 Nov  4 02:39 passwd
[root@master ~]# salt node1 file.file_exists /opt/lq
node1:
    True
[root@master ~]# salt node1 file.file_exists /opt/liuqianag
node1:
    False   ##返回False是因为abc是目录而非文件 

file.find

类似 find 命令并返回符合指定条件的路径列表

The options include match criteria:

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

and/or actions:

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

and/or depth criteria:

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

The default action is print=path

path-glob:

*                = match zero or more chars(匹配零个或多个字符)
?                = match any char(匹配任何字符)
[abc]            = match a, b, or c(匹配a, b或c)
[!abc] or [^abc] = match anything except a, b, and c(匹配除a, b, c以外的任何东西)
[x-y]            = match chars x through y(匹配字符x到y)
[!x-y] or [^x-y] = match anything except chars x through y(匹配任何字符,除了字符x到y)
{a,b,c}          = match a or b or c(匹配a或b或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(所有文件类型)
b: block device(块设备)
c: character device(字符设备)
d: directory(目录)
p: FIFO (named pipe)
f: plain file(普通文件)
l: symlink(符号链接)
s: 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 (组名)
md5:   MD5 digest of file contents(文件内容的md5摘要)
mode:  file permissions (as integer)  (文件权限(以整数形式))
mtime: last modification time (as time_t)  (最后一次修改时间(作为time_t))
name:  file basename   (文件)
path:  file absolute path  (文件的绝对路径)
size:  file size in bytes  (以字节为单位的文件大小)
type:  file type   (文件类型)
user:  user name   (用户名)

示例:

salt '*' file.find / type=f name=\*.bak size=+10m   (匹配文件后缀为.bak且大于10M的文件)

salt '*' file.find /var mtime=+30d size=+10m print=path,size,mtime  (匹配在30天前修改的文件且大于10M的文件的路径和大小以及最后一次修改的时间)

salt '*' file.find /var/log name=\*.[0-9] mtime=+30d size=+10m delete (匹配文件后缀名为数字结尾,且在30天前修改的并且大于10M的文件,并将它们删除)
file.get_gid

获取指定文件的gid

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

获取指定文件的组名

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

获取指定文件的hash值,该值通过 sha256 算法得来

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

获取指定文件的权限,以数字方式显示

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

file.get_selinux_context

获取指定文件的 SELINUX 上下文信息

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

按照指定的算法计算指定文件的特征码并显示,默认使用的sha256算法。
该函数可使用的算法参数有:

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

获取指定文件的 uid 或 用户名

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

将指定的 gid 转换为组名并显示

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

将指定的组名转换为 gid 并显示

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

在指定文件中检索指定内容
该函数支持通配符,若在指定的路径中用通配符则必须用双引号引起来

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

grep参数

  • -a 或 --text : 不要忽略二进制的数据。
  • -A<显示行数> 或 --after-context=<显示行数> : 除了显示符合范本样式的那一列之外,并显示该行之后的内容。
  • -b 或 --byte-offset : 在显示符合样式的那一行之前,标示出该行第一个字符的编号。
  • -B<显示行数> 或 --before-context=<显示行数> : 除了显示符合样式的那一行之外,并显示该行之前的内容。
  • -c 或 --count : 计算符合样式的列数。
  • -C<显示行数> 或 --context=<显示行数>或-<显示行数> : 除了显示符合样式的那一行之外,并显示该行之前后的内容。
  • -d <动作> 或 --directories=<动作> : 当指定要查找的是目录而非文件时,必须使用这项参数,否则grep指令将回报信息并停止动作。
  • -e<范本样式> 或 --regexp=<范本样式> : 指定字符串做为查找文件内容的样式。
  • -E 或 --extended-regexp : 将样式为延伸的正则表达式来使用。
  • -f<规则文件> 或 --file=<规则文件> : 指定规则文件,其内容含有一个或多个规则样式,让grep查找符合规则条件的文件内容,格式为每行一个规则样式。
  • -F 或 --fixed-regexp : 将样式视为固定字符串的列表。
  • -G 或 --basic-regexp : 将样式视为普通的表示法来使用。
  • -h 或 --no-filename : 在显示符合样式的那一行之前,不标示该行所属的文件名称。
  • -H 或 --with-filename : 在显示符合样式的那一行之前,表示该行所属的文件名称。
  • -i 或 --ignore-case : 忽略字符大小写的差别。
  • -l 或 --file-with-matches : 列出文件内容符合指定的样式的文件名称。
  • -L 或 --files-without-match : 列出文件内容不符合指定的样式的文件名称。
  • -n 或 --line-number : 在显示符合样式的那一行之前,标示出该行的列数编号。
  • -o 或 --only-matching : 只显示匹配PATTERN 部分。
  • -q 或 --quiet或–silent : 不显示任何信息。
  • -r 或 --recursive : 此参数的效果和指定"-d recurse"参数相同。
  • -s 或 --no-messages : 不显示错误信息。
  • -v 或 --invert-match : 显示不包含匹配文本的所有行。
  • -V 或 --version : 显示版本信息。
  • -w 或 --word-regexp : 只显示全字符合的列。
  • -x --line-regexp : 只显示全列符合的列。
  • -y : 此参数的效果和指定"-i"参数相同。
file.is_blkdev

判断指定的文件是否是块设备文件

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

file.lsattr

检查并显示出指定文件的属性信息

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

创建目录并设置属主、属组及权限

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

[root@master ~]# salt node1 file.mkdir /root/you lq lq 250
node1:
    True
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
    total 60
    -rw-------. 1 root root  1023 Jul 16 07:36 anaconda-ks.cfg
    drwxr-xr-x. 2 root root     6 Nov  4 04:09 best
    -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 you
    -rw-r--r--. 1 root root 52909 Nov  2 03:07 !
file.move

移动或重命名

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

把文本插入指定文件的开头

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

file.sed

修改文本文件的内容

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

读取文件内容

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

列出指定目录下的所有文件或目录,包括隐藏文件

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

删除指定的文件或目录,若给出的是目录,将递归删除

[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 68
    -rw-r--r--. 1 root root    22 Nov  7 02:03 liu
    -rw-r--r--. 1 root root    20 Nov  7 02:02 liu.bak
    drwxr-xr-x. 2 root root    51 Nov  4 03:16 liuqiang
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lq
    -rw-r-----. 1 root root 52909 Nov  2 03:00 master
    -rw-r--r--. 1 root root  1118 Nov  4 02:39 passwd
[root@master ~]# salt node1 file.remove '/opt/liuqiang'
node1:
    True
[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 68
    -rw-r--r--. 1 root root    22 Nov  7 02:03 liu
    -rw-r--r--. 1 root root    20 Nov  7 02:02 liu.bak
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lq
    -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

重命名文件或目录

[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 68
    -rw-r--r--. 1 root root    22 Nov  7 02:03 liu
    -rw-r--r--. 1 root root    20 Nov  7 02:02 liu.bak
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lq
    -rw-r-----. 1 root root 52909 Nov  2 03:00 master
    -rw-r--r--. 1 root root  1118 Nov  4 02:39 passwd
[root@master ~]# salt node1 file.rename /opt/liu /opt/qiang
node1:
    True
[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 68
    -rw-r--r--. 1 root root    20 Nov  7 02:02 liu.bak
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lq
    -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 qiang
file.set_mode

给指定文件设置权限

[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 68
    -rw-r--r--. 1 root root    20 Nov  7 02:02 liu.bak
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lq
    -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 qiang
[root@master ~]# salt node1 file.set_mode /opt/qiang 0777
node1:
    0777
[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 68
    -rw-r--r--. 1 root root    20 Nov  7 02:02 liu.bak
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lq
    -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 qiang

file.symlink

给指定的文件创建软链接

[root@master ~]# salt node1 file.symlink /opt/lq /mnt/liuq
node1:
    True
[root@master ~]# salt node1 cmd.run 'ls -l /opt/;ls -l /mnt'
node1:
    total 68
    -rw-r--r--. 1 root root    20 Nov  7 02:02 liu.bak
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lq
    -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 qiang
    total 8
    drwxr-xr-x. 2 root root  6 Jul 16 07:33 hgfs
    lrwxrwxrwx. 1 root root  7 Nov  7 02:11 liuq -> /opt/lq
    -rw-r--r--. 1 root root 11 Nov  4 02:54 lq
    -rw-r--r--. 1 root root 26 Nov  4 03:57 lqq

file.touch

创建空文件或更新时间戳

[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 68
    -rw-r--r--. 1 root root    20 Nov  7 02:02 liu.bak
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lq
    -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 qiang
[root@master ~]# salt node1 file.touch /opt/lql
node1:
    True
[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
    total 68
    -rw-r--r--. 1 root root    20 Nov  7 02:02 liu.bak
    -rw-r--r--. 1 root root    11 Nov  4 02:54 lq
    -rw-r--r--. 1 root root     0 Nov  7 02:13 lql
    -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 qiang

file.uid_to_user

将指定的 uid 转换成用户名显示出来

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

file.user_to_uid

将指定的用户转换成 uid 并显示出来

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

往一个指定的文件里覆盖写入指定内容

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枯木逢秋࿐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值