1. 程式人生 > 實用技巧 >SaltStack常用模組

SaltStack常用模組

目錄

SaltStack常用模組

Module是日常使用SaltStack接觸最多的一個元件,其用於管理物件操作,這也是SaltStack通過Push的方式進行管理的入口,比如我們日常簡單的執行命令、檢視包安裝情況、檢視服務執行情況等工作都是通過SaltStack Module來實現的。

當安裝好Master和Minion包後,系統上會安裝很多內建Module,可以通過以下命令檢視支援的所有Module列表:

#檢視所有module列表
[root@localhost ~]# salt '192.168.32.130' sys.list_modules
192.168.32.130:
    - acl
    - aliases
    - alternatives
    - ansible
    - archive
.....省略......
    - virtualenv
    - vsphere
    - x509
    - xfs
    - xml
    - zabbix
    - zenoss


#檢視指定module的所有function
[root@localhost ~]# salt '192.168.32.130' sys.list_functions cmd
192.168.32.130:
    - 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@localhost ~]# salt '192.168.32.130' sys.doc cmd
[root@localhost ~]# salt '192.168.32.130' sys.doc cmd.run
cmd.run:

    Execute the passed command and return the output as a string

    :param str cmd: The command to run. ex: ``ls -lart /home``

    :param str cwd: The directory from which to execute the command. Defaults
        to the home directory of the user specified by ``runas`` (or the user
        under which Salt is running if ``runas`` is not specified).
......




#SaltStack預設也支援一次執行多個Module,Module之間通過逗號隔開,預設傳參之間也是用逗號分隔
[root@localhost ~]# salt '192.168.32.130' cmd.run,test.ping,test.echo 'echo "1,2,3"' , , foo
192.168.32.130:
    ----------
    cmd.run:
        1,2,3
    test.echo:
        foo
    test.ping:
        True
        
#可以使用--args-separator選項更改分隔符:
[root@localhost ~]# salt --args-separator=: '192.168.32.130' cmd.run,test.ping,test.echo 'echo "1,2,3"' :: foo
192.168.32.130:
    ----------
    cmd.run:
        1,2,3
    test.echo:
        foo
    test.ping:
        True

1. network模組

network.active_tcp

返回所有處於活動的tcp連線

[root@salt-master ~]# salt '*' network.active_tcp
192.168.32.135:
    ----------
    0:
        ----------
        local_addr:
            192.168.32.135
        local_port:
            22
        remote_addr:
            192.168.32.1
        remote_port:
            56760
    1:
        ----------
        local_addr:
            192.168.32.135
        local_port:
            51696
        remote_addr:
            192.168.32.130
        remote_port:
            4505

#在192.168.32.135上檢視
[root@node1 ~]# netstat -antl
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0     36 192.168.32.135:22       192.168.32.1:56760      ESTABLISHED
tcp        0      0 192.168.32.135:51696    192.168.32.130:4505     ESTABLISHED
tcp        0      0 192.168.32.135:34548    175.6.241.248:80        TIME_WAIT  
tcp6       0      0 ::1:25                  :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN  



network.arp

返回minion端的arp表

[root@salt-master ~]# salt '*' network.arp
192.168.32.135:
    ----------
    00:0c:29:d7:d9:41:
        192.168.32.130
    00:50:56:c0:00:08:
        192.168.32.1
    00:50:56:ed:c9:8e:
        192.168.32.2

network.calc_net

根據IP地址(支援CIDR表示法)和可選的網路掩碼返回子網的CIDR(無類域間路由)。

[root@salt-master ~]# salt '*' network.calc_net 172.17.0.5 255.255.255.240
192.168.32.135:
    172.17.0.0/28
[root@salt-master ~]# salt '*' network.calc_net 2a02:f6e:a000:80:84d8:8332:7866:4e07/64
192.168.32.135:
    2a02:f6e:a000:80::/64

network.connect

測試minion與某一臺伺服器的特定的埠的連線

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

network.convert_cidr

返回cidr(無類域間路由)地址的網路地址,子網掩碼和廣播地址

[root@salt-master ~]# salt '*' network.convert_cidr 172.31.0.0/16
192.168.32.135:
    ----------
    broadcast:
        172.31.255.255
    netmask:
        255.255.0.0
    network:
        172.31.0.0

network.default_route

從路由表返回預設路由

[root@localhost ~]# salt '*' network.default_route
192.168.32.135:
    |_
      ----------
      addr_family:
          inet
      destination:
          0.0.0.0
      flags:
          UG
      gateway:
          192.168.32.2
      interface:
          ens33
      netmask:
          0.0.0.0

network.dig

在指定被控主機獲取dig、ping、traceroute目錄域名資訊

[root@salt-master ~]# salt '*' network.dig www.qq.com

network.get_bufsize

以字典的形式返回網路緩衝區大小

[root@salt-master ~]# salt '*' network.get_bufsize ens33
192.168.32.135:
    ----------
    result:
        True
    rx:
        256
    rx-jumbo:
        0
    rx-jumbo-max:
        0
    rx-max:
        4096
    rx-mini:
        0
    rx-mini-max:
        0
    tx:
        256
    tx-max:
        4096

network.get_fqdn

檢視主機的fqdn(完全限定域名)

[root@salt-master ~]# salt '*' network.get_fqdn
192.168.32.135:
    node1

network.get_hostname

獲取主機名

[root@salt-master ~]# salt '*' network.get_hostname
192.168.32.135:
    node1

network.get_route

目標IP的路由資訊

[root@salt-master ~]# salt '*' network.get_route 10.10.10.10
192.168.32.135:
    ----------
    destination:
        10.10.10.10
    gateway:
        192.168.32.2
    interface:
        ens33
    source:
        192.168.32.135

network.hw_addr

返回指定網絡卡的mac地址

network.hw_addr的別名network.hwaddr

[root@salt-master ~]# salt '*' network.hw_addr ens33
192.168.32.135:
    00:0c:29:26:1e:fc

[root@salt-master ~]# salt '*' network.hwaddr ens33
192.168.32.135:
    00:0c:29:26:1e:fc

network.ifacestartswith

從特定CIDR檢索介面名稱

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

network.in_subnet

如果主機在指定的子網內,則返回True,否則返回False。

[root@salt-master ~]# salt '*' network.in_subnet 192.168.32.0/24
192.168.32.135:
    True
[root@salt-master ~]# salt '*' network.in_subnet 10.0.0.0/16
192.168.32.135:
    False

network.interface

返回指定網絡卡介面的資訊

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

network.interface_ip

返回指定網絡卡的ip

[root@salt-master ~]# salt '*' network.interface_ip ens33
192.168.32.135:
    192.168.32.135

network.interfaces

返回所有網絡卡資訊

[root@salt-master ~]# salt '*' network.interfaces
192.168.32.135:
    ----------
    ens33:
        ----------
        hwaddr:
            00:0c:29:26:1e:fc
        inet:
            |_
              ----------
              address:
                  192.168.32.135
              broadcast:
                  192.168.32.255
              label:
                  ens33
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
              address:
                  fe80::20c:29ff:fe26:1efc
              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地址列表。 除非指示'include_loopback = True',否則將忽略127.0.0.1。 如果提供“介面”,則僅返回該介面的IP地址。 通過'cidr =“ 10.0.0.0/8”'提供CIDR將僅返回該子網內的地址。 如果“型別”為“公共”,則僅返回公共地址。 如'type'='private'。

[root@salt-master ~]# salt '*' network.ip_addrs
192.168.32.135:
    - 192.168.32.135
[root@salt-master ~]# salt '*' network.ip_addrs 'cidr=192.168.32.0/24'
192.168.32.135:
    - 192.168.32.135
[root@salt-master ~]# salt '*' network.ip_addrs 'cidr=10.10.10.0/24'
192.168.32.135:

network.ip_addrs6

返回分配給主機的IPv6地址列表,忽略::1,除非新增選項include_loopback = True

也可以指定某個介面

[root@salt-master ~]# salt '*' network.ip_addrs6
192.168.32.135:
    - fe80::20c:29ff:fe26:1efc

network.ip_in_subnet

如果給定IP在指定子網內,則返回True,否則返回False

[root@salt-master ~]# salt '*' network.ip_in_subnet 172.17.0.4 172.16.0.0/12
192.168.32.135:
    True

network.ip_networks

返回minion端的ipv4網段

[root@salt-master ~]# salt '*' network.ip_networks
192.168.32.135:
    - 192.168.32.0/24

返回minion端的ipv6網段

[root@salt-master ~]# salt '*' network.ip_networks6
192.168.32.135:
    - fe80::/64

network.iphexval

返回IP地址的十六進位制表示形式

[root@salt-master ~]# salt '*' network.iphexval 10.0.0.1
192.168.32.135:
    0A000001

network.is_loopback

檢查指定的IP地址是否是環回口地址

[root@salt-master ~]# salt '*' network.is_loopback 127.0.0.1
192.168.32.135:
    True

[root@salt-master ~]# salt '*' network.is_loopback 172.25.16.10
192.168.32.135:
    False

network.is_private

檢查指定的IP地址是否為私有地址

[root@salt-master ~]# salt '*' network.is_private 10.0.0.3
192.168.32.135:
    True

network.mod_hostname

修改minion端的主機名,重啟依然有效

[root@salt-master ~]# salt '*' network.mod_hostname test.mod_hostname
192.168.32.135:
    True
[root@salt-master ~]# salt '*' network.get_hostname
192.168.32.135:
    test.mod_hostname

#改回去
[root@salt-master ~]# salt '*' network.mod_hostname node1
192.168.32.135:
    True

network.netstat

返回有關開啟的埠和狀態的資訊

salt '*' network.netstat
192.168.32.135:
    |_
      ----------
      inode:
          20741
      local-address:
          127.0.0.1:25
      program:
          1096/master
      proto:
          tcp
      recv-q:
          0
      remote-address:
          0.0.0.0:*
      send-q:
          0
      state:
          LISTEN
      user:
          0
.......省略........

network.ping

對主機執行ICMP ping

[root@salt-master ~]# salt '*' network.ping www.baidu.com
192.168.32.135:
    PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data.
    64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=1 ttl=128 time=21.2 ms
    64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=2 ttl=128 time=22.6 ms
    64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=3 ttl=128 time=22.9 ms
    64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=4 ttl=128 time=31.9 ms
    
    --- www.a.shifen.com ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3006ms
    rtt min/avg/max/mdev = 21.252/24.691/31.968/4.248 ms

[root@salt-master ~]# salt '*' network.ping www.baidu.com return_boolean=Ture
192.168.32.135:
    True

[root@salt-master ~]# salt '*' network.ping www.baidu.com timeout=3 return_boolean=Ture
192.168.32.135:
    True

network.reverse_ip

反向返回IP地址

[root@salt-master ~]# salt '*' network.reverse_ip 172.17.0.4
192.168.32.135:
    4.0.17.172.in-addr.arpa

network.routes

從路由表返回當前配置的路由

[root@salt-master ~]# salt '*' network.routes
192.168.32.135:
    |_
      ----------
      addr_family:
          inet
      destination:
          0.0.0.0
      flags:
          UG
      gateway:
          192.168.32.2
      interface:
          ens33
      netmask:
          0.0.0.0
......

network.subnets

返回minion所屬的IPv4子網的列表

可以指定介面

[root@salt-master ~]# salt '*' network.subnets
192.168.32.135:
    - 192.168.32.0/24
[root@salt-master ~]# salt '*' network.subnets interfaces=ens33
192.168.32.135:
    - 192.168.32.0/24

network.subnets6

返回minion所屬的IPv6子網的列表

[root@salt-master ~]# salt '*' network.subnets6
192.168.32.135:
    - fe80::/64

network.traceroute

對指定主機追蹤路由

[root@salt-master ~]# salt '*' network.traceroute www.baidu.com

2. service模組

service.available

檢查給定服務是否可用

[root@salt-master ~]# salt '*' service.available sshd
192.168.32.135:
    True

service.disable

禁止服務開機自啟

[root@salt-master ~]# salt '*' service.disable postfix
192.168.32.135:
    True

service.disabled

檢測訪服務是否是禁止開機自啟的

[root@salt-master ~]# salt '*' service.disabled postfix
192.168.32.135:
    True

service.enable

讓服務開機自啟

[root@salt-master ~]# salt '*' service.enable postfix
192.168.32.135:
    True

service.enabled

檢測訪服務是否是開機自啟的

[root@salt-master ~]# salt '*' service.enabled postfix
192.168.32.135:
    True

service.execs

返回為服務指定的檔案列表

[root@salt-master ~]# salt '*' service.execs 
192.168.32.135:
    ----------
    NetworkManager:
        /usr/sbin/NetworkManager
    NetworkManager-dispatcher:
        /usr/libexec/nm-dispatcher
    NetworkManager-wait-online:
        /usr/bin/nm-online
[root@salt-master ~]# salt '*' service.execs | grep chronyd
    chronyd:
        /usr/sbin/chronyd

service.firstboot

呼叫systemd-firstboot來配置系統的基本設定

salt.modules.systemd_service.firstboot (locale=None, locale_message=None, keymap=None, timezone=None, hostname=None, machine_id=None, root=None)

locale

設定主要語言環境 (LANG=)

locale_message

設定訊息語言環境 (LC_MESSAGES=)

keymap

設定鍵對映

timezone

設定時區

hostname

設定主機名

machine_id

設定機器id

root

在備用檔案系統根目錄上操作

[root@salt-master ~]# salt '*' service.firstboot locale=en_US.UTF-8
192.168.32.135:
    True

service.force_reload

使用systemd強制重新載入指定的服務

[root@salt-master ~]# salt '*' service.force_reload postfix
192.168.32.135:
    True

service.get_all

返回所有可用服務的列表

[root@salt-master ~]# salt '*' service.get_all
192.168.32.135:
    - NetworkManager
    - NetworkManager-dispatcher
    - NetworkManager-wait-online
    - arp-ethers
    - auditd
    - autovt@
    - basic.target

service.get_disabled

返回所有禁止開機自啟服務的列表

[root@salt-master ~]# salt '*' service.get_disabled
192.168.32.135:
    - arp-ethers
    - blk-availability
    - brandbot.path
    - [email protected]
    - chrony-wait
    - console-getty
    - console-shell

service.get_enabled

返回所有開機自啟服務的列表

[root@salt-master ~]# salt '*' service.get_enabled
192.168.32.135:
    - NetworkManager
    - NetworkManager-dispatcher
    - NetworkManager-wait-online
    - auditd
    - autovt@
    - chronyd
......
[root@salt-master ~]# salt '*' service.get_enabled | grep httpd
    - httpd

service.get_running

就systemd而言,返回所有正在執行的服務的列表

[root@salt-master ~]# salt '*' service.get_running
192.168.32.135:
    - NetworkManager
    - auditd
    - chronyd
    - crond
    - dbus
    - dbus.socket
    - getty@tty1
    - lvm2-lvmetad
    - lvm2-lvmetad.socket
    - polkit
    - rsyslog
    - salt-minion
    - sshd
......

service.get_static

返回所有靜態服務的列表

[root@salt-master ~]# salt '*' service.get_static
192.168.32.135:
    - basic.target
    - bluetooth.target
    - brandbot
    - chrony-dnssrv@
    - container-getty@

service.mask

使用systemd遮蔽指定的服務,加上runtime=True表示僅在下去次重啟前生效

為了避免minion端修改服務時重啟服務造成資源競爭

[root@salt-master ~]# salt '*' service.mask postfix
192.168.32.135:
    True

service.masked

檢查服務是否被遮蔽

[root@salt-master ~]# salt '*' service.masked postfix
192.168.32.135:
    True

service.missing

檢測服務是否可用(能通過systemd啟動的)

可用返回False,不可用返回Ture

[root@salt-master ~]# salt '*' service.missing sshd
192.168.32.135:
    False
[root@salt-master ~]# salt '*' service.missing vsftpd
192.168.32.135:
    True

service.reload

重新載入服務,訪問必須正在執行

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

service.restart

重啟服務

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

service.show

顯示服務的一些屬性

[root@salt-master ~]# salt '*' service.show httpd
192.168.32.135:
    ----------
    ActiveEnterTimestamp:
        Wed 2020-08-19 11:56:25 CST
    ActiveEnterTimestampMonotonic:
        8504383957
    ActiveExitTimestampMonotonic:
        0
    ActiveState:
        active

service.start

使用systemd啟動指定的服務

[root@salt-master ~]# salt '*' service.start sshd
192.168.32.135:
    True

service.status

通過systemd返回服務的狀態,正在執行返回Ture,否則返回False

[root@salt-master ~]# salt '*' service.status ssh*
192.168.32.135:
    ----------
    sshd:
        True
    sshd-keygen:
        False
    sshd.socket:
        False
    sshd@:
        False

service.stop

使用systemd停止指定的服務

[root@salt-master ~]# salt '*' service.stop  httpd
192.168.32.135:
    True

service.systemctl_reload

重新載入systemctl,等同於systemctl daemon-reload

[root@salt-master ~]# salt '*' service.systemctl_reload
192.168.32.135:
    True

service.unmask

使用systemd取消遮蔽指定的服務(與service.mask、service.masked連用)

[root@salt-master ~]# salt '*' service.unmask postfix
192.168.32.135:
    True

3. pkg模組

pkg.download

只下載軟體包但不安裝
此功能將會下載指定的軟體包,但是需要在minion端安裝yum-utils,可以使用 cmd.run 進行遠端安裝

[root@salt-master ~]# salt '*' pkg.download httpd
192.168.32.135:
    ----------
    httpd:
        /var/cache/yum/packages/httpd-2.4.6-93.el7.centos.x86_64.rpm
[root@salt-master ~]# salt '*' cmd.run 'ls /var/cache/yum/packages'
192.168.32.135:
    httpd-2.4.6-93.el7.centos.x86_64.rpm

pkg.file_list

列出指定包或系統中已安裝的所有包的檔案

[root@salt-master ~]# salt '*' pkg.file_list httpd
192.168.32.135:
    ----------
    errors:
    files:
        - /etc/httpd
        - /etc/httpd/conf
        - /etc/httpd/conf.d
        - /etc/httpd/conf.d/README
......

pkg.group_info

列出包組的資訊

[root@salt-master ~]# salt '*' pkg.group_info 'Development Tools'
192.168.32.135:
    ----------
    conditional:
    default:
        - byacc
        - cscope
        - ctags
        - diffstat
        - doxygen
        - elfutils
        - gcc-gfortran
        - git
        - indent
        - intltool
        - patchutils
        - rcs
        - subversion
        - swig
        - systemtap
    description:
        A basic development environment.
    group:
        Development Tools
    id:
        development
    mandatory:
        - autoconf
        - automake
        - binutils
        - bison
        - flex
        - gcc
        - gcc-c++
        - gettext
        - libtool
        - make
        - patch
        - pkgconfig
        - redhat-rpm-config
        - rpm-build
        - rpm-sign
    optional:
        - ElectricFence
        - ant
        - babel
        - bzr
        - ccache
        - chrpath
        - clips
        - clips-devel
        - clips-doc
        - clips-emacs
        - clips-xclips
        - clipsmm-devel
        - clipsmm-doc
        - cmake
        - cmucl
        - colordiff
        - compat-gcc-44
        - compat-gcc-44-c++
        - cvs
        - cvsps
        - darcs
        - dejagnu
        - email2trac
        - expect
        - ftnchek
        - gcc-gnat
        - gcc-objc
        - gcc-objc++
        - ghc
        - git
        - haskell-platform
        - imake
        - javapackages-tools
        - ksc
        - lua
        - mercurial
        - mock
        - mod_dav_svn
        - nasm
        - nqc
        - nqc-doc
        - ocaml
        - perltidy
        - qgit
        - rpmdevtools
        - rpmlint
        - sbcl
        - scorep
        - systemtap-sdt-devel
        - systemtap-server
        - trac
        - trac-git-plugin
        - trac-mercurial-plugin
        - trac-webadmin
        - translate-toolkit
    type:
        package group

pkg.group_list

列出系統中的包組

[root@salt-master ~]# salt '*' pkg.group_list
192.168.32.135:
    ----------
    available:
        - Additional Development
        - Anaconda Tools
        - Backup Client
        - Backup Server
        - Base
        - Buildsystem building group
        - CentOS Linux Client product core
        - CentOS Linux ComputeNode product core
        - CentOS Linux Server product core
        - CentOS Linux Workstation product core
        - Cinnamon
        - Common NetworkManager submodules
        - Compatibility Libraries
        - Conflicts (Client)
        - Conflicts (ComputeNode)
        - Conflicts (Server)
        - Conflicts (Workstation)
        - Console Internet Tools
        - Core
        - DNS Name Server
        - Debugging Tools
        - Desktop Debugging and Performance Tools
        - Development Tools
        - Dial-up Networking Support
        - Directory Client
        - Directory Server
        - E-mail Server
        - Educational Software
        - Electronic Lab
        - Emacs
        - FTP Server
        - Fedora Packager
        - File and Storage Server
        - Fonts
        - GNOME
        - GNOME Applications
        - General Purpose Desktop
        - Graphical Administration Tools
        - Graphics Creation Tools
        - Guest Agents
        - Guest Desktop Agents
        - Hardware Monitoring Utilities
        - Haskell
        - High Availability
        - Hyper-v platform specific packages
        - Identity Management Server
        - Infiniband Support
        - Input Methods
        - Internet Applications
        - Internet Browser
        - Java Platform
        - KDE
        - KDE Applications
        - KDE Multimedia Support
        - KVM platform specific packages
        - LXQt Desktop
        - Large Systems Performance
        - Legacy UNIX Compatibility
        - Legacy X Window System Compatibility
        - Load Balancer
        - MATE
        - Mainframe Access
        - MariaDB Database Client
        - MariaDB Database Server
        - Milkymist
        - Multimedia
        - Network File System Client
        - Network Infrastructure Server
        - Networking Tools
        - Office Suite and Productivity
        - PHP Support
        - Performance Tools
        - Perl Support
        - Perl for Web
        - Platform Development
        - PostgreSQL Database Client
        - PostgreSQL Database Server
        - Print Server
        - Printing Client
        - Python
        - Remote Desktop Clients
        - Remote Management for Linux
        - Resilient Storage
        - Ruby Support
        - Scientific Support
        - Security Tools
        - Smart Card Support
        - System Administration Tools
        - System Management
        - Technical Writing
        - TurboGears application framework
        - VMware platform specific packages
        - Virtualization Client
        - Virtualization Hypervisor
        - Virtualization Platform
        - Virtualization Tools
        - Web Server
        - Web Servlet Engine
        - X Window System
        - Xfce
    available environments:
        - Minimal Install
        - Compute Node
        - Infrastructure Server
        - File and Print Server
        - Cinnamon Desktop
        - MATE Desktop
        - Basic Web Server
        - Virtualization Host
        - Server with GUI
        - GNOME Desktop
        - KDE Plasma Workspaces
        - Development and Creative Workstation
    available languages:
        ----------
    installed:
    installed environments:

pkg.install

安裝軟體

[root@salt-master ~]# salt '*' pkg.install unzip
192.168.32.135:
    ----------
    unzip:
        ----------
        new:
            6.0-21.el7
        old:

pkg.list_downloaded

列出已下載到本地的軟體包

[root@salt-master ~]# salt '*' pkg.list_downloaded
192.168.32.135:
    ----------
    httpd:
        ----------
        2.4.6-93.el7.centos:
            ----------
            creation_date_time:
                2020-08-19T18:37:24
            creation_date_time_t:
                1597833444
            path:
                /var/cache/yum/packages/httpd-2.4.6-93.el7.centos.x86_64.rpm
            size:
                2843664

pkg.list_pkgs

以字典的方式列出當前已安裝的軟體包

[root@salt-master ~]# salt '*' pkg.list_pkgs
192.168.32.135:
    ----------
    NetworkManager:
        1:1.18.0-5.el7
    NetworkManager-libnm:
        1:1.18.0-5.el7
    NetworkManager-team:
        1:1.18.0-5.el7
    NetworkManager-tui:
        1:1.18.0-5.el7
    acl:
        2.2.51-14.el7
    aic94xx-firmware:
        30-6.el7
......

pkg.owner

列出指定檔案是由哪個包提供的

[root@salt-master ~]# salt '*' pkg.owner /usr/sbin/apachectl /usr/bin/unzip
192.168.32.135:
    ----------
    /usr/bin/unzip:
        unzip
    /usr/sbin/apachectl:
        httpd

pkg.remove

解除安裝軟體

[root@salt-master ~]# salt '*' pkg.remove unzip
192.168.32.135:
    ----------
    unzip:
        ----------
        new:
        old:
            6.0-21.el7

pkg.upgrade

升級系統中所有的軟體包或升級指定的軟體包

[root@salt-master ~]# salt '*' pkg.upgrade name=curl
192.168.32.135:
    ----------
    curl:
        ----------
        new:
            7.29.0-57.el7_8.1
        old:
            7.29.0-54.el7
    libcurl:
        ----------
        new:
            7.29.0-57.el7_8.1
        old:
            7.29.0-54.el7
//若想升級系統中所有的軟體包則把 name 引數去掉即可

4. state模組

state.check_request

返回狀態請求資訊

[root@salt-master ~]# salt '*' state.check_request
192.168.32.135:

Summary for 192.168.32.135
-----------
Succeeded: 0
Failed:   0
-----------
Total states run:    0
Total run time:  0.000 ms

state.show_highstate

顯示當前系統中有哪些高階狀態

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

state.show_state_usage

顯示當前系統中的高階狀態執行情況


state.show_top

返回minion將用於highstate的頂級資料

[root@salt-master salt]# salt '*' state.show_top
192.168.32.135:
    ----------
    base:
        - apache
    dev:
        - nginx

state.show_sls

顯示 master 上特定sls或sls檔案列表中的狀態資料

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

5. salt-cp

salt-cp能夠很方便的把 master 上的檔案批量傳到 minion上

#拷貝單個檔案到目標主機的/usr/src目錄下
[root@salt-master ~]# salt '*' cmd.run 'ls /usr/src/'
192.168.32.135:
    debug
    kernels
[root@salt-master ~]# salt-cp '*' /etc/passwd /usr/src/
192.168.32.135:
    ----------
    /usr/src/passwd:
        True
[root@salt-master ~]# salt '*' cmd.run 'ls /usr/src'
192.168.32.135:
    debug
    kernels
    passwd


#拷貝多個檔案到目標主機的/usr/src目錄下
[root@salt-master ~]# salt-cp '*' /etc/shadow /etc/group /usr/src
192.168.32.135:
    ----------
    /usr/src/group:
        True
    /usr/src/shadow:
        True
[root@salt-master ~]# salt '*' cmd.run 'ls /usr/src'
192.168.32.135:
    debug
    group
    kernels
    passwd
    shadow


6. file模組

file.access

檢查指定路徑是否存在

[root@salt-master ~]# salt '*' cmd.run 'ls /usr/src'
192.168.32.135:
    debug
    group
    kernels
    passwd
    shadow

[root@salt-master ~]# salt '*' file.access /usr/src/abc f
192.168.32.135:
    False

檢查指定檔案的許可權資訊

[root@salt-master ~]# salt '*' cmd.run 'ls -l /usr/src/'
192.168.32.135:
    total 12
    drwxr-xr-x. 2 root root   6 Apr 11  2018 debug
    -rw-r--r--  1 root root 477 Aug 19 20:58 group
    drwxr-xr-x. 2 root root   6 Apr 11  2018 kernels
    -rw-r--r--  1 root root 946 Aug 19 20:57 passwd
    -rw-r--r--  1 root root 604 Aug 19 20:58 shadow
[root@salt-master ~]# salt '*' file.access /usr/src/passwd r 
192.168.32.135:
    True
[root@salt-master ~]# salt '*' file.access /usr/src/passwd w 
192.168.32.135:
    True
[root@salt-master ~]# salt '*' file.access /usr/src/passwd x
192.168.32.135:
    False

file.append

往一個檔案裡追加內容,若此檔案不存在則會報異常

[root@salt-master ~]# salt '*' cmd.run 'ls -l /root/a'
192.168.32.135:
    -rw-r--r-- 1 root root 0 Aug 19 21:04 /root/a
[root@salt-master ~]# salt '*' file.append /root/a "hello world" "world hello"
192.168.32.135:
    Wrote 2 lines to "/root/a"
[root@salt-master ~]# salt '*' cmd.run 'cat /root/a'
192.168.32.135:
    hello world
    world hello

file.basename

獲取指定路徑的基名,等同於basename命令

[root@salt-master ~]# salt '*' file.basename '/usr/local/abc'
192.168.32.135:
    abc

file.dirname

獲取指定路徑的目錄名

[root@salt-master ~]# salt '*' file.dirname '/usr/local/abc'
192.168.32.135:
    /usr/local

file.check_hash

檢查指定的檔案與hash字串是否匹配,匹配則返回 True 否則返回 False

[root@salt-master ~]# salt '*' cmd.run 'md5sum /etc/passwd'
192.168.32.135:
    36020f0d2206eee6a8d1615a0a9d44bd  /etc/passwd
    
[root@salt-master ~]# salt '*' file.check_hash /etc/passwd 36020f0d2206eee6a8d1615a0a9d44bd
192.168.32.135:
    True
    

file.chattr

修改指定檔案的屬性

屬性 對檔案的意義 對目錄的意義
a 只允許在這個檔案之後追加資料, 不允許任何程序覆蓋或截斷這個檔案 只允許在這個目錄下建立和修改檔案, 而不允許刪除任何檔案
i 不允許對這個檔案進行任何的修改, 不能刪除、更改、移動 任何的程序只能修改目錄之下的檔案, 不允許建立和刪除檔案

給指定檔案新增屬性

[root@salt-master ~]# salt '*' file.chattr /root/a operator=add attributes=ai
192.168.32.135:
    True
[root@salt-master ~]# salt '*' cmd.run 'lsattr /root'
192.168.32.135:
    ---------------- /root/anaconda-ks.cfg
    ----ia---------- /root/a

給指定檔案去除屬性

[root@salt-master ~]# salt '*' file.chattr /root/a operator=remove attributes=ai
192.168.32.135:
    True
[root@salt-master ~]# salt '*' cmd.run 'lsattr /root'
192.168.32.135:
    ---------------- /root/anaconda-ks.cfg
    ---------------- /root/a

file.chown

設定指定檔案的屬主、屬組資訊

[root@salt-master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.32.135:
    total 8
    -rw-r--r--  1 root root   24 Aug 19 21:04 a
    -rw-------. 1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
[root@salt-master ~]# salt '*' file.chown /root/a tom tom
192.168.32.135:
    None
[root@salt-master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.32.135:
    total 8
    -rw-r--r--  1 tom  tom    24 Aug 19 21:04 a
    -rw-------. 1 root root 1257 Apr  8 02:56 anaconda-ks.cfg

file.copy

在遠端主機上覆制檔案或目錄

拷貝檔案

[root@salt-master ~]# salt '*' cmd.run 'ls -l /root'
192.168.32.135:
    total 8
    -rw-r--r--  1 tom  tom    24 Aug 19 21:04 a
    -rw-------. 1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
[root@salt-master ~]# salt '*' file.copy /root/a /root/abc
192.168.32.135:
    True
[root@salt-master ~]#  salt '*' cmd.run 'ls -l /root'
192.168.32.135:
    total 12
    -rw-r--r--  1 tom  tom    24 Aug 19 21:04 a
    -rw-r--r--  1 tom  tom    24 Aug 19 21:13 abc
    -rw-------. 1 root root 1257 Apr  8 02:56 anaconda-ks.cfg

覆蓋並拷貝目錄,將會覆蓋同名檔案或目錄

[root@salt-master ~]# salt '*' cmd.run 'ls -l /root'
192.168.32.135:
    total 12
    -rw-r--r--  1 tom  tom    24 Aug 19 21:04 a
    -rw-r--r--  1 tom  tom    24 Aug 19 21:13 abc
    -rw-------. 1 root root 1257 Apr  8 02:56 anaconda-ks.cfg

[root@salt-master ~]#  salt '*' file.copy /tmp/ /root/abcd recurse=True
192.168.32.135:
    True
[root@salt-master ~]# salt '*' cmd.run 'ls -l /root'
192.168.32.135:
    total 16
    -rw-r--r--   1 tom  tom    24 Aug 19 21:04 a
    -rw-r--r--   1 tom  tom    24 Aug 19 21:13 abc
    drwxrwxrwt  24 root root 4096 Aug 19 21:15 abcd
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg

刪除目標目錄中同名的檔案或目錄並拷貝新內容至其中

[root@salt-master ~]# salt '*' cmd.run 'ls -l /root/test'
192.168.32.135:
    total 0
    -rw-r--r-- 1 root root 0 Aug 19 21:19 111
    -rw-r--r-- 1 root root 0 Aug 19 21:19 222
    -rw-r--r-- 1 root root 0 Aug 19 21:19 333
[root@salt-master ~]# salt '*' cmd.run 'ls -l /opt/test'
192.168.32.135:
    total 0
    -rw-r--r-- 1 root root 0 Aug 19 21:19 aaa
    -rw-r--r-- 1 root root 0 Aug 19 21:19 bbb
    -rw-r--r-- 1 root root 0 Aug 19 21:19 ccc
[root@salt-master ~]# salt '*' file.copy /opt/test/ /root/test/ recurse=True remove_existing=True
192.168.32.135:
    True
[root@salt-master ~]# salt '*' cmd.run 'ls -l /root/test'
192.168.32.135:
    total 0
    -rw-r--r-- 1 root root 0 Aug 19 21:19 aaa
    -rw-r--r-- 1 root root 0 Aug 19 21:19 bbb
    -rw-r--r-- 1 root root 0 Aug 19 21:19 ccc

file.ditectory_exists

判斷指定目錄是否存在,存在則返回 True ,否則返回 False

[root@salt-master ~]# salt '*' file.directory_exists /opt/test
192.168.32.135:
    True
[root@salt-master ~]# salt '*' file.directory_exists /opt/test/aaaaaa
192.168.32.135:
    False

file.diskusage

遞迴計算指定路徑的磁碟使用情況並以位元組為單位返回

[root@salt-master ~]# salt '*' cmd.run 'du -sb /etc'
192.168.32.135:
    27980060	/etc
[root@salt-master ~]# salt '*' file.diskusage /etc/
192.168.32.135:
    29498564

file.file_exists

判斷指定檔案是否存在

[root@salt-master ~]# salt '*' file.file_exists /usr/local/
192.168.32.135:
    False
[root@salt-master ~]# salt '*' file.file_exists /root/anaconda-ks.cfg 
192.168.32.135:
    True

file.find

類似 find 命令並返回符合指定條件的路徑列表

匹配條件:

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-regex: a Python Regex (regular expression) pattern to match pathnames

file-types: 包含以下一項或多項的字串

a: all file types
b: block device
c: character device
d: directory
p: FIFO (named pipe)
f: plain file
l: symlink
s: socket

users: 用空格或都好分隔使用者名稱或uid

groups: 用空格或都好分隔使用者名稱或gid

size-unit:

b: bytes
k: kilobytes
m: megabytes
g: gigabytes
t: terabytes

時間間隔:

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

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

示例:

#查詢型別為普通檔案,以.log結尾的,大小在10以上的檔案
[root@salt-master ~]# salt '*' file.find / type=f name=\*.log size=+1m
192.168.32.135:
    - /var/log/anaconda/journal.log


#在var目錄下查詢修改後的時間在10天前,大小在10M以上的檔案,列印路徑,檔案大小,修改後的時間
[root@salt-master ~]# salt '*' file.find /var mtime=+10d size=+10m print=path,size,mtime
192.168.32.135:
    |_
      - /var/cache/yum/x86_64/7/base/gen/primary_db.sqlite
      - 31612928
      - 1588646891
    |_
      - /var/cache/yum/x86_64/7/updates/gen/primary_db.sqlite
      - 19569664
      - 1596824114

#在/var/log下查詢以.加上任意數字結尾的30天前修改的,大於10m的檔案,刪除
[root@salt-master ~]# salt '*' file.find /var/log name=\*.[0-9] mtime=+30d size=+10m delete

file.get_gid

獲取指定檔案的gid

[root@salt-master ~]# salt '*' file.get_gid /etc/passwd
192.168.32.135:
    0

file.get_group

獲取指定檔案的組名

[root@salt-master ~]# salt '*' file.get_group /etc/passwd
192.168.32.135:
    root

file.get_hash

獲取指定檔案的hash值,該值通過 sha256 演算法得來

[root@salt-master ~]# salt '*' cmd.run 'sha256sum /etc/passwd'
192.168.32.135:
    3323e8cb2eae644f3758da7bd37002bf83ef2041ad6edc62f697a69c2d75df55  /etc/passwd
[root@salt-master ~]# salt '*' file.get_hash /etc/passwd
192.168.32.135:
    3323e8cb2eae644f3758da7bd37002bf83ef2041ad6edc62f697a69c2d75df55

file.get_mode

獲取指定檔案的許可權,以數字方式顯示

[root@salt-master ~]# salt '*' cmd.run 'ls -l /etc/passwd'
192.168.32.135:
    -rw-r--r-- 1 root root 998 Aug 19 21:12 /etc/passwd
[root@salt-master ~]# salt '*' file.get_mode /etc/passwd
192.168.32.135:
    0644

file.get_sum

按照指定的演算法計算指定檔案的特徵碼並顯示,預設使用的sha256演算法。
該函式可使用的演算法引數有:

  • md5
  • sha1
  • sha224
  • sha256 (default)
  • sha384
  • sha512
[root@salt-master ~]# salt '*' cmd.run 'sha256sum /root/a'
192.168.32.135:
    ededca6726748c692616b9658e937a8a7badd7437c4272d44f1b9aadf9d1f6d7  /root/a
[root@salt-master ~]# salt '*' file.get_sum /root/a
192.168.32.135:
    ededca6726748c692616b9658e937a8a7badd7437c4272d44f1b9aadf9d1f6d7
[root@salt-master ~]# salt '*' cmd.run 'md5sum /root/a'
192.168.32.135:
    6a5f5fb87030a20cc1e371904fdcdf18  /root/a
[root@salt-master ~]# salt '*' file.get_sum /root/a md5
192.168.32.135:
    6a5f5fb87030a20cc1e371904fdcdf18

file.get_uid與file.get_user

獲取指定檔案的 uid 或 使用者名稱

[root@salt-master ~]# salt '*' cmd.run 'ls -l /root/a'
192.168.32.135:
    -rw-r--r-- 1 tom tom 24 Aug 19 21:04 /root/a
[root@salt-master ~]# salt '*' file.get_uid /root/a
192.168.32.135:
    1000
[root@salt-master ~]# salt '*' file.get_user /root/a
192.168.32.135:
    tom

file.gid_to_group

將指定的 gid 轉換為組名並顯示

[root@salt-master ~]# salt '*' file.group_to_gid root
192.168.32.135:
    0
[root@salt-master ~]# salt '*' file.group_to_gid tom
192.168.32.135:
    1000

file.grep

在指定檔案中檢索指定內容
該函式支援萬用字元,若在指定的路徑中用萬用字元則必須用雙引號引起來

[root@salt-master ~]# salt '*' file.grep /etc/passwd nobody
192.168.32.135:
    ----------
    pid:
        3910
    retcode:
        0
    stderr:
    stdout:
        nobody:x:99:99:Nobody:/:/sbin/nologin


[root@salt-master ~]# salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i
192.168.32.135:
    ----------
    pid:
        3971
    retcode:
        0
    stderr:
    stdout:
        IPADDR="192.168.32.135"


[root@salt-master ~]# salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i -B2
192.168.32.135:
    ----------
    pid:
        4003
    retcode:
        0
    stderr:
    stdout:
        DEVICE=ens33
        ONBOOT=yes
        IPADDR="192.168.32.135"


[root@salt-master ~]# salt '*' file.grep "/etc/sysconfig/network-scripts/*" ipaddr -- -i -l
[root@salt-master ~]# salt '*' file.grep "/etc/sysconfig/network-scripts/*" ipaddr -- -i -l
192.168.32.135:
    ----------
    pid:
        4021
    retcode:
        0
    stderr:
    stdout:
        /etc/sysconfig/network-scripts/ifcfg-lo
        /etc/sysconfig/network-scripts/ifdown-ipv6
        /etc/sysconfig/network-scripts/ifup-aliases
        /etc/sysconfig/network-scripts/ifup-eth
        /etc/sysconfig/network-scripts/ifup-ippp
        /etc/sysconfig/network-scripts/ifup-ipv6
        /etc/sysconfig/network-scripts/ifup-isdn
        /etc/sysconfig/network-scripts/ifup-plip
        /etc/sysconfig/network-scripts/ifup-plusb
        /etc/sysconfig/network-scripts/ifup-post
        /etc/sysconfig/network-scripts/ifup-ppp
        /etc/sysconfig/network-scripts/ifup-tunnel
        /etc/sysconfig/network-scripts/network-functions
        /etc/sysconfig/network-scripts/ifcfg-ens33

file.is_blkdev

判斷指定的檔案是否是塊裝置檔案

[root@salt-master ~]# salt '*' file.is_blkdev /dev/sr0
192.168.32.135:
    True

file.lsattr

檢查並顯示出指定檔案的屬性資訊

[root@salt-master ~]# salt '*' file.lsattr /etc/passwd
192.168.32.135:
    ----------
    /etc/passwd:

file.mkdir

建立目錄並設定屬主、屬組及許可權

[root@salt-master ~]# salt '*' file.mkdir /root/aaa
192.168.32.135:
    True
[root@salt-master ~]# salt '*' cmd.run 'ls -l /root'
192.168.32.135:
    total 16
    -rw-r--r--   1 tom  tom    24 Aug 19 21:04 a
    drwxr-xr-x   2 root root    6 Aug 19 21:53 aaa
    -rw-r--r--   1 tom  tom    24 Aug 19 21:13 abc
    drwxrwxrwt  24 root root 4096 Aug 19 21:15 abcd
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    drwxr-xr-x   2 root root   39 Aug 19 21:19 test


[root@salt-master ~]# salt '*' file.mkdir /root/bbb tom tom 400
192.168.32.135:
    True
[root@salt-master ~]# salt '*' cmd.run 'ls -l /root'
192.168.32.135:
    total 16
    -rw-r--r--   1 tom  tom    24 Aug 19 21:04 a
    drwxr-xr-x   2 root root    6 Aug 19 21:53 aaa
    -rw-r--r--   1 tom  tom    24 Aug 19 21:13 abc
    drwxrwxrwt  24 root root 4096 Aug 19 21:15 abcd
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    dr--------   2 tom  tom     6 Aug 19 21:54 bbb
    drwxr-xr-x   2 root root   39 Aug 19 21:19 test

file.move

移動或重新命名

#重新命名
[root@salt-master ~]# salt '*' file.move /root/aaa /root/ccc
192.168.32.135:
    ----------
    comment:
        '/root/aaa' moved to '/root/ccc'
    result:
        True
[root@salt-master ~]# salt '*' cmd.run 'ls -l /root'
192.168.32.135:
    total 16
    -rw-r--r--   1 tom  tom    24 Aug 19 21:04 a
    -rw-r--r--   1 tom  tom    24 Aug 19 21:13 abc
    drwxrwxrwt  24 root root 4096 Aug 19 21:15 abcd
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    dr--------   2 tom  tom     6 Aug 19 21:54 bbb
    drwxr-xr-x   2 root root    6 Aug 19 21:53 ccc
    drwxr-xr-x   2 root root   39 Aug 19 21:19 test


#移動
[root@salt-master ~]# salt '*' file.move /root/bbb /opt/ddd
192.168.32.135:
    ----------
    comment:
        '/root/bbb' moved to '/opt/ddd'
    result:
        True
[root@salt-master ~]# salt '*' cmd.run 'ls -l /root'
192.168.32.135:
    total 16
    -rw-r--r--   1 tom  tom    24 Aug 19 21:04 a
    -rw-r--r--   1 tom  tom    24 Aug 19 21:13 abc
    drwxrwxrwt  24 root root 4096 Aug 19 21:15 abcd
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    drwxr-xr-x   2 root root    6 Aug 19 21:53 ccc
    drwxr-xr-x   2 root root   39 Aug 19 21:19 test
[root@salt-master ~]# salt '*' cmd.run 'ls -l /opt'
192.168.32.135:
    total 0
    dr-------- 2 tom  tom   6 Aug 19 21:54 ddd
    drwxr-xr-x 2 root root 39 Aug 19 21:19 test

file.prepend

把文字插入指定檔案的開頭

[root@salt-master ~]# salt '*' cmd.run 'cat /root/a'
192.168.32.135:
    hello world
    world hello
[root@salt-master ~]# salt '*' file.prepend /root/a "1111" "2222" "3333"
192.168.32.135:
    Prepended 3 lines to "/root/a"
[root@salt-master ~]# salt '*' cmd.run 'cat /root/a'
192.168.32.135:
    1111
    2222
    3333
    hello world
    world hello

file.sed

修改文字檔案的內容

[root@salt-master ~]# salt '*' cmd.run 'cat /root/a'
192.168.32.135:
    1111
    2222
    3333
    hello world
    world hello
[root@salt-master ~]# salt '*' file.sed /root/a 'hello' 'hi'
192.168.32.135:
    ----------
    pid:
        4574
    retcode:
        0
    stderr:
    stdout:
[root@salt-master ~]# salt '*' cmd.run 'cat /root/a'
192.168.32.135:
    1111
    2222
    3333
    hi world
    world hi


#每行第一個hi改成hello
[root@salt-master ~]# salt '*' file.sed /root/a 'hi' 'hello' flags=1
192.168.32.135:
    ----------
    pid:
        4926
    retcode:
        0
    stderr:
    stdout:
[root@salt-master ~]# salt '*' cmd.run 'cat /root/a'
192.168.32.135:
    1111
    2222
    3333
    hello world
    world hello

file.read

讀取檔案內容

[root@salt-master ~]# salt '*' cmd.run 'cat /root/a'
192.168.32.135:
    1111
    2222
    3333
    hello world
    world hello
[root@salt-master ~]# salt '*' file.read /root/a
192.168.32.135:
    1111
    2222
    3333
    hello world
    world hello

file.readdir

列出指定目錄下的所有檔案或目錄,包括隱藏檔案

[root@salt-master ~]# salt '*' file.readdir /root
192.168.32.135:
    - .
    - ..
    - .bash_logout
    - .bash_profile
    - .bashrc
    - .cshrc
    - .tcshrc
    - anaconda-ks.cfg
    - .pki
    - .bash_history
    - .viminfo
    - abc
    - abcd
    - test
    - ccc
    - a.bak
    - a

file.remove

刪除指定的檔案或目錄,若給出的是目錄,將遞迴刪除

[root@salt-master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.32.135:
    total 20
    -rw-r--r--   1 tom  tom    39 Aug 19 22:06 a
    -rw-r--r--   1 tom  tom    33 Aug 19 22:05 a.bak
    -rw-r--r--   1 tom  tom    24 Aug 19 21:13 abc
    drwxrwxrwt  24 root root 4096 Aug 19 21:15 abcd
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    drwxr-xr-x   2 root root    6 Aug 19 21:53 ccc
    drwxr-xr-x   2 root root   39 Aug 19 21:19 test


[root@salt-master ~]# salt '*' file.remove /root/abc 
192.168.32.135:
    True
[root@salt-master ~]# salt '*' file.remove /root/ccc
192.168.32.135:
    True
[root@salt-master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.32.135:
    total 16
    -rw-r--r--   1 tom  tom    39 Aug 19 22:06 a
    -rw-r--r--   1 tom  tom    33 Aug 19 22:05 a.bak
    drwxrwxrwt  24 root root 4096 Aug 19 21:15 abcd
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    drwxr-xr-x   2 root root   39 Aug 19 21:19 test

file.rename

重新命名檔案或目錄

[root@salt-master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.32.135:
    total 16
    -rw-r--r--   1 tom  tom    39 Aug 19 22:06 a
    -rw-r--r--   1 tom  tom    33 Aug 19 22:05 a.bak
    drwxrwxrwt  24 root root 4096 Aug 19 21:15 abcd
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    drwxr-xr-x   2 root root   39 Aug 19 21:19 test
[root@salt-master ~]# salt '*' file.rename /root/abcd /root/ABCD
192.168.32.135:
    True
[root@salt-master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.32.135:
    total 16
    drwxrwxrwt  24 root root 4096 Aug 19 21:15 ABCD
    -rw-r--r--   1 tom  tom    39 Aug 19 22:06 a
    -rw-r--r--   1 tom  tom    33 Aug 19 22:05 a.bak
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    drwxr-xr-x   2 root root   39 Aug 19 21:19 test

file.set_mode

給指定檔案設定許可權

[root@salt-master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.32.135:
    total 16
    drwxrwxrwt  24 root root 4096 Aug 19 21:15 ABCD
    -rw-r--r--   1 tom  tom    39 Aug 19 22:06 a
    -rw-r--r--   1 tom  tom    33 Aug 19 22:05 a.bak
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    drwxr-xr-x   2 root root   39 Aug 19 21:19 test
[root@salt-master ~]# salt '*' file.set_mode /root/a 0755
192.168.32.135:
    0755
[root@salt-master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.32.135:
    total 16
    drwxrwxrwt  24 root root 4096 Aug 19 21:15 ABCD
    -rwxr-xr-x   1 tom  tom    39 Aug 19 22:06 a
    -rw-r--r--   1 tom  tom    33 Aug 19 22:05 a.bak
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    drwxr-xr-x   2 root root   39 Aug 19 21:19 test

給指定的檔案建立軟連結

[root@salt-master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.32.135:
    total 16
    drwxrwxrwt  24 root root 4096 Aug 19 21:15 ABCD
    -rwxr-xr-x   1 tom  tom    39 Aug 19 22:06 a
    -rw-r--r--   1 tom  tom    33 Aug 19 22:05 a.bak
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    drwxr-xr-x   2 root root   39 Aug 19 21:19 test
[root@salt-master ~]# salt '*' file.symlink /root/a /opt/a
192.168.32.135:
    True
[root@salt-master ~]# salt '*' cmd.run 'ls -l /opt'
192.168.32.135:
    total 0
    lrwxrwxrwx 1 root root  7 Aug 19 22:15 a -> /root/a
    dr-------- 2 tom  tom   6 Aug 19 21:54 ddd
    drwxr-xr-x 2 root root 39 Aug 19 21:19 test

file.touch

建立空檔案或更新時間戳

[root@salt-master ~]# salt '*' cmd.run 'ls -l /root'
192.168.32.135:
    total 16
    drwxrwxrwt  24 root root 4096 Aug 19 21:15 ABCD
    -rwxr-xr-x   1 tom  tom    39 Aug 19 22:06 a
    -rw-r--r--   1 tom  tom    33 Aug 19 22:05 a.bak
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    drwxr-xr-x   2 root root   39 Aug 19 21:19 test
[root@salt-master ~]# salt '*' file.touch /root/anaconda-ks.cfg 
192.168.32.135:
    True
[root@salt-master ~]# salt '*' cmd.run 'ls -l /root'
192.168.32.135:
    total 16
    drwxrwxrwt  24 root root 4096 Aug 19 21:15 ABCD
    -rwxr-xr-x   1 tom  tom    39 Aug 19 22:06 a
    -rw-r--r--   1 tom  tom    33 Aug 19 22:05 a.bak
    -rw-------.  1 root root 1257 Aug 19 22:17 anaconda-ks.cfg
    drwxr-xr-x   2 root root   39 Aug 19 21:19 test

file.uid_to_user

將指定的 uid 轉換成使用者名稱顯示出來

[root@salt-master ~]# salt '*' file.user_to_uid tom
192.168.32.135:
    1000
[root@salt-master ~]# salt '*' file.user_to_uid root
192.168.32.135:
    0

file.write

往一個指定的檔案裡覆蓋寫入指定內容

[root@salt-master ~]# salt '*' cmd.run 'cat /root/a'
192.168.32.135:
    1111
    2222
    3333
    hello world
    world hello
[root@salt-master ~]# salt '*' file.write /root/a "11111" "22222" "333"
192.168.32.135:
    Wrote 3 lines to "/root/a"
[root@salt-master ~]# salt '*' cmd.run 'cat /root/a'
192.168.32.135:
    11111
    22222
    333