SaltStack常用的模块

目录
  • 1. SaltStack模块介绍
  • 2. SaltStack常用模块
    • 2.1 SaltStack常用模块之network
      • 2.1.1 network.active_tcp
      • 2.1.2 network.calc_net
      • 2.1.3 network.connect
      • 2.1.4 network.default_route
      • 2.1.5 network.get_fqdn
      • 2.1.6 network.get_hostname
      • 2.1.7 network.get_route
      • 2.1.8 network.hw_addr
      • 2.1.9 network.ifacestartswith
      • 2.1.10 network.in_subnet
      • 2.1.11 network.interface
      • 2.1.12 network.interface_ip
      • 2.1.13 network.interfaces
      • 2.1.14 network.ip_addrs
      • 2.1.15 network.netstat
      • 2.1.16 network.ping
      • 2.1.17 network.reverse_ip
    • 2.2 SaltStack常用模块之service
      • 2.2.1 service.available
      • 2.2.2 service.get_all
      • 2.2.3 service.disabled
      • 2.2.4 service.enabled
      • 2.2.5 service.enable
      • 2.2.6 service.disable
    • 2.3 SaltStack常用模块之pkg
      • 2.3.1 pkg.download
      • 2.3.2 pkg.file_list
      • 2.3.3 pkg.group_info
      • 2.3.4 pkg.install
      • 2.3.5 pkg.list_downloaded
    • 2.4 SaltStack常用模块之state
      • 2.4.1 state.show_highstate
      • 2.4.2 state.highstate
      • 2.4.3 state.show_state_usage
      • 2.4.4 state.show_top
      • 2.4.5 state.show_sls
    • 2.5 SaltStack常用模块之salt-cp
    • 2.6 SaltStack常用模块之file
      • 2.6.1 file.access
      • 2.6.2 file.append
      • 2.6.3 file.basename
      • 2.6.4 file.dirname
      • 2.6.5 file.check_hash
      • 2.6.6 file.chattr
      • 2.6.7 file.chown
      • 2.6.8 file.copy
      • 2.6.9 file.ditectory_exists
      • 2.6.10 file.diskusage
      • 2.6.11 file.file_exists
      • 2.6.12 file.find
      • 2.6.13 file.get_gid
      • 2.6.14 file.get_group
      • 2.6.15 file.get_hash
      • 2.6.16 file.get_mode
      • 2.6.17 file.get_selinux_context
      • 2.6.18 file.get_sum
      • 2.6.19 file.get_uid与file.get_user
      • 2.6.20 file.gid_to_group
      • 2.6.21 file.group_to_gid
      • 2.6.22 file.grep
      • 2.6.23 file.is_blkdev
      • 2.6.24 file.lsattr
      • 2.6.25 file.mkdir
      • 2.6.26 file.move
      • 2.6.27 file.prepend
      • 2.6.28 file.sed
      • 2.6.29 file.read
      • 2.6.30 file.readdir

1. SaltStack模块介绍

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

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

//查看所有module列表
[root@node1 ~]# salt 'node1*' sys.list_modules
node1:
    - acl
    - aliases
    - alternatives
    - ansible
    - archive
    - artifactory
    - beacons
    - bigip
    - btrfs
    - buildout
    - chroot
    - cloud
    - cmd
    - composer
    - config
    - consul
...

//查看指定module的所有function
[root@node1 ~]# 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@node1 ~]# 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.

    CLI Example:

        salt '*' cmd.exec_code ruby 'puts "cheese"' 
...

2. SaltStack常用模块

2.1 SaltStack常用模块之network

2.1.1 network.active_tcp

[root@node1 ~]# salt '*' network.active_tcp
node1:
    ----------
    0:
        ----------
        local_addr:
            192.168.136.164
        local_port:
            4505
        remote_addr:
            192.168.136.164
        remote_port:
            41790
    1:
        ----------
        local_addr:
            192.168.136.164
        local_port:
            4505
        remote_addr:
            192.168.136.165
        remote_port:
            56486
    2:
        ----------
        local_addr:
            192.168.136.164
        local_port:
            41790
        remote_addr:
            192.168.136.164
        remote_port:
            4505
    3:
        ----------
        local_addr:
            192.168.136.164
        local_port:
            22
        remote_addr:
            192.168.136.1
        remote_port:
            52454
node2:
    ----------
    0:
        ----------
        local_addr:
            192.168.136.165
        local_port:
            56486
        remote_addr:
            192.168.136.164
        remote_port:
            4505
    1:
        ----------
        local_addr:
            192.168.136.165
        local_port:
            22
        remote_addr:
            192.168.136.1
        remote_port:
            52458

2.1.2 network.calc_net

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

[root@node1 ~]# salt '*' network.calc_net 192.168.136.164 255.255.255.0
node2:
    192.168.136.0/24
node1:
    192.168.136.0/24

2.1.3 network.connect

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

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

2.1.4 network.default_route

查看默认路由

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

2.1.5 network.get_fqdn

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

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

2.1.6 network.get_hostname

获取主机名

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

2.1.7 network.get_route

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

[root@node1 ~]# salt '*' network.get_route 192.168.136.164
node2:
    ----------
    destination:
        192.168.136.164
    gateway:
        None
    interface:
        ens33
    source:
        192.168.136.165
node1:
    ----------
    destination:
        192.168.136.164
    gateway:
        None
    interface:
        lo
    source:
        192.168.136.164

2.1.8 network.hw_addr

返回指定网卡的MAC地址

[root@node1 ~]# salt '*' network.hw_addr ens33
node2:
    00:0c:29:00:52:6e
node1:
    00:0c:29:26:a4:52

2.1.9 network.ifacestartswith

从特定CIDR检索接口名称

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

2.1.10 network.in_subnet

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

[root@node1 ~]# salt '*' network.in_subnet 192.168.136.0/24
node2:
    True
node1:
    True

2.1.11 network.interface

返回指定网卡的信息

[root@node1 ~]# salt '*' network.interface ens33
node1:
    |_
      ----------
      address:
          192.168.136.164
      broadcast:
          192.168.136.255
      label:
          ens33
      netmask:
          255.255.255.0
node2:
    |_
      ----------
      address:
          192.168.136.165
      broadcast:
          192.168.136.255
      label:
          ens33
      netmask:
          255.255.255.0

2.1.12 network.interface_ip

返回指定网卡的IP地址

[root@node1 ~]# salt '*' network.interface_ip ens33
node2:
    192.168.136.165
node1:
    192.168.136.164

2.1.13 network.interfaces

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

[root@node1 ~]# salt '*' network.interfaces
node1:
    ----------
    ens33:
        ----------
        hwaddr:
            00:0c:29:26:a4:52
        inet:
            |_
              ----------
              address:
                  192.168.136.164
              broadcast:
                  192.168.136.255
              label:
                  ens33
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
              address:
                  fe80::ff7a:d77e:ff2:64bd
              prefixlen:
                  64
              scope:
                  link
        up:
            True
    ens38:
        ----------
        hwaddr:
            00:0c:29:26:a4:5c
        up:
            True
    ens39:
        ----------
        hwaddr:
            00:0c:29:26:a4:66
        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
node2:
    ----------
    ens33:
        ----------
        hwaddr:
            00:0c:29:00:52:6e
        inet:
            |_
              ----------
              address:
                  192.168.136.165
              broadcast:
                  192.168.136.255
              label:
                  ens33
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
              address:
                  fe80::cb81:16ba:de26:872d
              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@node1 ~]# salt '*' network.ip_addrs
node2:
    - 192.168.136.165
node1:
    - 192.168.136.164

2.1.15 network.netstat

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

[root@node1 ~]# salt '*' network.netstat
node2:
    |_
      ----------
      inode:
          19461
      local-address:
          0.0.0.0:22
      program:
          1005/sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          0.0.0.0:*
      send-q:
          0
      state:
          LISTEN
      user:
          0
....

2.1.16 network.ping

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

[root@node1 ~]# salt '*' network.ping baidu.com
node1:
    PING baidu.com (220.181.38.148) 56(84) bytes of data.
    64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=128 time=36.9 ms
    64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=128 time=92.2 ms
    64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=3 ttl=128 time=42.6 ms
    64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=4 ttl=128 time=98.2 ms
    
    --- baidu.com ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3006ms
    rtt min/avg/max/mdev = 36.962/67.500/98.216/27.863 ms
node2:
    PING baidu.com (39.156.69.79) 56(84) bytes of data.
    64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=128 time=36.8 ms
    64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=128 time=92.1 ms
    64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=128 time=42.5 ms
    64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=4 ttl=128 time=94.8 ms
    
    --- baidu.com ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3006ms
    rtt min/avg/max/mdev = 36.842/66.605/94.882/27.003 ms

2.1.17 network.reverse_ip

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

[root@node1 ~]# salt '*' network.reverse_ip 192.168.136.164
node2:
    164.136.168.192.in-addr.arpa
node1:
    164.136.168.192.in-addr.arpa

2.2 SaltStack常用模块之service

2.2.1 service.available

判断指定的服务是否可用

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

2.2.2 service.get_all

获取所有正在运行的服务

[root@node1 ~]# salt '*' service.get_all
node1:
    - NetworkManager
    - NetworkManager-dispatcher
    - NetworkManager-wait-online
    - arp-ethers
    - auditd
    - autovt@
    - basic.target
    - blk-availability
    - bluetooth.target
    - brandbot
    - brandbot.path
    - chrony-dnssrv@
    - [email protected]
    - chrony-wait
    - chronyd
    - console-getty
    - console-shell
    - container-getty@
    - cpupower
    - crond
    - cryptsetup-pre.target
    - cryptsetup.target
    - ctrl-alt-del.target
...

2.2.3 service.disabled

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

[root@node1 ~]# salt '*' service.disabled firewalld
node1:
    True
node2:
    True

2.2.4 service.enabled

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

[root@node1 ~]# salt '*' service.enabled firewalld
node2:
    False
node1:
    False

2.2.5 service.enable

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

[root@node1 ~]# salt '*' service.enable firewalld
node2:
    True
node1:
    True

2.2.6 service.disable

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

[root@node1 ~]# salt '*' service.disable firewalld
node2:
    True
node1:
    True

2.3 SaltStack常用模块之pkg

2.3.1 pkg.download

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

[root@node1 ~]# salt '*' pkg.download wget
node2:
    ----------
    wget:
        /var/cache/yum/packages/wget-1.14-15.el7.x86_64.rpm
node1:
    ----------
    wget:
        /var/cache/yum/packages/wget-1.14-15.el7.x86_64.rpm

2.3.2 pkg.file_list

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

[root@node1 ~]# salt '*' pkg.file_list firewalld
node1:
    ----------
    errors:
    files:
        - /etc/dbus-1/system.d/FirewallD.conf
        - /etc/firewalld
        - /etc/firewalld/firewalld.conf
        - /etc/firewalld/helpers
        - /etc/firewalld/icmptypes
        - /etc/firewalld/ipsets
        - /etc/firewalld/lockdown-whitelist.xml
        - /etc/firewalld/services
        - /etc/firewalld/zones
...

2.3.3 pkg.group_info

查看包组的信息

[root@node1 ~]# salt '*' pkg.group_info 'Development Tools'
node2:
    ----------
    conditional:
    default:
        - byacc
        - cscope
        - ctags
        - diffstat
        - doxygen
        - elfutils
        - gcc-gfortran
        - git
        - indent
        - intltool
        - patchutils
...

2.3.4 pkg.install

安装软件

[root@node1 ~]# salt 'node1*' pkg.install httpd
node1:
    ----------
    apr:
        ----------
        new:
            1.4.8-3.el7
        old:
    apr-util:
        ----------
        new:
            1.5.2-6.el7
        old:
    httpd:
        ----------
        new:
            2.4.6-67.el7
        old:
    httpd-tools:
        ----------
        new:
            2.4.6-67.el7
        old:
    mailcap:
        ----------
        new:
            2.1.41-2.el7
        old:

2.3.5 pkg.list_downloaded

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

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

2.4 SaltStack常用模块之state

2.4.1 state.show_highstate

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

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

2.4.2 state.highstate

执行高级状态

[root@node1 base]# salt '*' state.highstate web.apache.apache
node1:
----------
          ID: states
    Function: no.None
      Result: False
     Comment: No Top file or master_tops data matches found. Please see master log for details.
     Changes:   

Summary for node1
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
Total run time:   0.000 ms
node2:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: None
     Comment: The following packages would be installed/updated: httpd
     Started: 19:46:51.313227
    Duration: 1772.822 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: None
     Comment: Service httpd not present; if created in this state run, it would have been started
     Started: 19:46:53.154353
    Duration: 19.465 ms
     Changes:   

Summary for node2
------------
Succeeded: 2 (unchanged=2)
Failed:    0
------------
Total states run:     2
Total run time:   1.792 s

2.4.3 state.show_state_usage

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

[root@node1 base]# salt '*' state.show_state_usage
node2:
    ----------
    base:
        ----------
        count_all:
            2
        count_unused:
            1
        count_used:
            1
        unused:
            - top
        used:
            - web.apache.apache
    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@node1 base]# salt '*' state.show_top
node2:
    ----------
    base:
        - web.apache.apache
node1:
    ----------

2.4.5 state.show_sls

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

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

2.5 SaltStack常用模块之salt-cp

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

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

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

2.6 SaltStack常用模块之file

2.6.1 file.access

检查指定路径是否存在

[root@node1 ~]# salt '*' cmd.run 'ls /usr/src'
node1:
    debug
    group
    kernels
    passwd
    shadow
node2:
    debug
    group
    kernels
    passwd
    shadow
[root@node1 ~]# salt '*' file.access /usr/src/passwd f
node2:
    True
node1:
    True
[root@node1 ~]# salt '*' file.access /usr/src/abc f
node1:
    False
node2:
    False

检查指定文件的权限信息
[root@node1 ~]# salt '*' cmd.run 'ls -l /usr/src/'
node1:
    total 12
    drwxr-xr-x. 2 root root   6 Mar 10  2016 debug
    -rw-r--r--  1 root root 477 Feb 23 19:52 group
    drwxr-xr-x. 2 root root   6 Mar 10  2016 kernels
    -rw-r--r--  1 root root 942 Feb 23 19:51 passwd
    -rw-r--r--  1 root root 636 Feb 23 19:52 shadow
node2:
    total 12
    drwxr-xr-x. 2 root root   6 Mar 10  2016 debug
    -rw-r--r--  1 root root 477 Feb 23 19:52 group
    drwxr-xr-x. 2 root root   6 Mar 10  2016 kernels
    -rw-r--r--  1 root root 942 Feb 23 19:51 passwd
    -rw-r--r--  1 root root 636 Feb 23 19:52 shadow
[root@node1 ~]# salt '*' file.access /usr/src/passwd r
node2:
    True
node1:
    True
[root@node1 ~]# salt '*' file.access /usr/src/passwd w
node2:
    True
node1:
    True
[root@node1 ~]# salt '*' file.access /usr/src/passwd x
node2:
    False
node1:
    False

2.6.2 file.append

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

[root@node1 ~]# salt '*' cmd.run 'ls -l /root/a'
node2:
    -rw-r--r-- 1 root root 0 Feb 23 19:55 /root/a
node1:
    -rw-r--r-- 1 root root 0 Feb 23 19:55 /root/a
[root@node1 ~]# salt '*' file.append /root/a "hello world" "haha" "xixi"
node2:
    Wrote 3 lines to "/root/a"
node1:
    Wrote 3 lines to "/root/a"
[root@node1 ~]# salt '*' cmd.run 'ls -l /root/a'
node2:
    -rw-r--r-- 1 root root 22 Feb 23 19:56 /root/a
node1:
    -rw-r--r-- 1 root root 22 Feb 23 19:56 /root/a
[root@node1 ~]# salt '*' cmd.run 'cat /root/a'
node2:
    hello world
    haha
    xixi
node1:
    hello world
    haha
    xixi

2.6.3 file.basename

获取指定路径的基名

[root@node1 ~]# salt '*' file.basename '/root/zabbix/abc'
node2:
    abc
node1:
    abc

2.6.4 file.dirname

获取指定路径的目录名

[root@node1 ~]# salt '*' file.dirname '/root/zabbix/abc'
node2:
    /root/zabbix
node1:
    /root/zabbix

2.6.5 file.check_hash

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

[root@node1 ~]# salt '*' cmd.run 'md5sum /etc/passwd'
node1:
    22059454a7752f2da99e6342f802d91b  /etc/passwd
node2:
    cc28c4b0bac9d434c61f5b40df8f2f59  /etc/passwd
[root@node1 ~]# salt '*' file.check_hash /etc/passwd 22059454a7752f2da99e6342f802d91b
node1:
    True
node2:
    False

2.6.6 file.chattr

修改指定文件的属性

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

给指定文件添加属性

[root@node1 ~]# salt '*' cmd.run 'lsattr /root'
node1:
    ---------------- /root/anaconda-ks.cfg
    ---------------- /root/nginx-1.16.1.tar.gz
    ---------------- /root/nginx-1.16.1
    ---------------- /root/abc
    ---------------- /root/a
    ---------------- /root/zabbix
node2:
    ---------------- /root/anaconda-ks.cfg
    ---------------- /root/a
[root@node1 ~]# salt '*' file.chattr /root/a operator=add attributes=ai
node1:
    True
node2:
    True
[root@node1 ~]# salt '*' cmd.run 'lsattr /root'
node1:
    ---------------- /root/anaconda-ks.cfg
    ---------------- /root/nginx-1.16.1.tar.gz
    ---------------- /root/nginx-1.16.1
    ---------------- /root/abc
    ----ia---------- /root/a
    ---------------- /root/zabbix
node2:
    ---------------- /root/anaconda-ks.cfg
    ----ia---------- /root/a

2.6.7 file.chown

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

[root@node1 ~]# salt '*' file.chown /root/abc root root
node2:
    File not found
node1:
    None
[root@node1 ~]# salt '*' cmd.run 'ls -l /root/abc'
node1:
    -rw-r--r--. 1 root root 0 Oct 11 18:49 /root/abc
node2:
    ls: cannot access /root/abc: No such file or directory

2.6.8 file.copy

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

拷贝文件

[root@node1 ~]# salt '*' file.copy /root/a /root/cc
node1:
    True
node2:
    True
[root@node1 ~]# salt '*' cmd.run 'ls -l /root'
node1:
    total 1024
    -rw-r--r--  1 root root      22 Feb 23 19:56 a
    -rw-r--r--. 1 root root       0 Oct 11 18:49 abc
    -rw-------. 1 root root    1451 Oct 11 00:40 anaconda-ks.cfg
    -rw-r--r--  1 root root      22 Feb 23 20:04 cc
    drwxr-xr-x. 9 1001 1001     186 Oct 11 18:02 nginx-1.16.1
    -rw-r--r--. 1 root root 1032630 Oct 11 18:00 nginx-1.16.1.tar.gz
    drwxr-xr-x  2 root root      17 Feb 23 19:58 zabbix
node2:
    total 12
    -rw-r--r--  1 root root   22 Feb 23 19:56 a
    -rw-------. 1 root root 1463 Oct 11 02:25 anaconda-ks.cfg
    -rw-r--r--  1 root root   22 Feb 23 20:04 cc

2.6.9 file.ditectory_exists

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

[root@node1 ~]# salt '*' cmd.run 'ls -l /opt'
node1:
    total 1048576
    drwxr-xr-x. 6 root root         54 Oct 11 18:06 nginx-1.16.1
    drwxr-xr-x. 2 root root          6 Oct 11 15:52 sdb
    drwxr-xr-x. 2 root root          6 Oct 11 16:12 sdc
    -rw-r--r--. 1 root root 1073741824 Oct 12 14:49 swap_file
node2:
    total 0
[root@node1 ~]# salt '*' file.directory_exists /opt/sdc
node1:
    True
node2:
    False

2.6.10 file.diskusage

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

[root@node1 ~]# salt '*' cmd.run 'du -sb /opt'
node1:
    1077603922	/opt
node2:
    6	/opt
[root@node1 ~]# salt '*' file.diskusage /opt
node2:
    0
node1:
    1077599630

2.6.11 file.file_exists

判断指定文件是否存在

[root@node1 ~]# salt '*' file.file_exists /root/a
node1:
    True
node2:
    True
[root@node1 ~]# salt '*' file.file_exists /root/zabbix
node2:
    False
node1:
    False

2.6.12 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
path-glob:

*                = match zero or more chars
?                = match any char
[abc]            = match a, b, or c
[!abc] or [^abc] = match anything except a, b, and c
[x-y]            = match chars x through y
[!x-y] or [^x-y] = match anything except chars x through y
{a,b,c}          = match a or b or c

2.6.13 file.get_gid

获取指定文件的gid

[root@node1 ~]# salt '*' cmd.run 'ls -l /root/a'
node2:
    -rw-r--r-- 1 root root 22 Feb 23 19:56 /root/a
node1:
    -rw-r--r-- 1 root root 22 Feb 23 19:56 /root/a
[root@node1 ~]# salt '*' file.get_gid /root/a
node2:
    0
node1:
    0

2.6.14 file.get_group

获取指定文件的组名

[root@node1 ~]# salt '*' cmd.run 'ls -l /root/a'
node1:
    -rw-r--r-- 1 root root 22 Feb 23 19:56 /root/a
node2:
    -rw-r--r-- 1 root root 22 Feb 23 19:56 /root/a
[root@node1 ~]# salt '*' file.get_group /root/a
node1:
    root
node2:
    root

2.6.15 file.get_hash

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

[root@node1 ~]# salt '*' cmd.run 'sha256sum /root/a'
node2:
    11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a  /root/a
node1:
    11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a  /root/a
[root@node1 ~]#  salt '*' file.get_hash /root/a
node2:
    11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a
node1:
    11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a

2.6.16 file.get_mode

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

[root@node1 ~]# salt '*' cmd.run 'ls -l /root/a'
node1:
    -rw-r--r-- 1 root root 22 Feb 23 19:56 /root/a
node2:
    -rw-r--r-- 1 root root 22 Feb 23 19:56 /root/a
[root@node1 ~]# salt '*' file.get_mode /root/a
node2:
    0644
node1:
    0644

2.6.17 file.get_selinux_context

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

[root@node1 ~]# salt '*' cmd.run 'ls -Z /root/abc'
node1:
    -rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 /root/abc
node2:
    ls: cannot access /root/abc: No such file or directory
ERROR: Minions returned with non-zero exit code
[root@node1 ~]# salt '*' file.get_selinux_context /root/abc
node2:
    No selinux context information is available for /root/abc
node1:
    unconfined_u:object_r:admin_home_t:s0
ERROR: Minions returned with non-zero exit code

2.6.18 file.get_sum

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

md5
sha1
sha224
sha256 (default)
sha384
sha512

[root@node1 ~]# salt '*' cmd.run 'sha256sum /root/a'
node1:
    11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a  /root/a
node2:
    11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a  /root/a
[root@node1 ~]# salt '*' file.get_sum /root/a
node2:
    11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a
node1:
    11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a
[root@node1 ~]# salt '*' cmd.run 'md5sum /root/a'
node2:
    671ded4ec86c82a8779c8df17823f810  /root/a
node1:
    671ded4ec86c82a8779c8df17823f810  /root/a
[root@node1 ~]# salt '*' file.get_sum /root/a md5
node1:
    671ded4ec86c82a8779c8df17823f810
node2:
    671ded4ec86c82a8779c8df17823f810

2.6.19 file.get_uid与file.get_user

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

[root@node1 ~]# salt '*' cmd.run 'ls -l /root/a'
node1:
    -rw-r--r-- 1 root root 22 Feb 23 19:56 /root/a
node2:
    -rw-r--r-- 1 root root 22 Feb 23 19:56 /root/a
[root@node1 ~]# salt '*' file.get_uid /root/a
node1:
    0
node2:
    0
[root@node1 ~]# salt '*' file.get_user /root/a
node2:
    root
node1:
    root

2.6.20 file.gid_to_group

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

[root@node1 ~]# salt '*' file.gid_to_group 1000
node1:
    liping
node2:
    1000
[root@node1 ~]# salt '*' file.gid_to_group 0
node2:
    root
node1:
    root

2.6.21 file.group_to_gid

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

[root@node1 ~]# salt '*' file.group_to_gid root
node2:
    0
node1:
    0
[root@node1 ~]# salt '*' file.group_to_gid liping
node1:
    1000
node2:

2.6.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

2.6.23 file.is_blkdev

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

[root@node1 ~]# salt '*' cmd.run 'ls -l /dev/sr0'
node1:
    brw-rw---- 1 root cdrom 11, 0 Feb 23 19:28 /dev/sr0
node2:
    brw-rw---- 1 root cdrom 11, 0 Feb 23 19:28 /dev/sr0
[root@node1 ~]# salt '*' file.is_blkdev /dev/sr0
node2:
    True
node1:

2.6.24 file.lsattr

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

[root@node1 ~]# salt '*' cmd.run 'lsattr /root/a'
node2:
    ----ia---------- /root/a
node1:
    ----ia---------- /root/a
[root@node1 ~]# salt '*' cmd.run 'chattr +i /root/a'
node2:
node1:
[root@node1 ~]# salt '*' file.lsattr /root/a
node1:
    ----------
    /root/a:
        - i
        - a
node2:
    ----------
    /root/a:
        - i
        - a

2.6.25 file.mkdir

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

[root@node1 ~]# salt '*' file.mkdir /root/haha tom tom
node2:
    True
node1:
    True
[root@node1 ~]# salt '*' cmd.run 'ls -l /root/'
node2:
    total 12
    -rw-r--r--  1 root root   22 Feb 23 19:56 a
    -rw-------. 1 root root 1463 Oct 11 02:25 anaconda-ks.cfg
    -rw-r--r--  1 root root   22 Feb 23 20:04 cc
    drwxr-xr-x  2 tom  tom     6 Feb 27 16:48 haha
node1:
    total 1020
    -rw-r--r--  1 root root      22 Feb 23 19:56 a
    -rw-------. 1 root root    1451 Oct 11 00:40 anaconda-ks.cfg
    drwxr-xr-x  2 tom  tom        6 Feb 27 16:48 haha
    drwxr-xr-x. 9 tom  tom      186 Oct 11 18:02 nginx-1.16.1
    -rw-r--r--. 1 root root 1032630 Oct 11 18:00 nginx-1.16.1.tar.gz
    drwxr-xr-x  2 root root      17 Feb 23 19:58 zabbix

2.6.26 file.move

移动或重命名

[root@node1 ~]# salt 'node1*' cmd.run 'ls -l /root'
node1:
    total 1020
    -rw-r--r--  1 root root      22 Feb 23 19:56 a
    -rw-r--r--  1 root root       0 Feb 27 16:50 ab
    -rw-------. 1 root root    1451 Oct 11 00:40 anaconda-ks.cfg
    drwxr-xr-x  2 tom  tom        6 Feb 27 16:48 haha
    drwxr-xr-x. 9 tom  tom      186 Oct 11 18:02 nginx-1.16.1
    -rw-r--r--. 1 root root 1032630 Oct 11 18:00 nginx-1.16.1.tar.gz
    drwxr-xr-x  2 root root      17 Feb 23 19:58 zabbix
[root@node1 ~]# salt '*' file.move /root/ab /root/bc
node2:
    ERROR: Unable to move '/root/ab' to '/root/bc': [Errno 2] No such file or directory: u'/root/ab'
node1:
    ----------
    comment:
        '/root/ab' moved to '/root/bc'
    result:
        True
ERROR: Minions returned with non-zero exit code
[root@node1 ~]# salt 'node1*' cmd.run 'ls -l /root'
node1:
    total 1020
    -rw-r--r--  1 root root      22 Feb 23 19:56 a
    -rw-------. 1 root root    1451 Oct 11 00:40 anaconda-ks.cfg
    -rw-r--r--  1 root root       0 Feb 27 16:50 bc
    drwxr-xr-x  2 tom  tom        6 Feb 27 16:48 haha
    drwxr-xr-x. 9 tom  tom      186 Oct 11 18:02 nginx-1.16.1
    -rw-r--r--. 1 root root 1032630 Oct 11 18:00 nginx-1.16.1.tar.gz
    drwxr-xr-x  2 root root      17 Feb 23 19:58 zabbix
[root@node1 ~]# salt 'node1*' cmd.run 'ls -l /opt'
node1:
    total 1048576
    drwxr-xr-x. 6 root root         54 Oct 11 18:06 nginx-1.16.1
    drwxr-xr-x. 2 root root          6 Oct 11 15:52 sdb
    drwxr-xr-x. 2 root root          6 Oct 11 16:12 sdc
    -rw-r--r--. 1 root root 1073741824 Oct 12 14:49 swap_file
[root@node1 ~]# salt 'node1*' file.move /root/bc /opt/
node1:
    ----------
    comment:
        '/root/bc' moved to '/opt/'
    result:
        True
[root@node1 ~]# salt 'node1*' cmd.run 'ls -l /opt'
node1:
    total 1048576
    -rw-r--r--  1 root root          0 Feb 27 16:50 bc
    drwxr-xr-x. 6 root root         54 Oct 11 18:06 nginx-1.16.1
    drwxr-xr-x. 2 root root          6 Oct 11 15:52 sdb
    drwxr-xr-x. 2 root root          6 Oct 11 16:12 sdc
    -rw-r--r--. 1 root root 1073741824 Oct 12 14:49 swap_file

2.6.27 file.prepend

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

[root@node1 ~]# salt 'node1*' cmd.run 'cat /opt/bc'
node1:
[root@node1 ~]# salt 'node1*' file.prepend /opt/bc "hehe" "xixi" "haha"
node1:
    Prepended 3 lines to "/opt/bc"
[root@node1 ~]# salt 'node1*' cmd.run 'cat /opt/bc'
node1:
    hehe
    xixi
    haha

2.6.28 file.sed

修改文本文件的内容

[root@node1 ~]# salt 'node1*' cmd.run 'cat /opt/bc'
node1:
    hehe
    xixi
    haha
[root@node1 ~]# salt 'node1*' file.sed /opt/bc 'haha' 'liping'
node1:
    ----------
    pid:
        3391
    retcode:
        0
    stderr:
    stdout:
[root@node1 ~]# salt 'node1*' cmd.run 'cat /opt/bc'
node1:
    hehe
    xixi
    liping

2.6.29 file.read

读取文件内容

[root@node1 ~]# salt 'node1*' cmd.run 'cat /opt/bc'
node1:
    hehe
    xixi
    liping
[root@node1 ~]# salt 'node1*' file.read /opt/bc
node1:
    hehe
    xixi
    liping

2.6.30 file.readdir

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

[root@node1 ~]#  salt 'node1*' file.readdir /root
node1:
    - .
    - ..
    - .bash_logout
    - .bash_profile
    - .bashrc
    - .cshrc
    - .tcshrc
    - anaconda-ks.cfg
    - .bash_history
    - nginx-1.16.1.tar.gz
    - nginx-1.16.1
    - .pki
    - .viminfo
    - a
    - zabbix
    - haha

你可能感兴趣的:(SaltStack常用的模块)