SaltStack常用的模块

目录

1. SaltStack模块介绍

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

当安装好Master和Minion包后,系统上会安装很多Module,大家可以通过以下命令查看支持的所有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
  ··························省略

//查看指定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:
········································省略


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

2. SaltStack常用模块

2.1 SaltStack常用模块之network

2.1.1 network.active_tcp

返回所有活动的tcp连接

[root@master ~]# salt '*' network.active_tcp
node1:
    ----------
    0:
        ----------
        local_addr:
            192.168.235.172
        local_port:
            46716
        remote_addr:
            192.168.235.160
        remote_port:
            4505
    1:
        ----------
        local_addr:
            192.168.235.172
        local_port:
            22
        remote_addr:
            192.168.235.1
        remote_port:
            51040
    2:
        ----------
        local_addr:
            192.168.235.172
        local_port:
            22
        remote_addr:
            192.168.235.1
        remote_port:
            51039
master:
    ----------
    0:
        ----------
        local_addr:
            192.168.235.160
        local_port:
            22
        remote_addr:
            192.168.235.1
        remote_port:
            51078
    1:
        ----------
        local_addr:
            192.168.235.160
        local_port:
            22
        remote_addr:
            192.168.235.1
        remote_port:
            51013
    2:
        ----------
        local_addr:
            192.168.235.160
        local_port:
            39636
        remote_addr:
            192.168.235.160
        remote_port:
            4505
    3:
        ----------
        local_addr:
            192.168.235.160
        local_port:
            4505
        remote_addr:
            192.168.235.160
        remote_port:
            39636
    4:
        ----------
        local_addr:
            192.168.235.160
        local_port:
            22
        remote_addr:
            192.168.235.1
        remote_port:
            51076
    5:
        ----------
        local_addr:
            192.168.235.160
        local_port:
            4505
        remote_addr:
            192.168.235.172
        remote_port:
            46716
2.1.2 network.calc_net

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

[root@master ~]# salt '*' network.calc_net 192.168.235.172 255.255.255.0
node1:
    192.168.235.0/24
master:
    192.168.235.0/24

[root@master ~]# salt '*' network.calc_net 192.168.235.172 255.255.0.0
node1:
    192.168.0.0/16
master:
    192.168.0.0/16

2.1.3 network.connect

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

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

查看默认路由

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

2.1.5 network.get_fqdn

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

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

获取主机名

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

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

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

返回指定网卡的MAC地址

[root@master ~]# salt '*' network.hw_addr ens33
node1:
    00:0c:29:f9:f9:6a

2.1.9 network.ifacestartswith

从特定CIDR检索接口名称

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

2.1.10 network.in_subnet

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

[root@master ~]# salt '*' network.in_subnet 192.168.235.0/24
node1:
    True
2.1.11 network.interface

返回指定网卡的信息

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

2.1.12 network.interface_ip

返回指定网卡的IP地址

[root@master ~]# salt '*' network.interface_ip ens33
node1:
    192.168.235.172

2.1.13 network.interfaces

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

[root@master ~]# salt '*' network.interfaces
node1:
    ----------
    ens33:
        ----------
        hwaddr:
            00:0c:29:f9:f9:6a
        inet:
            |_
              ----------
              address:
                  192.168.235.172
              broadcast:
                  192.168.235.255
              label:
                  ens33
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
              address:
                  fe80::f0c5:7dad:6ced:2d9d
              prefixlen:
                  64
              scope:
                  link
        up:
            True
    lo:
        ----------
        hwaddr:
            00:00:00:00:00:00
        inet:
            |_
              ----------
              address:
                  127.0.0.1
              broadcast:
                  None
              label:
                  lo
              netmask:
                  255.0.0.0
        inet6:
            |_
              ----------
              address:
                  ::1
              prefixlen:
                  128
              scope:
                  host
        up:
            True

2.1.14 network.ip_addrs

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

[root@master ~]# salt '*' network.ip_addrs
node1:
    - 192.168.235.172
2.1.15 network.netstat

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

[root@master ~]# salt '*' network.netstat
node1:
    |_
      ----------
      inode:
          28044
      local-address:
          192.168.235.172%ens33:68
      program:
          NetworkManager
      proto:
          udp
      recv-q:
          0
      remote-address:
          192.168.235.254:67
      send-q:
          0
      user:
          0
    |_
      ----------
      inode:
          25245
      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:
          25246
      local-address:
          [::1]:323
      program:
          chronyd
      proto:
          udp
      recv-q:
          0
      remote-address:
          [::]:*
      send-q:
          0
      user:
          0
    |_
      ----------
      inode:
          27661
      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:
          100435
      local-address:
          192.168.235.172:46716
      program:
          salt-minion
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.235.160:4505
      send-q:
          0
      state:
          ESTABLISHED
      user:
          0
    |_
      ----------
      inode:
          0
      local-address:
          192.168.235.172:59592
      program:
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.235.160:4506
      send-q:
          0
      state:
          TIME-WAIT
      user:
          0
    |_
      ----------
      inode:
          30231
      local-address:
          192.168.235.172:22
      program:
          sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.235.1:51040
      send-q:
          0
      state:
          ESTABLISHED
      user:
          0
    |_
      ----------
      inode:
          30189
      local-address:
          192.168.235.172:22
      program:
          sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.235.1:51039
      send-q:
          0
      state:
          ESTABLISHED
      user:
          0
    |_
      ----------
      inode:
          27673
      local-address:
          [::]:22
      program:
          sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          [::]:*
      send-q:
          128
      state:
          LISTEN
      user:
          0
    |_
      ----------
      inode:
          26500
      local-address:
          *:80
      program:
          httpd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          *:*
      send-q:
          128
      state:
          LISTEN
      user:
          0
2.1.16 network.ping

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

[root@master ~]# salt '*' network.ping baidu.com
node1:
    PING baidu.com (220.181.38.251) 56(84) bytes of data.
    
    --- baidu.com ping statistics ---
    4 packets transmitted, 0 received, 100% packet loss, time 3078ms

2.1.17 network.reverse_ip

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

[root@master ~]# salt '*' network.reverse_ip 192.168.235.172
node1:
    172.235.168.192.in-addr.arpa

2.2 SaltStack常用模块之service

2.2.1 service.available

判断指定的服务是否可用

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

获取所有正在运行的服务

[root@master ~]# salt '*' 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
2.2.3 service.disabled

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

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

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

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

2.2.5 service.disable

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

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

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

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

重新加载指定服务

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

2.2.8 service.stop

停止指定服务

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

启动指定服务

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

2.2.10 service.restart

重启指定服务


2.2.11 service.status

查看指定服务的状态

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

2.3 SaltStack常用模块之pkg

2.3.1 pkg.download

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

[root@master ~]# salt '*' pkg.download wget
node1:
    ----------
    wget:
        /var/cache/yum/packages/wget-1.19.5-10.el8.x86_64.rpm   //下载好的软件放在这里

2.3.2 pkg.file_list

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

//列出已安装的apache软件包提供的所有文件
[root@master ~]# salt '*' 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
        - /etc/httpd/conf/magic

2.3.3 pkg.group_info

查看包组的信息

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

列出系统中所有的包组

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

安装软件

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

2.3.6 pkg.list_downloaded

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

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

2.3.7 pkg.list_pkgs

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

[root@master ~]# salt '*' pkg.list_pkgs
node1:
    ----------
    NetworkManager:
        1:1.30.0-0.3.el8
    NetworkManager-libnm:
        1:1.30.0-0.3.el8
    NetworkManager-team:
        1:1.30.0-0.3.el8
    NetworkManager-tui:
        1:1.30.0-0.3.el8
    abattis-cantarell-fonts:
        0.0.25-4.el8
    acl:
        2.2.53-1.el8
    adwaita-cursor-theme:
        3.28.0-2.el8
    adwaita-icon-theme:
        3.28.0-2.el8
        ·····························
2.3.8 pkg.owner

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

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

2.3.9 pkg.remove

卸载指定软件

[root@master ~]# salt '*' cmd.run 'rpm -qa|grep wget'
node1:
    wget-1.19.5-10.el8.x86_64
[root@master ~]# salt '*' pkg.remove wget
node1:
    ----------
    wget:
        ----------
        new:
        old:
            1.19.5-10.el8
//若要卸载多个文件,中间需要用逗号隔开
2.3.10 pkg.upgrade

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

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

//若想升级系统中所有的软件包则把 name 参数去掉即可

2.4 SaltStack常用模块之state

2.4.1 state.show_highstate

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

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

2.4.2 state.highstate

执行高级状态

[root@master ~]# salt '*' state.highstate web.apache.apache
node1:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 19:01:14.490789
    Duration: 587.158 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 19:01:15.083009
    Duration: 52.102 ms
     Changes:   

Summary for node1
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
Total run time: 639.260 ms

2.4.3 state.show_state_usage

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

[root@master ~]# salt '*' state.show_state_usage
node1:
    ----------
    base:
        ----------
        count_all:
            4
        count_unused:
            3
        count_used:
            1
        unused:
            - top
            - web.apache.apache
            - web.nginx.install
        used:
            - web.apache.install
    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

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

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

2.4.5 state.top

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

[root@master ~]# salt 'node1' state.top test.sls
node1:
----------
          ID: httpd_install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 03:57:43.469124
    Duration: 421.665 ms
     Changes:   
----------
          ID: httpd_service
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 03:57:43.892584
    Duration: 44.345 ms
     Changes:   

Summary for node1
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
Total run time: 466.010 ms
2.4.6 state.show_sls

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

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

2.5 SaltStack常用模块之uesr

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

创建用户

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

//node1上查看
[root@node1 ~]# id weixiaoya
uid=1001(weixiaoya) gid=1001(weixiaoya) 组=1001(weixiaoya)

2.5.2 user.chgid

更改用户的默认组

[root@master ~]#  salt 'node1' user.chgid weixiaoya 1000
node1:
    True

[root@node1 ~]# id weixiaoya
uid=1001(weixiaoya) gid=1000(wxy) 组=1000(wxy)
2.5.3 user.chgroups

更改用户的所属组

[root@master ~]# salt 'node1' user.chgroups weixiaoya weixiaoya
node1:
    True

[root@node1 ~]# id weixiaoya
uid=1001(weixiaoya) gid=1000(wxy) 组=1000(wxy),1001(weixiaoya)

2.5.4 user.chhome

更改用户的家目录,如果旧的家目录存在,则将家目录的文件移动到新的家目录

[root@master ~]# salt 'node1' user.chhome weixiaoya /home/wxy
node1:
    True
2.5.5 user.chuid

更改指定用户的uid

[root@master ~]# salt node1 user.chuid weixiaoya 1024
node1:
    True
    
 [root@node1 ~]# id weixiaoya
uid=1024(weixiaoya) gid=1000(wxy) 组=1000(wxy),1001(weixiaoya)

2.5.6 user.delete

从minion中删除一个用户

[root@master ~]# salt 'node1' user.delete weixiaoya
node1:
    True
    
[root@node1 ~]# id weixiaoya
id: “weixiaoya”:无此用户
2.5.7 user.rename

更改用户名

[root@node1 ~]# id luohuan
uid=1001(luohuan) gid=1002(lhh) 组=1002(lhh)

[root@master ~]# salt node1 user.rename luohuan yaya
node1:
	True

[root@node1 ~]# id luohuan
id: “luohuan”:无此用户

[root@node1 ~]# id yaya
uid=1001(yaya) gid=1002(lhh) 组=1002(lhh)

2.6 SaltStack常用模块之salt-cp

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

//拷贝单个文件到目标主机的/usr/src目录下
[root@master ~]#  salt '*' cmd.run 'ls /usr/src/'
node1:
    debug
    kernels

[root@master ~]# salt-cp '*' /etc/passwd /usr/src/
master:
    ----------
    /usr/src/passwd:
        True
[root@master ~]# salt '*' cmd.run 'ls /usr/src'
node1:
    debug
    kernels
    passwd

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

2.7 SaltStack常用模块之file

2.7.1 file.access

检查指定路径是否存在

[root@node1 ~]# ls /usr/src/
debug  group  kernels  passwd  shadow

[root@master ~]# salt node1 file.access /usr/src/debug f
node1:
    True
[root@master ~]# salt node1 file.access /usr/src/yaya f
node1:
    False

检查指定文件的权限信息

[root@node1 ~]# ll /usr/src/
总用量 12
drwxr-xr-x. 2 root root    6 5月  19 2020 debug
-rw-r--r--  1 root root  513 11月  4 18:20 group
drwxr-xr-x. 2 root root    6 5月  19 2020 kernels

[root@master ~]# salt node1 file.access /usr/src/debug r	//是否有读权限
node1:
    True
[root@master ~]# salt node1 file.access /usr/src/debug w	//是否有写权限
node1:
    True
[root@master ~]# salt node1 file.access /usr/src/debug x	//是否有执行权限
node1:
    True

2.7.2 file.append

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

[root@node1 ~]# ll hh
-rw-r--r-- 1 root root 2 11月  4 18:29 hh

[root@master ~]# salt node1 file.append /root/hh "hello" "yaya"
node1:
    Wrote 2 lines to "/root/hh"
    
[root@node1 ~]# cat hh
hello
yaya

[root@node1 ~]# ll hh
-rw-r--r-- 1 root root 13 11月  4 18:32 hh

2.7.3 file.basename

获取指定路径的基名

[root@master ~]# salt node1 file.basename '/usr/src/debug'
node1:
    debug

2.7.4 file.dirname

获取指定路径的目录名

[root@master ~]# salt node1 file.dirname '/usr/src/debug'
node1:
    /usr/src

2.7.5 file.check_hash

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

[root@node1 ~]# md5sum /etc/passwd
3735b6645a321bf2971d5a9a718937e1  /etc/passwd
[root@master ~]# salt node1 file.check_hash /etc/passwd 3735b6645a321bf2971d5a9a718937e1
node1:
    True
2.7.6 file.chattr

修改指定文件的属性

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

给指定文件添加属性

//查看当前属性
[root@master ~]# salt '*' cmd.run 'lsattr /root'
node1:
    -------------------- /root/anaconda-ks.cfg
    -------------------- /root/yaya
    -------------------- /root/hh

//添加属性
[root@master ~]# salt '*' file.chattr /root/hh operator=add attributes=ai
node1:
    True

给指定文件去除属性

[root@master ~]# salt '*' file.chattr /root/hh operator=remove attributes=i
node1:
    True

[root@master ~]# salt '*' cmd.run 'lsattr /root'
node1:
    -------------------- /root/anaconda-ks.cfg
    -------------------- /root/yaya
    -----a-------------- /root/hh

2.7.7 file.chown

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

[root@master ~]# salt '*' cmd.run 'ls -l /root/'
node1:
    total 8
    -rw-------. 1 root root 1244 Nov  1 16:27 anaconda-ks.cfg
    -rw-r--r--  1 root root   13 Nov  4 18:32 hh
    drwxr-xr-x  2 root root    6 Nov  4 18:29 yaya

[root@master ~]# salt node1 file.chown /root/yaya tom tom
node1:
    None
    
 [root@master ~]# salt node1 cmd.run 'ls -l /root/'
node1:
    total 8
    -rw-------. 1 root root 1244 Nov  1 16:27 anaconda-ks.cfg
    -rw-r--r--  1 root root   13 Nov  4 18:32 hh
    drwxr-xr-x  2 tom  tom     6 Nov  4 18:29 yaya
2.7.8 file.copy

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

拷贝文件

[root@node1 ~]# ll
总用量 8
-rw-------. 1 root root 1244 11月  1 16:27 anaconda-ks.cfg
-rw-r--r--  1 root root   13 11月  4 18:32 hh
drwxr-xr-x  2 tom  tom     6 11月  4 18:29 yaya

[root@master ~]# salt node1 file.copy /root/hh /root/ww
node1:
    True
    
[root@node1 ~]# ll
总用量 12
-rw-------. 1 root root 1244 11月  1 16:27 anaconda-ks.cfg
-rw-r--r--  1 root root   13 11月  4 18:32 hh
-rw-r--r--  1 root root   13 11月  4 18:50 ww
drwxr-xr-x  2 tom  tom     6 11月  4 18:29 yaya

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

[root@node1 ~]# ll
total 12
-rw-r--r--. 1 root root   14 Nov  4 02:49 abc
-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
-rw-r--r--. 1 root root   14 Nov  4 02:27 test

[root@master ~]# salt node1 file.copy /tmp /root/123 recurse=True
node1:
    True
    
[root@node1 ~]# ll
total 16
drwxrwxrwt. 14 root root 4096 Nov  4 02:52 123
-rw-r--r--.  1 root root   14 Nov  4 02:49 abc
-rw-------.  1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
-rw-r--r--.  1 root root   14 Nov  4 02:27 test

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

[root@master ~]# salt '*' cmd.run 'ls -l /root/abc'
192.168.69.202:
    total 336
    drwxr-xr-x. 2 root root      6 Feb 24 15:02 abc
    -rw-r--r--. 1 root root   1259 Feb 24 15:02 anaconda-ks.cfg
    -rw-r--r--. 1 root root 219068 Feb 24 15:02 libevent-2.0.21-4.el7.x86_64.rpm
    -rw-r--r--. 1 root root  86564 Feb 24 15:02 libevent-devel-2.0.21-4.el7.x86_64.rpm
    drwxr-xr-x. 3 root root     17 Feb 24 15:02 systemd-private-07ec2f23d5a348e5b7cf1d1c0799ebd4-httpd.service-qGPJuL
    drwxr-xr-x. 3 root root     17 Feb 24 15:02 systemd-private-07ec2f23d5a348e5b7cf1d1c0799ebd4-vgauthd.service-8p0jur
    drwxr-xr-x. 3 root root     17 Feb 24 15:02 systemd-private-07ec2f23d5a348e5b7cf1d1c0799ebd4-vmtoolsd.service-ouRK2O
    drwxr-xr-x. 2 root root      6 Feb 24 15:02 systemd-private-07ec2f23d5a348e5b7cf1d1c0799ebd4-vmtoolsd.service-ouRK2Ovmware-root
    -rw-r--r--. 1 root root    493 Feb 24 15:02 yum_save_tx.2019-02-22.14-22.RXRzka.yumtx
    -rw-r--r--. 1 root root  22692 Feb 24 15:02 zabbix_agentd.log
[root@master ~]# salt '*' cmd.run 'ls -l /opt/abc'
192.168.69.202:
    total 0
    drwx------. 3 root root 17 Jan  7 16:41 systemd-private-fdaaf26c12984e209f544671b67c1951-vgauthd.service-kI2C1K
    drwx------. 3 root root 17 Jan  7 16:41 systemd-private-fdaaf26c12984e209f544671b67c1951-vmtoolsd.service-ZzRiqC
    
    
//拷贝目录
[root@master ~]# salt '*' file.copy /opt/abc/ /root/abc/ recurse=True remove_existing=True
192.168.69.202:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root/abc'
192.168.69.202:
    total 0
    drwx------. 3 root root 17 Jan  7 16:41 systemd-private-fdaaf26c12984e209f544671b67c1951-vgauthd.service-kI2C1K
    drwx------. 3 root root 17 Jan  7 16:41 systemd-private-fdaaf26c12984e209f544671b67c1951-vmtoolsd.service-ZzRiqC
2.7.9 file.ditectory_exists

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

[root@master ~]# salt '*' cmd.run 'ls -l /opt'
node1:
    total 0

[root@master ~]# salt '*' file.directory_exists /opt/abc
node1:
    False
2.7.10 file.file_exists

判断指定文件是否存在

[root@master ~]# salt '*' file.file_exists /root/hh
node1:
    True

[root@master ~]# salt '*' file.file_exists /root/yaya
node1:
    False

2.7.11 file.find

类似 find 命令并返回符合指定条件的路径列表
The options include match criteria:

name    = path-glob                 # 区分大小写
iname   = path-glob                 # 不区分大小写
regex   = path-regex                # 区分大小写
iregex  = path-regex                # 不区分大小写
type    = file-types                # 匹配任何列出的类型
user    = users                     # 匹配任何列出的用户
group   = groups                    # 匹配任何列出的组
size    = [+-]number[size-unit]     # 默认单位=字节
mtime   = interval                  # 从日期开始修改
grep    = regex                     # 搜索文件内容

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:

*                = 匹配零个或多个字符
?                = 匹配任意字符
[abc]            = 匹配a, b或c
[!abc] or [^abc] = 匹配除a, b, c之外的任何东西
[x-y]            = 匹配字符x到y
[!x-y] or [^x-y] = 匹配除了字符x到y以外的任何字符
{a,b,c}          = 匹配 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
b: block device
c: character device
d: directory
p: FIFO (named pipe)
f: plain file
l: symlink
s: socket
--------------------
a: 所有文件类型
b: 块设备
c: 字符设备
d: 目录
p: FIFO(命名管道)
f: 普通文件
l: 符号链接
s: 套接字

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: :组名
md5:   文件内容的md5摘要
mode:  文件权限(以整数形式)
mtime: 最后一次修改时间(as time_t)
name:  file basename
path:  文件的绝对路径
size:  以字节为单位的文件大小
type:  文件类型
user:  用户名

示例:

salt '*' file.find / type=f name=\*.bak size=+10m
    
salt '*' file.find /var mtime=+30d(30天以前文件被修改的时间) size=+10m print=path,size,mtime
salt '*' file.find /var/log name=\*.[0-9] mtime=+30d size=+10m delete
2.7.12 file.get_gid

获取指定文件的gid

[root@master ~]#  salt '*' cmd.run 'ls -l /root/hh'
node1:
    -rw-r--r-- 1 root root 13 Nov  4 18:32 /root/hh
[root@master ~]# salt '*' file.get_gid /root/hh
node1:
    0

2.7.13 file.get_group

获取指定文件的组名

[root@master ~]# salt '*' cmd.run 'ls -l /root/hh'
node1:
    -rw-r--r-- 1 root root 13 Nov  4 18:32 /root/hh

[root@master ~]# salt '*' file.get_group /root/hh
node1:
    root
2.7.14 file.get_hash

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

[root@master ~]# salt '*' cmd.run 'sha256sum /root/hh'
node1:
    51cbfc85c15e0916ac9ca96c357b514385d61f58ef200d0be919d36e6bae31fc  /root/hh
[root@master ~]# salt '*' file.get_hash /root/hh
node1:
    51cbfc85c15e0916ac9ca96c357b514385d61f58ef200d0be919d36e6bae31fc
2.7.15 file.get_mode

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

[root@master ~]# salt '*' cmd.run 'ls -l /root/hh'
node1:
    -rw-r--r-- 1 root root 13 Nov  4 18:32 /root/hh
    
[root@master ~]#  salt '*' file.get_mode /root/hh
node1:
    0644

2.7.16 file.get_selinux_context

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

# 获取/varlog目录的上下文
[root@master ~]# salt 'node1' file.get_selinux_context /var/log
node1:
    system_u:object_r:var_log_t:s0
  
# 获取/root/目录的上下文  
[root@master ~]# salt 'node1' file.get_selinux_context /root/
node1:
    system_u:object_r:admin_home_t:s0  
2.7.18 file.get_sum

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

md5
sha1
sha224
sha256 (default)
sha384
sha512

[root@master ~]#  salt 'node1' file.get_sum /etc/httpd/conf/httpd.conf
node1:
    49d17066363644a212d3b3e6874bbfe63d16a107d693d90bacb88d4a90f85ebb
[root@master ~]# salt 'node1' file.get_sum /etc/httpd/conf/httpd.conf md5
node1:
    58b5c2fed6c4f0731b258eff25e7b5e1
[root@master ~]# salt 'node1' file.get_sum /etc/httpd/conf/httpd.conf sha512
node1:
    c51c29687d0c6fd19fb7bbf4e3b61fbdf7c056b92572cb3d0c6a8f9ad58d80b7961c44c595c1351f75ac2e86ce86d978b260ac0d04dfcec20752b971510fcb5a
[root@master ~]# salt 'node1' file.get_sum /etc/httpd/conf/httpd.conf sha224
node1:
    4d4f8f5c7f0a6a369ebb2f896c433fe897ec0bf96e7032d5f45a649b
2.7.19 file.get_uid与file.get_user

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

# 获取/root/test.sh的uid
[root@master ~]# salt 'node1' file.get_uid /root/test.sh
node1:
    1112
   
# 获取/root/test.sh的用户 
[root@master ~]# salt 'node1' file.get_user /root/test.sh
node1:
    alice
   
# 获取/root/test.sh的gid   
[root@master ~]# salt 'node1' file.get_gid /root/test.sh
node1:
    1112
  
# 获取/root/test.sh的组 
[root@master ~]# salt 'node1' file.get_group /root/test.sh
node1:
    harry
2.7.20 file.gid_to_group

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

[root@master ~]# salt 'node1' file.gid_to_group 0
node1:
    root

2.7.21 file.group_to_gid

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

[root@master ~]# salt '*' file.group_to_gid root
node1:
    0
    
[root@master ~]# salt '*' file.group_to_gid tom
node1:
    1003
2.7.22 file.grep

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

salt '*' file.grep /etc/passwd nobody
salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr -- -i
salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr -- -i -B2
salt '*' file.grep "/etc/sysconfig/network-scripts/*" ipaddr -- -i -l

# 在/etc/passwd文件中查找以root开头的
[root@master ~]#  salt 'node1' file.grep /etc/passwd "^root"
node1:
    ----------
    pid:
        267952
    retcode:
        0
    stderr:
    stdout:
        root:x:0:0:root:/root:/bin/bash
        
# 查找指定文件中的ipaddr 忽略大小写
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i
node1:
    ----------
    pid:
        323676
    retcode:
        0
    stderr:
    stdout:
        IPADDR=192.168.235.172
# 查找指定文件中的ipaddr 忽略大小写更详细显示        
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -il
node1:
    ----------
    pid:
        324061
    retcode:
        0
    stderr:
    stdout:
        /etc/sysconfig/network-scripts/ifcfg-ens33  
        
  
 # 查找指定文件中的ipaddr 忽略大小写并显示后两行
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i -A2
node1:
    ----------
    pid:
        326627
    retcode:
        0
    stderr:
    stdout:
        IPADDR=192.168.220.10
        GATEWAY=192.168.220.2
        NETMASK=255.255.255.0
 
 # 查找指定文件中的ipaddr 忽略大小写并显示前两行
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i -B2
node1:
    ----------
    pid:
        327128
    retcode:
        0
    stderr:
    stdout:
        DEVICE=ens33
        ONBOOT=yes
        IPADDR=192.168.220.10
        
# 查找指定文件中的ipaddr 忽略大小写并显示前后两行  
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i -C2
node1:
    ----------
    pid:
        327683
    retcode:
        0
    stderr:
    stdout:
        DEVICE=ens33
        ONBOOT=yes
        IPADDR=192.168.235.172
        GATEWAY=192.168.235.2
        NETMASK=255.255.255.0   
2.7.23 file.is_blkdev

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

[root@master ~]# salt '*' cmd.run 'ls -l /dev/sr0'
node1:
    brw-rw---- 1 root cdrom 11, 0 Nov  4 16:50 /dev/sr0
[root@master ~]# salt '*' file.is_blkdev /dev/sr0
node1:
    True
2.7.24 file.lsattr

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

# 查看属性
[root@master ~]# salt 'node1' cmd.run 'lsattr /etc/passwd'
node1:
    -------------------- /etc/passwd

# 添加属性a
[root@master ~]# salt 'node1'  cmd.run 'chattr +a /etc/passwd'
node1:

# 查看添加结果
[root@master ~]# salt 'node1' cmd.run 'lsattr /etc/passwd'
node1:
    -----a-------------- /etc/passwd
    
# 删除添加的属性a
[root@master ~]# salt 'node1'  cmd.run 'chattr -a /etc/passwd'
node1:

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

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

# 创建目录/opt/hehe 属主为root  属组为alice 权限为755
[root@master ~]# salt 'node1' file.mkdir /opt/hehe root alice 755
node1:
    True
    
# 查看创建的/opt/hehe
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
    total 4
    drwxr-xr-x 3 root root   34 Nov  4 02:19 123
    -rw-r--r-- 1 root root 1023 Nov  4 00:54 anaconda-ks.cfg
    drwxr-xr-x 2 root root    6 Nov  4 03:48 hehe       
2.7.26 file.move

移动或重命名

重命名

[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
    total 12
    drwxr-xr-x. 2 root root   66 Nov  4 02:54 123
    drwxr-xr-x. 2 root root    6 Nov  4 03:38 aaa
    -rw-r--r--. 1 root root   14 Nov  4 02:49 abc
    -rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
    drwxrwxrwx. 2 tom  tom     6 Nov  4 03:38 bbb
    -rw-r--r--. 1 root root   14 Nov  4 02:27 test
[root@master ~]# salt node1 file.move /root/bbb /root/ccc
node1:
    ----------
    comment:
        '/root/bbb' moved to '/root/ccc'
    result:
        True
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
    total 12
    drwxr-xr-x. 2 root root   66 Nov  4 02:54 123
    drwxr-xr-x. 2 root root    6 Nov  4 03:38 aaa
    -rw-r--r--. 1 root root   14 Nov  4 02:49 abc
    -rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
    drwxrwxrwx. 2 tom  tom     6 Nov  4 03:38 ccc
    -rw-r--r--. 1 root root   14 Nov  4 02:27 test    

移动

[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
    total 12
    drwxr-xr-x. 2 root root   66 Nov  4 02:54 123
    drwxr-xr-x. 2 root root    6 Nov  4 03:38 aaa
    -rw-r--r--. 1 root root   14 Nov  4 02:49 abc
    -rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
    drwxrwxrwx. 2 tom  tom     6 Nov  4 03:38 ccc
    -rw-r--r--. 1 root root   14 Nov  4 02:27 test 
[root@master ~]# salt node1 cmd.run 'ls -l /opt'
node1:
    total 0
    drwxr-xr-x. 2 root root 66 Nov  4 02:54 123
[root@master ~]# salt node1 file.move /root/ccc /opt/
node1:
    ----------
    comment:
        '/root/ccc' moved to '/opt/'
    result:
        True
[root@master ~]# salt node1 cmd.run 'ls -l /opt'
node1:
    total 0
    drwxr-xr-x. 2 root root 66 Nov  4 02:54 123
    drwxrwxrwx. 2 tom  tom   6 Nov  4 03:38 ccc

2.7.27 file.prepend

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

# 查看/root/test.sh文本内容
[root@master ~]# salt 'node1' cmd.run 'cat /root/test.sh'
node1:   
    echo "hello world"
    xixi
    hehe
    
# 在/root/tests.h/前面加上内容查看
[root@master ~]# salt 'node1' file.prepend /root/test.sh xixi hehe
node1:
    Prepended 2 lines to "/root/test.sh"

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

修改文本文件的内容

# 查看,将xixi改为jjyy
[root@master ~]# salt 'node1' cmd.run 'cat /root/test.sh'
node1:
    xixi
    hehe    
    echo "hello world"
    xixi
    hehe
    
# 执行并查看    
[root@master ~]# salt 'node1' file.sed /root/test.sh xixi jjyy
node1:
    ----------
    pid:
        31777
    retcode:
        0
    stderr:
    stdout:
    
[root@master ~]# salt 'node1' cmd.run 'cat /root/test.sh'
node1:
    jjyy
    hehe
    echo "hello world"
    jjyy
    hehe    
    
# 指定修改
[root@master ~]# salt 'node1' file.sed /root/test.sh jjyy xixi flags=1
node1:
    ----------
    pid:
        42402
    retcode:
        0
    stderr:
    stdout:

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

读取文件内容

[root@master ~]# salt 'node1' file.read '/root/wxy.sh'
node1:
    loveu
    wenwen
     echo "hello world"
     yaya
     huanhuan
2.7.30 file.readdir

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

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

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

# 创建递归目录查看
[root@master ~]# salt 'node1' cmd.run 'mkdir -p a/b/c/d/e/f'
node1:

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

[root@master ~]# salt 'node1' cmd.run 'ls /root'
node1:
    anaconda-ks.cfg
    test.sh
    
# 创建文件后删除
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 12
    -rw-------. 1 root  root  1023 Nov  4 01:58 anaconda-ks.cfg
    -rw-r--r--  1 root  root     0 Nov  5 04:15 hello
    -rw-r--r--  1 alice harry   39 Nov  5 04:07 test.sh
    
[root@master ~]# salt 'node1' file.remove /root/hello
node1:
    True
    
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 12
    -rw-------. 1 root  root  1023 Nov  4 01:58 anaconda-ks.cfg
    -rw-r--r--  1 alice harry   39 Nov  5 04:07 test.sh   

2.7.32 file.rename

重命名文件或目录

[root@master ~]# salt 'node1' cmd.run 'ls /root'
node1:
    anaconda-ks.cfg
    hh
    test.sh.bak
    wxy
    wxy.sh
    yaya
[root@master ~]# salt 'node1' file.rename /root/wxy.sh /root/hyhy.shnode1:
    True
[root@master ~]# salt 'node1' cmd.run 'ls /root'
node1:
    anaconda-ks.cfg
    hh
    hyhy.sh
    test.sh.bak
    wxy
    yaya

2.7.33 file.set_mode

给指定文件设置权限

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

2.7.34 file.symlink

给指定的文件创建软链接

[root@master ~]#  salt 'node1' cmd.run 'ls -l /root'
node1:
    total 16
    -rw-------. 1 root root 1244 Nov  1 16:27 anaconda-ks.cfg
    -rw-r--r--  1 root root   13 Nov  4 18:32 hh
    -rw-r--r--  1 root root   50 Nov  7 09:58 test.sh.bak
    -rwxr-xr-x  1 root root   49 Nov  7 09:59 wxy.sh
    drwxr-xr-x  2 tom  tom     6 Nov  4 18:29 yaya
[root@master ~]# salt 'node1' file.symlink /root/wxy.sh /root/wxy
node1:
    True
[root@master ~]#  salt 'node1' cmd.run 'ls -l /root'
node1:
    total 16
    -rw-------. 1 root root 1244 Nov  1 16:27 anaconda-ks.cfg
    -rw-r--r--  1 root root   13 Nov  4 18:32 hh
    -rw-r--r--  1 root root   50 Nov  7 09:58 test.sh.bak
    lrwxrwxrwx  1 root root   12 Nov  7 10:35 wxy -> /root/wxy.sh
    -rwxr-xr-x  1 root root   49 Nov  7 09:59 wxy.sh
    drwxr-xr-x  2 tom  tom     6 Nov  4 18:29 yaya

2.7.35 file.touch

创建空文件或更新时间戳

# 查看并创建一个文件在/root下
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 8
    -rw-------. 1 root  root  1023 Nov  4 01:58 anaconda-ks.cfg
    -rwxr-xr-x  1 alice harry   39 Nov  5 04:07 hehe.sh
    lrwxrwxrwx  1 root  root    13 Nov  5 04:24 hello -> /root/hehe.sh
 
# 创建查看 
 [root@master ~]# salt 'node1' file.touch /root/word
node1:
    True
    
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 8
    -rw-------. 1 root  root  1023 Nov  4 01:58 anaconda-ks.cfg
    -rwxr-xr-x  1 alice harry   39 Nov  5 04:07 hehe.sh
    lrwxrwxrwx  1 root  root    13 Nov  5 04:24 hello -> /root/hehe.sh
    -rw-r--r--  1 root  root     0 Nov  5 04:27 word   
    
 # 对一个已经存在的文件更新时间戳
 # 查看时间
 [root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 8
    -rw-------. 1 root  root  1023 Nov  4 01:58 anaconda-ks.cfg
    -rwxr-xr-x  1 alice harry   39 Nov  5 04:07(时间) hehe.sh
    lrwxrwxrwx  1 root  root    13 Nov  5 04:24 hello -> /root/hehe.sh
    -rw-r--r--  1 root  root     0 Nov  5 04:27 word
  
# 更新时间戳/root/hehe.sh  
[root@master ~]# salt 'node1' file.touch /root/hehe.sh
node1:
    True
 
# 查看 
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 8
    -rw-------. 1 root  root  1023 Nov  4 01:58 anaconda-ks.cfg
    -rwxr-xr-x  1 alice harry   39 Nov  5 04:29(时间更新) hehe.sh
    lrwxrwxrwx  1 root  root    13 Nov  5 04:24 hello -> /root/hehe.sh
    -rw-r--r--  1 root  root     0 Nov  5 04:27 word
2.7.36 file.uid_to_user

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

[root@master ~]# salt 'node1' file.user_to_uid tom
node1:
    1002
[root@master ~]#  salt 'node1' file.user_to_uid root
node1:
    0

2.7.38 file.write

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


[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 20
    -rw-------. 1 root root 1244 Nov  1 16:27 anaconda-ks.cfg
    -rw-r--r--  1 root root   13 Nov  4 18:32 hh
    -rwxr-xr-x  1 root root   49 Nov  7 09:59 hyhy.sh
    -rw-r--r--  1 root root   50 Nov  7 09:58 test.sh.bak
    lrwxrwxrwx  1 root root   12 Nov  7 10:35 wxy -> /root/wxy.sh
    -rw-r--r--  1 root root   14 Nov  7 11:17 wxy.sh
    drwxr-xr-x  2 tom  tom     6 Nov  4 18:29 yaya
[root@master ~]# salt 'node1' file.write /root/lhh "xixi" "haha" "wxy"
node1:
    Wrote 3 lines to "/root/lhh"
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 24
    -rw-------. 1 root root 1244 Nov  1 16:27 anaconda-ks.cfg
    -rw-r--r--  1 root root   13 Nov  4 18:32 hh
    -rwxr-xr-x  1 root root   49 Nov  7 09:59 hyhy.sh
    -rw-r--r--  1 root root   14 Nov  7 11:18 lhh
    -rw-r--r--  1 root root   50 Nov  7 09:58 test.sh.bak
    lrwxrwxrwx  1 root root   12 Nov  7 10:35 wxy -> /root/wxy.sh
    -rw-r--r--  1 root root   14 Nov  7 11:17 wxy.sh
    drwxr-xr-x  2 tom  tom     6 Nov  4 18:29 yaya

[root@master ~]# salt 'node1' file.read  '/root/lhh'
node1:
    xixi
    haha
    wxy
 覆盖写入刚创建的/root/lhh文件
[root@master ~]# salt 'node1' file.write /root/lhh  'loveu'
node1:
    Wrote 1 lines to "/root/lhh"
[root@master ~]# salt 'node1' file.read  '/root/lhh'
node1:
    loveu

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值