码迷,mamicode.com
首页 > 其他好文 > 详细

SaltStack常用模块

时间:2020-08-25 15:51:39      阅读:51      评论:0      收藏:0      [点我收藏+]

标签:正在运行的服务   hardware   error   header   vsftpd   iso   send   屏蔽   配置   

目录

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
    - chrony-dnssrv@.timer
    - 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

SaltStack常用模块

标签:正在运行的服务   hardware   error   header   vsftpd   iso   send   屏蔽   配置   

原文地址:https://www.cnblogs.com/shipment/p/13526858.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!