SaltStack常用的模块

Saltstack常用的模块

1.SaltStack模块介绍

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

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

//查看所有module列表
[root@master ~]# salt 'node1' sys.list_modules
node1:
    - acl
    - aliases
    - alternatives
    - ansible
    - archive
    - artifactory
    - at
    - baredoc
    - beacons
    - bigip
    - btrfs
    - buildout
    - chroot
    - cloud
    - cmd
    - composer
    - config
    -此处省略n行


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

//查看指定module的用法
[root@master ~]# salt 'node1' sys.doc cmd
'cmd.exec_code:'

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

    CLI Example:

        salt '*' cmd.exec_code ruby 'puts "cheese"' 
...此处省略N行...


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

2.SaltStack常用模块

2.1 SaltStack常用模块之network

2.1.1 network.active_tcp

返回所有活动的tcp连接

node1:
    ----------
    0:
        ----------
        local_addr:
            192.168.147.55
        local_port:
            54108
        remote_addr:
            192.168.147.33
        remote_port:
            4505
    1:
        ----------
        local_addr:
            192.168.147.55
        local_port:
            22
        remote_addr:
            192.168.147.1
        remote_port:
            13269
   ----此处省略n行
   master:
    ----------
    0:
        ----------
        local_addr:
            192.168.147.33
        local_port:
            22
        remote_addr:
            192.168.147.1
        remote_port:
            5451
    1:
        ----------
        local_addr:
            192.168.147.33
        local_port:
            4505
        remote_addr:
            192.168.147.33
        remote_port:
            40872
  ----此处省略n行

2.1.2 network.calc_net

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

[root@master ~]# salt '*' network.calc_net 192.168.147.33 255.255.255.0
node1:
    192.168.147.0/24
master:
    192.168.147.0/24
[root@master ~]# salt '*' network.calc_net 192.168.147.33 255.255.255.240
node1:
    192.168.147.32/28
master:
    192.168.147.32/28

2.1.3 network.connect

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

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

2.1.4 network.default_route

查看默认路由

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

2.1.5 network.get_fqdn

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

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

2.1.6 network.get_hostname

获取主机名

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

2.1.7 network.get_route

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

[root@master ~]# salt '*' network.get_route 192.168.147.55
node1:
    ----------
    destination:
        192.168.147.55
    gateway:
        None
    interface:
        lo
    source:
        192.168.147.55
master:
    ----------
    destination:
        192.168.147.55
    gateway:
        None
    interface:
        ens33
    source:
        192.168.147.33

2.1.8 network.hw_addr

返回指定网卡的MAC地址

[root@master ~]# salt '*' network.hw_addr ens33
node1:
    00:0c:29:e1:dd:18
master:
    00:0c:29:02:ac:c7

2.1.9 network.ifacestartswith

从特定CIDR检索接口名称

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

2.1.10 network.in_subnet

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

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

2.1.11 network.interface

返回指定网卡的信息

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

2.1.12 network.interface_ip

返回指定网卡的IP地址

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

2.1.13 network.interfaces

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

[root@master ~]# salt '*' network.interfaces
master:
    ----------
    ens33:
        ----------
        hwaddr:
            00:0c:29:02:ac:c7
        inet:
            |_
              ----------
              address:
                  192.168.147.33
              broadcast:
                  192.168.147.255
              label:
                  ens33
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
              address:
                  fe80::b1d6:f0c4:330a:bd51
              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
node1:
    ----------
    ens33:
        ----------
        hwaddr:
            00:0c:29:e1:dd:18
        inet:
            |_
              ----------
              address:
                  192.168.147.55
              broadcast:
                  192.168.147.255
              label:
                  ens33
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
              address:
                  fe80::bbf3:80da:49bd:f61b
              prefixlen:
                  64
              scope:
                  link
        up:
            True
    lo:
        ----------
        hwaddr:
            00:00:00:00:00:00
        inet:
            |_
              ----------
              address:
                  127.0.0.1
              broadcast:
                  None
              label:
                  lo
              netmask:
                  255.0.0.0
        inet6:
            |_
              ----------
              address:
                  ::1
              prefixlen:
                  128
              scope:
                  host
        up:
            True

2.1.14network.ip_addrs

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

[root@master ~]# salt '*' network.ip_addrs
node1:
    - 192.168.147.55
master:
    - 192.168.147.33

2.1.15network.netstat

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

[root@master ~]# salt '*' network.netstat
master:
    |_
      ----------
      inode:
          28578
      local-address:
          0.0.0.0:22
      program:
          1027/sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          0.0.0.0:*
      send-q:
          0
      state:
          LISTEN
---此处省略n行
node1:
    |_
      ----------
      inode:
          29090
      local-address:
          0.0.0.0:22
      program:
          1029/sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          0.0.0.0:*
      send-q:
          0
      state:
----此处省略n行

2.1.16network.ping

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

[root@master ~]# salt '*' network.ping baidu.com
node1:
    PING baidu.com (39.156.69.79) 56(84) bytes of data.
    64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=128 time=25.9 ms
    64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=128 time=33.1 ms
    64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=128 time=28.2 ms
    64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=4 ttl=128 time=26.5 ms
    
    --- baidu.com ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3006ms
    rtt min/avg/max/mdev = 25.882/28.444/33.123/2.838 ms
master:
    PING baidu.com (39.156.69.79) 56(84) bytes of data.
    64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=128 time=25.7 ms
    64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=128 time=30.3 ms
    64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=128 time=27.4 ms
    64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=4 ttl=128 time=26.4 ms
    
    --- baidu.com ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3006ms
    rtt min/avg/max/mdev = 25.665/27.430/30.296/1.763 ms

2.1.17network.reverse_ip

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

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

2.2 SaltStack常用模块之service

2.2.1 service.available

判断指定的服务是否可用

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

2.2.2 service.get_all

获取所有正在运行的服务

[root@master ~]# salt '*' service.get_all
node1:
    - NetworkManager
    - NetworkManager-dispatcher
    - NetworkManager-wait-online
    - arp-ethers
    - atd
    - auditd
    - autovt@
    - basic.target
    - blk-availability
    - bluetooth.target
    - bolt
----此处省略n行
master:
    - NetworkManager
    - NetworkManager-dispatcher
    - NetworkManager-wait-online
    - arp-ethers
    - atd
    - auditd
    - autovt@
    - basic.target
    - blk-availability
    - bluetooth.target
    - bolt
    - boot-complete.target
    - chrony-dnssrv@
    - chrony-dnssrv@.timer
---此处省略n行

2.2.3 service.disabled

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

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

2.2.4 service.enabled

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

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

2.2.5 service.disable

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

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

2.2.6 service.enable

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

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

2.2.7 service.reload

重新加载指定服务

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

2.2.8 service.stop

停止指定服务

[root@master ~]# salt '*' service.stop httpd
node1:
    True
    
[root@node1 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Tue 2021-07-06 08:25:14 EDT; 39s ago
     Docs: man:httpd.service(8)

2.2.9 service.start

启动指定服务

[root@master ~]# salt '*' service.start httpd
node1:
    True
    
[root@node1 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-07-06 08:26:37 EDT; 12s ago
     Docs: man:httpd.service(8)

2.2.10 service.restart

重启指定服务

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

2.2.11 service.status

查看指定服务的状态

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

2.3 SaltStack常用模块之pkg

2.3.1 pkg.download

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

[root@master ~]#  salt '*' pkg.download wget
node1:
    ----------
    wget:
        /var/cache/yum/packages/wget-1.19.5-10.el8.x86_64.rpm

2.3.2 pkg.file_list

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

[root@master ~]# salt '*' pkg.file_list httpd
node1:
    ----------
    errors:
    files:
        - /etc/httpd/conf
        - /etc/httpd/conf.d/autoindex.conf
        - /etc/httpd/conf.d/userdir.conf
        - /etc/httpd/conf.d/welcome.conf
        - /etc/httpd/conf.modules.d
        - /etc/httpd/conf.modules.d/00-base.conf
        - /etc/httpd/conf.modules.d/00-dav.conf
        - /etc/httpd/conf.modules.d/00-lua.conf
---此处省略n行

//当不提供参数时,将会列出当前系统中所有已安装软件
[root@master ~]# salt '*' pkg.file_list
node1:
    ----------
    errors:
    files:
        VALUE_TRIMMED
master:
    ----------
    errors:
    files:
        VALUE_TRIMMED

2.3.3 pkg.group_info

查看包组的信息

[root@master ~]# salt '*' pkg.group_info 'Development Tools'
node1:
    ----------
    conditional:
    default:
        - asciidoc
        - byacc
        - ctags
        - diffstat
        - elfutils-libelf-devel
        - git
        - intltool
        - jna
        - ltrace
        - patchutils
----此处省略n行

2.3.4 pkg.group_list

列出系统中所有的包组

[root@master ~]# salt '*' pkg.group_list
master:
    ----------
    available:
        - Backup Client
        - base-x
        - Conflicts AppStream
        - Debugging Tools
        - Desktop Debugging and Performance Tools
        - .NET Core Development
        - FTP Server
        - GNOME Applications
        - Graphics Creation Tools
        - Guest Agents
        - Guest Desktop Agents
        - Input Methods
        - Internet Applications
        - Internet Browser
----此处省略n行

2.3.5 pkg.install

安装软件

[root@master ~]# salt '*' pkg.install wget
node1:
    ----------
master:
    ----------
[root@master ~]# which wget
/usr/bin/wget

2.3.6 pkg.list_downloaded

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

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

2.3.7 pkg.list_pkgs

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

[root@master ~]# salt '*' pkg.list_pkgs
master:
    ----------
    gmp:
        1:6.1.2-10.el8
    gnupg2:
        2.2.20-2.el8
    gnupg2-smime:
        2.2.20-2.el8
    gnutls:
        3.6.14-6.el8
    gobject-introspection:
        1.56.1-1.el8
    gpg-pubkey.(none):
        8483c65d-5ccc5b19,de57bfbe-53a9be98
    gpgme:
        1.13.1-6.el8
----此处省略n行

2.3.8 pkg.owner

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

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

2.3.9 pkg.remove

卸载指定软件

[root@master ~]# salt '*' cmd.run 'rpm -qa|grep wget'
master:
    wget-1.19.5-10.el8.x86_64
node1:
    wget-1.19.5-10.el8.x86_64


[root@master ~]# salt '*' pkg.remove wget
master:
    ----------
    wget:
        ----------
        new:
        old:
            1.19.5-10.el8
node1:
    ----------
    wget:
        ----------
        new:
        old:
            1.19.5-10.el8
[root@master ~]# which wget
/usr/bin/which: no wget in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)

2.3.10 pkg.upgrade

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

[root@master ~]# salt '*' pkg.upgrade name=openssl
master:
    ----------
    openssl:
        ----------
        new:
            1:1.1.1k-1.el8
        old:
            1:1.1.1g-11.el8
    openssl-libs:
        ----------
        new:
            1:1.1.1k-1.el8
        old:
            1:1.1.1g-11.el8
node1:
    ----------
    openssl:
        ----------
        new:
            1:1.1.1k-1.el8
        old:
            1:1.1.1g-11.el8
    openssl-libs:
        ----------
        new:
            1:1.1.1k-1.el8
        old:
            1:1.1.1g-11.el8

2.4 SaltStack常用模块之state

2.4.1 state.show_highstate

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

在这里插入代码片

2.4.2 state.highstate

执行高级状态

node1:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 20:07:37.012733
    Duration: 1050.73 ms
     Changes:
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 20:07:38.065796
    Duration: 94.747 ms
     Changes:

Summary for node1
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
Total run time:   1.145 s

2.4.3 state.show_state_usage

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

node1:
    ----------
    base:
        ----------
        count_all:
            2
        count_unused:
            1
        count_used:
            1
        unused:
            - top
        used:
            - web.apache.install
    dev:
        ----------
        count_all:
            0
        count_unused:
            0
        count_used:
            0
        unused:
        used:
    prod:
        ----------
        count_all:
            0
        count_unused:
            0
        count_used:
            0
        unused:
        used:
    test:
        ----------
        count_all:
            0
        count_unused:
            0
        count_used:
            0
        unused:
        used:

2.4.4 state.show_top

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

node1:
    ----------
    base:
        - web.apache.install

2.4.5 state.top

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

[root@master ~]# salt '*' state.top install.sls
node1:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 20:17:20.129390
    Duration: 917.659 ms
     Changes:
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 20:17:21.048144
    Duration: 46.958 ms
     Changes:

Summary for 192.168.69.202
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
Total run time: 964.617 ms

2.4.6 state.show_sls

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

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

2.5 SaltStack常用模块之salt-cp

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

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


[root@master ~]# touch yy
[root@master ~]# echo '123456' > yy
[root@master ~]# cat yy
123456
[root@master ~]#  salt-cp '*' /root/yy /root/hh
master:
    ----------
    /root/hh:
        True
node1:
    ----------
    /root/hh:
        True
[root@node1 ~]# cat hh
123456

2.6 SaltStack常用模块之file

2.6.1 file.access

检查指定路径是否存在

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

检查指定文件的权限信息

[root@node1 ~]# cd /usr/src/
[root@node1 src]# touch abc
[root@node1 src]# ls
abc  debug  kernels

[root@master src]# salt '*' cmd.run 'ls -l /usr/src/'
node1:
    total 0
    -rw-r--r--. 1 root root 0 Jul  7 02:31 abc
    drwxr-xr-x. 2 root root 6 May 18  2020 debug
    drwxr-xr-x. 2 root root 6 May 18  2020 kernels
[root@master src]# salt '*' file.access /usr/src/abc r    //是否有读权限
node1:
    True
[root@master src]# salt '*' file.access /usr/src/abc w    //是否有写权限
node1: 
    True
[root@master src]# salt '*' file.access /usr/src/abc x    //是否有执行权限
node1:
    False

2.6.2 file.append

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

[root@master ~]# salt '*' cmd.run 'ls -l /root/oo'
node1:
    -rw-r--r--. 1 root root 0 Jul  7 02:34 /root/oo
[root@master ~]# salt '*' file.append /root/oo "hello world" "jjyy"
node1:
    Wrote 2 lines to "/root/oo"
[root@master ~]# salt '*' cmd.run 'ls -l /root/oo'
node1:
    -rw-r--r--. 1 root root 17 Jul  7 02:36 /root/oo
[root@master ~]# salt '*' cmd.run 'cat /root/oo'
node1:
    hello world
    jjyy

//没有qqww文件,就报错了
[root@master ~]# salt '*' file.append /root/qqww "hello world" "jjyy"
node1:
    The minion function caused an exception: Traceback (most recent call last):
      File "/usr/lib/python3.6/site-packages/salt/minion.py", line 1905, in _thread_return
        function_name, function_args, executors, opts, data
      File "/usr/lib/python3.6/site-packages/salt/minion.py", line 1861, in _execute_job_function
        return_data = self.executors[fname](opts, data, func, args, kwargs)
      File "/usr/lib/python3.6/site-packages/salt/loader.py", line 1241, in __call__
        return self.loader.run(run_func, *args, **kwargs)
      File "/usr/lib/python3.6/site-packages/salt/loader.py", line 2274, in run
        return self._last_context.run(self._run_as, _func_or_method, 
        -------此处省略n行

2.6.3 file.basename

获取指定路径的基名

[root@master ~]# salt '*' file.basename '/root/oo'
node1:
    oo

2.6.4 file.dirname

获取指定路径的目录名

[root@master ~]# salt '*' file.dirname '/root/oo'
node1:
    /root

2.6.5 file.check_hash

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

[root@master ~]# salt '*' cmd.run 'md5sum /etc/yy'
node1:
    d41d8cd98f00b204e9800998ecf8427e  /etc/yy
[root@master ~]# salt '*' file.check_hash /etc/yy d41d8cd98f00b204e9800998ecf8427e 
node1:
    True

2.6.6 file.chattr

修改指定文件的属性

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

给指定文件添加属性

//查看当前属性
[root@master ~]# salt '*' cmd.run 'lsattr /root'
node1:
    -------------------- /root/anaconda-ks.cfg
    -------------------- /root/mysql
    -------------------- /root/hh
    -------------------- /root/httpd
    -------------------- /root/oo
[root@master ~]# salt '*' file.chattr /root/oo operator=add attributes=ai
node1:
    True
 
 //添加属性
[root@master ~]# salt '*' cmd.run 'lsattr /root'
node1:
    -------------------- /root/anaconda-ks.cfg
    -------------------- /root/mysql
    -------------------- /root/hh
    -------------------- /root/httpd
    ----ia-------------- /root/oo

[root@node1 ~]# rm -rf oo
rm: 无法删除'oo': 不允许的操作

[root@node1 ~]# echo '123' > oo
-bash: oo: 不允许的操作
[root@node1 ~]# echo '123' >> oo
-bash: oo: 不允许的操作

给指定目录添加属性

[root@master ~]# salt '*' file.chattr /root/ww operator=add attributes=ai
node1:
    True

[root@node1 ww]# echo "qweer" > jj
[root@node1 ww]# rm -rf jj
rm: 无法删除'jj': 不允许的操作

给指定文件去除属性

[root@master ~]# salt '*' file.chattr /root/oo operator=remove attributes=i
node1:
    True
[root@master ~]# salt '*' cmd.run 'lsattr /root'
node1:
    -------------------- /root/anaconda-ks.cfg
    -------------------- /root/mysql
    -------------------- /root/hh
    -------------------- /root/httpd
    -----a-------------- /root/oo
    
[root@node1 ~]# rm -rf oo
rm: 无法删除'oo': 不允许的操作

2.6.7 file.chown

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

[root@node1 ~]# chown -R apache.apache /root/hh
[root@node1 ~]# ll
总用量 12
-rw-------. 1 root   root   1030 63 14:36 anaconda-ks.cfg
-rw-r--r--. 1 apache apache    7 76 09:20 hh
-rw-r--r--. 1 root   root     17 77 02:36 oo
drwxr-xr-x. 2 root   root     16 77 03:04 ww


[root@master ~]# salt '*' file.chown /root/hh root root
node1:
    None
[root@master ~]# salt '*' cmd.run 'ls -l /root/'
node1:
    total 12
    -rw-------. 1 root root 1030 Jun  3 14:36 anaconda-ks.cfg
    -rw-r--r--. 1 root root    7 Jul  6 09:20 hh
    -rw-r--r--. 1 root root   17 Jul  7 02:36 oo
    drwxr-xr-x. 2 root root   16 Jul  7 03:04 ww

2.6.8 file.copy

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

拷贝文件

[root@node1 ~]# cat oo
hello world
jjyy

[root@master ~]# salt '*' cmd.run 'ls -l /root'
node1:
    total 12
    -rw-------. 1 root root 1030 Jun  3 14:36 anaconda-ks.cfg
    -rw-r--r--. 1 root root    7 Jul  6 09:20 hh
    -rw-r--r--. 1 root root   17 Jul  7 02:36 oo
    drwxr-xr-x. 2 root root   16 Jul  7 03:04 ww
[root@master ~]# salt '*' file.copy /root/oo /root/aa
node1:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root'
node1:
    total 16
    -rw-r--r--. 1 root root   17 Jul  7 03:14 aa
    -rw-------. 1 root root 1030 Jun  3 14:36 anaconda-ks.cfg
    -rw-r--r--. 1 root root    7 Jul  6 09:20 hh
    -rw-r--r--. 1 root root   17 Jul  7 02:36 oo
    drwxr-xr-x. 2 root root   16 Jul  7 03:04 ww

[root@node1 ~]# cat aa
hello world
jjyy

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

[root@node1 opt]# ls
pp

[root@master ~]# salt '*' file.copy /opt/ /root/ii recurse=True
node1:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root'
node1:
    total 16
    -rw-r--r--. 1 root root   17 Jul  7 03:14 aa
    -rw-------. 1 root root 1030 Jun  3 14:36 anaconda-ks.cfg
    -rw-r--r--. 1 root root    7 Jul  6 09:20 hh
    drwxr-xr-x. 2 root root   16 Jul  7 03:19 ii
    -rw-r--r--. 1 root root   17 Jul  7 02:36 oo
    drwxr-xr-x. 2 root root   16 Jul  7 03:04 ww

[root@node1 ii]# ls
pp

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

[root@master ~]# salt '*' cmd.run 'ls -l /root/abc'
node1:
    total 0
    -rw-r--r--. 1 root root 0 Jul  7 03:36 jerry
    -rw-r--r--. 1 root root 0 Jul  7 03:36 lisi
    -rw-r--r--. 1 root root 0 Jul  7 03:36 tom
    -rw-r--r--. 1 root root 0 Jul  7 03:36 wangwu
    -rw-r--r--. 1 root root 0 Jul  7 03:36 zhangsan
[root@master ~]# salt '*' cmd.run 'ls -l /opt/abc'
node1:
    total 0
    -rw-r--r--. 1 root root 0 Jul  7 03:38 jerry
    -rw-r--r--. 1 root root 0 Jul  7 03:38 tom
[root@master ~]# salt '*' file.copy /opt/abc/ /root/abc/ recurse=True remove_existing=True
node1:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root/abc'
node1:
    total 0
    -rw-r--r--. 1 root root 0 Jul  7 03:38 jerry
    -rw-r--r--. 1 root root 0 Jul  7 03:38 tom

2.6.9 file.ditectory_exists

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

[root@master ~]# salt '*' cmd.run 'ls -l /opt'
node1:
    total 0
    -rw-r--r--. 1 root root 0 Jul  7 03:22 ii
    -rw-r--r--. 1 root root 0 Jul  7 03:18 pp
[root@master ~]# salt '*' file.directory_exists /opt/ii
node1:
    True

2.6.10 file.diskusage

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

[root@master ~]# salt '*' cmd.run 'du -sb /opt'
node1:
    26	/opt
[root@master ~]# salt '*' file.diskusage /opt
node1:
    0

2.6.11 file.file_exists

判断指定文件是否存在

[root@master ~]# salt '*' cmd.run 'ls -l /root/'
node1:
    total 16
    -rw-r--r--. 1 root root   17 Jul  7 03:14 aa
    -rw-------. 1 root root 1030 Jun  3 14:36 anaconda-ks.cfg
    -rw-r--r--. 1 root root    7 Jul  6 09:20 hh
    drwxr-xr-x. 2 root root   26 Jul  7 03:22 ii
    -rw-r--r--. 1 root root   17 Jul  7 02:36 oo
    drwxr-xr-x. 2 root root   16 Jul  7 03:04 ww
[root@master ~]# salt '*' file.file_exists /root/aa
node1:
    True

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

2.6.12 file.find

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

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

and/or actions:

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

and/or depth criteria:

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

The default action is print=path

path-glob:

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

path-regex: a Python Regex (regular expression) pattern to match pathnames

file-types: a string of one or more of the following:

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

users: a space and/or comma separated list of user names and/or uids

groups: a space and/or comma separated list of group names and/or gids

size-unit:

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

interval:

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

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

print-opts: a comma and/or space separated list of one or more of the following:

group: group name
md5:   MD5 digest of file contents
mode:  file permissions (as integer)
mtime: last modification time (as time_t)
name:  file basename
path:  file absolute path
size:  file size in bytes
type:  file type
user:  user name

示例:

[root@node1 ~]# ls
abc  anaconda-ks.cfg  hh  ii  oo  ww

[root@master ~]# salt '*' file.find /root name=hh type=f delete
node1:
    - /root/hh

[root@master ~]# salt '*' file.find /root name=hh type=f delete
node1:
    - /root/hh

//删除目录
[root@node1 ~]# ls
abc  anaconda-ks.cfg  ii  oo  ww

salt '*' file.find / type=f name=\*.bak size=+10m

2.6.13 file.get_gid

获取指定文件的gid

[root@master ~]# salt '*' cmd.run 'ls -l /root/oo'
node1:
    -rw-r--r--. 1 root root 17 Jul  7 02:36 /root/oo
[root@master ~]# salt '*' file.get_gid /root/oo
node1:
    0

2.6.14 file.get_group

获取指定文件的组名

[root@master ~]# salt '*' cmd.run 'ls -l /root/oo'
node1:
    -rw-r--r--. 1 root root 17 Jul  7 02:36 /root/oo
[root@master ~]# salt '*' file.get_group /root/oo
node1:
    root

2.6.15 file.get_hash

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

[root@master ~]# salt '*' cmd.run 'sha256sum /root/oo'
node1:
    2290bf9e8d970b94f7a1bdbdf0778f070155f4a3ac733634297965fd7734e97a  /root/oo
[root@master ~]# salt '*' file.get_hash /root/oo
node1:
    2290bf9e8d970b94f7a1bdbdf0778f070155f4a3ac733634297965fd7734e97a

2.6.16 file.get_mode

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

[root@master ~]# salt '*' cmd.run 'ls -l /root/oo'
node1:
    -rw-r--r--. 1 root root 17 Jul  7 02:36 /root/oo
[root@master ~]# salt '*' file.get_mode /root/oo
node1:
    0644

2.6.17 file.get_selinux_context

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

[root@master ~]# salt '*' cmd.run 'ls -Z /root/oo'
node1:
    unconfined_u:object_r:admin_home_t:s0 /root/oo
[root@master ~]# salt '*' file.get_selinux_context /root/oo
node1:
    unconfined_u:object_r:admin_home_t:s0

2.6.18 file.get_sum

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

  • md5
  • sha1
  • sha224
  • sha256 (default)
  • sha384
  • sha512
[root@master ~]# salt '*' cmd.run 'sha256sum /root/oo'
node1:
    2290bf9e8d970b94f7a1bdbdf0778f070155f4a3ac733634297965fd7734e97a  /root/oo
    
[root@master ~]# salt '*' file.get_sum /root/oo
node1:
    2290bf9e8d970b94f7a1bdbdf0778f070155f4a3ac733634297965fd7734e97a


[root@master ~]# salt '*' cmd.run 'md5sum /root/oo'
node1:
    4e8a818b3dbbbe05ab32fad3fa5aa514  /root/oo
    
[root@master ~]# salt '*' file.get_sum /root/oo md5
node1:
    4e8a818b3dbbbe05ab32fad3fa5aa514


2.6.19 file.get_uid与file.get_user

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

[root@master ~]# salt '*' cmd.run 'ls -l /root/oo'
node1:
    -rw-r--r--. 1 root root 17 Jul  7 02:36 /root/oo
[root@master ~]# salt '*' file.get_uid /root/oo
node1:
    0
[root@master ~]# salt '*' file.get_user /root/oo
node1:
    root

2.6.20 file.gid_to_group

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

[root@master ~]# salt '*' file.gid_to_group 1
node1:
    bin
[root@master ~]# salt '*' file.gid_to_group 2
node1:
    daemon
[root@master ~]# salt '*' file.gid_to_group 0
node1:
    root

2.6.21 file.group_to_gid

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

[root@master ~]# salt '*' file.group_to_gid root
node1:
    0
[root@master ~]# salt '*' file.group_to_gid bin
node1:
    1

2.6.22 file.grep

在指定文件中检索指定内容

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

[root@master ~]# salt '*' file.grep /etc/passwd nobody
node1:
    ----------
    pid:
        40055
    retcode:
        0
    stderr:
    stdout:
        nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
        
[root@master ~]# salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i
node1:
    ----------
    pid:
        40060
    retcode:
        0
    stderr:
    stdout:
        IPADDR=192.168.147.55
        
[root@master ~]# salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i -B2
node1:
    ----------
    pid:
        40066
    retcode:
        0
    stderr:
    stdout:
        DEVICE=ens33
        ONBOOT=yes
        IPADDR=192.168.147.55
        
[root@master ~]# salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i -A2
node1:
    ----------
    pid:
        40071
    retcode:
        0
    stderr:
    stdout:
        IPADDR=192.168.147.55
        NETMASK=255.255.255.0
        GATEWAY=192.168.147.2
        
[root@master ~]# salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i -C2
node1:
    ----------
    pid:
        40076
    retcode:
        0
    stderr:
    stdout:
        DEVICE=ens33
        ONBOOT=yes
        IPADDR=192.168.147.55
        NETMASK=255.255.255.0
        GATEWAY=192.168.147.2


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

2.6.23 file.is_blkdev

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

[root@master ~]# salt '*' cmd.run 'ls -l /dev/sr0'
node1:
    brw-rw----. 1 root cdrom 11, 0 Jun  3 14:38 /dev/sr0
[root@master ~]# salt '*' file.is_blkdev /dev/sr0
node1:
    True

2.6.24 file.lsattr

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

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

2.6.25 file.mkdir

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

[root@master ~]# salt '*' file.mkdir /root/jjyy
node1:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root'
node1:
    total 8
    drwxr-xr-x. 2 root root   30 Jul  7 03:38 abc
    -rw-------. 1 root root 1030 Jun  3 14:36 anaconda-ks.cfg
    drwxr-xr-x. 2 root root   26 Jul  7 03:22 ii
    drwxr-xr-x. 2 root root    6 Jul  7 04:21 jjyy
    -rw-r--r--. 1 root root   17 Jul  7 02:36 oo
[root@master ~]# salt '*' file.mkdir /root/xixi tom tom 077
node1:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root/'
node1:
    total 8
    drwxr-xr-x. 2 root root   30 Jul  7 03:38 abc
    -rw-------. 1 root root 1030 Jun  3 14:36 anaconda-ks.cfg
    drwxr-xr-x. 2 root root   26 Jul  7 03:22 ii
    drwxr-xr-x. 2 root root    6 Jul  7 04:21 jjyy
    -rw-r--r--. 1 root root   17 Jul  7 02:36 oo
    d---rwxrwx. 2 root root    6 Jul  7 04:22 xixi

2.6.26 file.move

移动或重命名

//重命名
[root@master ~]# salt '*' cmd.run 'ls -l /root'
node1:
    total 8
    drwxr-xr-x. 2 root root   30 Jul  7 03:38 abc
    -rw-------. 1 root root 1030 Jun  3 14:36 anaconda-ks.cfg
    drwxr-xr-x. 2 root root    6 Jul  7 04:21 jjyy
    -rw-r--r--. 1 root root   17 Jul  7 02:36 oo
    drwxr-xr-x. 2 root root   26 Jul  7 03:22 uu
    d---rwxrwx. 2 root root    6 Jul  7 04:22 xixi

//移动
[root@master ~]# salt '*' cmd.run 'ls -l /root'
node1:
    total 8
    drwxr-xr-x. 2 root root   30 Jul  7 03:38 abc
    -rw-------. 1 root root 1030 Jun  3 14:36 anaconda-ks.cfg
    drwxr-xr-x. 2 root root    6 Jul  7 04:21 jjyy
    -rw-r--r--. 1 root root   17 Jul  7 02:36 oo
    drwxr-xr-x. 2 root root   26 Jul  7 03:22 uu
    d---rwxrwx. 2 root root    6 Jul  7 04:22 xixi
[root@master ~]# salt '*' cmd.run 'ls -l /opt'
node1:
    total 0
    drwxr-xr-x. 2 root root 30 Jul  7 03:38 abc
    -rw-r--r--. 1 root root  0 Jul  7 03:22 ii
    -rw-r--r--. 1 root root  0 Jul  7 03:18 pp
[root@master ~]# salt '*' file.move /root/uu /opt/
node1:
    ----------
    comment:
        '/root/uu' moved to '/opt/'
    result:
        True
[root@master ~]# salt '*' cmd.run 'ls -l /opt'
node1:
    total 0
    drwxr-xr-x. 2 root root 30 Jul  7 03:38 abc
    -rw-r--r--. 1 root root  0 Jul  7 03:22 ii
    -rw-r--r--. 1 root root  0 Jul  7 03:18 pp
    drwxr-xr-x. 2 root root 26 Jul  7 03:22 uu

2.6.27 file.prepend

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

[root@master ~]# salt '*' cmd.run 'cat /root/oo'
node1:
    hello world
    jjyy

[root@master ~]# salt '*' file.prepend /root/oo "hehe" "xixi" "haha"
node1:
    Prepended 3 lines to "/root/oo"
[root@master ~]# salt '*' cmd.run 'cat /root/oo'
node1:
    hehe
    xixi
    haha
    hello world
    jjyy

2.6.28 file.sed

修改文本文件的内容

[root@master ~]# salt '*' cmd.run 'cat /root/qqww'
node1:
    haha
    123
    xixi
    hello hello hello
[root@master ~]# salt '*' file.sed /root/qqww 'hello' 'runtime'
node1:
    ----------
    pid:
        40308
    retcode:
        0
    stderr:
    stdout:
[root@master ~]# salt '*' cmd.run 'cat /root/qqww'
node1:
    haha
    123
    xixi
    runtime runtime runtime



[root@master ~]# salt '*' cmd.run 'cat /root/qqww'
node1:
    haha
    123
    xixi
    hello hello hello
[root@master ~]# salt '*' file.sed /root/qqww 'hello' 'runtime' flags=2
node1:
    ----------
    pid:
        40338
    retcode:
        0
    stderr:
    stdout:
[root@master ~]# salt '*' cmd.run 'cat /root/qqww'
node1:
    haha
    123
    xixi
    hello runtime hello

2.6.29 file.read

读取文件内容

[root@master ~]# salt '*' cmd.run 'cat /root/qqww'
node1:
    haha
    123
    xixi
    hello runtime hello
[root@master ~]# salt '*' file.read /root/qqww
node1:
    haha
    123
    xixi
    hello runtime hello

2.6.30 file.readdir

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

[root@master ~]# salt '*' file.readdir /root
node1:
    - .
    - ..
    - .bash_logout
    - .bash_profile
    - .bashrc
    - .cshrc
    - .tcshrc
    - anaconda-ks.cfg
    - .config
    - .bash_history
    - oo
    - abc
    - jjyy
    - xixi
    - b.bak
    - b
    - qqww.bak
    - .viminfo
    - qqww

2.6.31 file.remove

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

[root@master ~]# salt '*' cmd.run 'ls -l /root/'
node1:
    total 24
    drwxr-xr-x. 2 root root   30 Jul  7 03:38 abc
    -rw-------. 1 root root 1030 Jun  3 14:36 anaconda-ks.cfg
    -rw-r--r--. 1 root root   15 Jul  7 04:46 b
    -rw-r--r--. 1 root root   15 Jul  7 04:45 b.bak
    drwxr-xr-x. 2 root root    6 Jul  7 04:21 jjyy
    -rw-r--r--. 1 root root   17 Jul  7 02:36 oo
    -rw-r--r--. 1 root root   34 Jul  7 04:50 qqww
    -rw-r--r--. 1 root root   32 Jul  7 04:50 qqww.bak
    d---rwxrwx. 2 root root    6 Jul  7 04:22 xixi
[root@master ~]# salt '*' file.remove /root/b
node1:
    True
[root@master ~]# salt '*' file.remove /root/b.bak
node1:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root/'
node1:
    total 16
    drwxr-xr-x. 2 root root   30 Jul  7 03:38 abc
    -rw-------. 1 root root 1030 Jun  3 14:36 anaconda-ks.cfg
    drwxr-xr-x. 2 root root    6 Jul  7 04:21 jjyy
    -rw-r--r--. 1 root root   17 Jul  7 02:36 oo
    -rw-r--r--. 1 root root   34 Jul  7 04:50 qqww
    -rw-r--r--. 1 root root   32 Jul  7 04:50 qqww.bak
    d---rwxrwx. 2 root root    6 Jul  7 04:22 xixi

2.6.32 file.rename

重命名文件或目录

[root@master ~]# salt '*' cmd.run 'ls -l /root/'
node1:
    total 16
    drwxr-xr-x. 2 root root   30 Jul  7 03:38 abc
    -rw-------. 1 root root 1030 Jun  3 14:36 anaconda-ks.cfg
    drwxr-xr-x. 2 root root    6 Jul  7 04:21 jjyy
    -rw-r--r--. 1 root root   17 Jul  7 02:36 oo
    -rw-r--r--. 1 root root   34 Jul  7 04:50 qqww
    -rw-r--r--. 1 root root   32 Jul  7 04:50 qqww.bak
    d---rwxrwx. 2 root root    6 Jul  7 04:22 xixi
[root@master ~]# 
[root@master ~]# salt '*' file.rename /root/qqww.bak /root/uu
node1:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root/'
node1:
    total 16
    drwxr-xr-x. 2 root root   30 Jul  7 03:38 abc
    -rw-------. 1 root root 1030 Jun  3 14:36 anaconda-ks.cfg
    drwxr-xr-x. 2 root root    6 Jul  7 04:21 jjyy
    -rw-r--r--. 1 root root   17 Jul  7 02:36 oo
    -rw-r--r--. 1 root root   34 Jul  7 04:50 qqww
    -rw-r--r--. 1 root root   32 Jul  7 04:50 uu
    d---rwxrwx. 2 root root    6 Jul  7 04:22 xixi

2.6.33 file.set_mode

给指定文件设置权限

[root@master ~]# salt '*' cmd.run 'ls -l /root/'
node1:
    total 16
    drwxr-xr-x. 2 root root   30 Jul  7 03:38 abc
    -rw-------. 1 root root 1030 Jun  3 14:36 anaconda-ks.cfg
    drwxr-xr-x. 2 root root    6 Jul  7 04:21 jjyy
    -rw-r--r--. 1 root root   17 Jul  7 02:36 oo
    -rw-r--r--. 1 root root   34 Jul  7 04:50 qqww
    -rw-r--r--. 1 root root   32 Jul  7 04:50 uu
    d---rwxrwx. 2 root root    6 Jul  7 04:22 xixi
    
[root@master ~]# salt '*' file.set_mode /root/uu 755
node1:
    0755
    
[root@master ~]# salt '*' cmd.run 'ls -l /root/'
node1:
    total 16
    drwxr-xr-x. 2 root root   30 Jul  7 03:38 abc
    -rw-------. 1 root root 1030 Jun  3 14:36 anaconda-ks.cfg
    drwxr-xr-x. 2 root root    6 Jul  7 04:21 jjyy
    -rw-r--r--. 1 root root   17 Jul  7 02:36 oo
    -rw-r--r--. 1 root root   34 Jul  7 04:50 qqww
    -rwxr-xr-x. 1 root root   32 Jul  7 04:50 uu
    d---rwxrwx. 2 root root    6 Jul  7 04:22 xixi

2.6.34 file.symlink

给指定的文件创建软链接

[root@master ~]# salt '*' cmd.run 'ls -l /root/'
node1:
    total 16
    drwxr-xr-x. 2 root root   30 Jul  7 03:38 abc
    -rw-------. 1 root root 1030 Jun  3 14:36 anaconda-ks.cfg
    drwxr-xr-x. 2 root root    6 Jul  7 04:21 jjyy
    -rw-r--r--. 1 root root   17 Jul  7 02:36 oo
    -rw-r--r--. 1 root root   34 Jul  7 04:50 qqww
    -rwxr-xr-x. 1 root root   32 Jul  7 04:50 uu
    d---rwxrwx. 2 root root    6 Jul  7 04:22 xixi
[root@master ~]# salt '*' file.symlink /root/uu /opt/tt
node1:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root;ls -l /opt/'
node1:
    total 16
    drwxr-xr-x. 2 root root   30 Jul  7 03:38 abc
    -rw-------. 1 root root 1030 Jun  3 14:36 anaconda-ks.cfg
    drwxr-xr-x. 2 root root    6 Jul  7 04:21 jjyy
    -rw-r--r--. 1 root root   17 Jul  7 02:36 oo
    -rw-r--r--. 1 root root   34 Jul  7 04:50 qqww
    -rwxr-xr-x. 1 root root   32 Jul  7 04:50 uu
    d---rwxrwx. 2 root root    6 Jul  7 04:22 xixi
    total 0
    drwxr-xr-x. 2 root root 30 Jul  7 03:38 abc
    -rw-r--r--. 1 root root  0 Jul  7 03:22 ii
    -rw-r--r--. 1 root root  0 Jul  7 03:18 pp
    lrwxrwxrwx. 1 root root  8 Jul  7 04:59 tt -> /root/uu
    drwxr-xr-x. 2 root root 26 Jul  7 03:22 uu

2.6.35 file.touch

创建空文件或更新时间戳

[root@master ~]# salt '*' cmd.run 'ls -l /opt'
node1:
    total 0
    drwxr-xr-x. 2 root root 30 Jul  7 03:38 abc
    -rw-r--r--. 1 root root  0 Jul  7 03:22 ii
    -rw-r--r--. 1 root root  0 Jul  7 03:18 pp
    lrwxrwxrwx. 1 root root  8 Jul  7 04:59 tt -> /root/uu
    drwxr-xr-x. 2 root root 26 Jul  7 03:22 uu
[root@master ~]# salt '*' file.touch /opt/cc
node1:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /opt'
node1:
    total 0
    drwxr-xr-x. 2 root root 30 Jul  7 03:38 abc
    -rw-r--r--. 1 root root  0 Jul  7 05:00 cc
    -rw-r--r--. 1 root root  0 Jul  7 03:22 ii
    -rw-r--r--. 1 root root  0 Jul  7 03:18 pp
    lrwxrwxrwx. 1 root root  8 Jul  7 04:59 tt -> /root/uu
    drwxr-xr-x. 2 root root 26 Jul  7 03:22 uu

2.6.36 file.uid_to_user

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

[root@master ~]# salt '*' file.uid_to_user 0
node1:
    root
[root@master ~]# salt '*' file.uid_to_user 1
node1:
    bin

2.6.37 file.user_to_uid

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

[root@master ~]# salt '*' file.user_to_uid apache
node1:
    48
[root@master ~]# salt '*' file.user_to_uid root
node1:
    0

2.6.38 file.write

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

[root@master ~]# salt '*' cmd.run 'cat /root/qqww'
node1:
    haha
    123
    xixi
    hello runtime hello
[root@master ~]# salt '*' file.write /root/qqww "I'm fine"
node1:
    Wrote 1 lines to "/root/qqww"
[root@master ~]# salt '*' cmd.run 'cat /root/qqww'
node1:
    I'm fine

2.7 SaltStack模板之user及group相关模块

2.7.1 useradd

在minion端上创建一个用户

[root@master ~]# salt 'node1' user.add yh
node1:
    True
[root@node1 ~]# id yh
uid=1000(yh) gid=1001(yh)=1001(yh)

2.7.2 user.info

返回用户信息

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

2.7.3 user.getent

返回所有系统用户信息的列表

[root@master ~]# salt 'node1' user.getent yh
node1:
    |_
      ----------
      fullname:
          root
      gid:
          0
      groups:
          - root
      home:
          /root
      homephone:
      name:
          root
      other:
      passwd:
          x
      roomnumber:
      shell:
          /bin/bash
      uid:
-----此处省略n行

2.7.4 user.list_groups

列出指定用户所属组的列表

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

2.7.5 user.rename

修改指定用户的用户名

[root@master ~]# salt 'node1' user.rename yh qqww
node1:
    False
//虽然显示是false,但是是成功修改了
[root@node1 ~]# id yh
id: “yh”:无此用户
[root@node1 ~]# id qqww
uid=1000(qqww) gid=1001(yh)=1001(yh)

2.7.6 user.delete

在minion端删除一个用户

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

2.7.7 user.chuid

修改指定用户的uid

[root@master ~]# salt 'node1' user.add oo
node1:
    True
[root@node1 ~]# id oo
uid=1000(oo) gid=1002(oo)=1002(oo)

[root@master ~]# salt 'node1' user.chuid oo 988
node1:
    True
[root@node1 ~]# id oo
uid=988(oo) gid=1002(oo)=1002(oo)

2.7.8 user.chgid

修改指定用户的gid

[root@master ~]# salt 'node1' user.chgid oo 988
node1:
    True
[root@node1 ~]# id oo
uid=988(oo) gid=988(setroubleshoot)=988(setroubleshoot)

2.7.9 group.add

添加指定用户组

[root@master ~]# salt 'node1' group.add zz 605
node1:
    True

2.7.10 group.info

返回用户组信息

[root@master ~]# salt 'node1' group.info zz
node1:
    ----------
    gid:
        605
    members:
    name:
        zz
    passwd:
        x

2.7.11 group.getent

返回所有用户组的信息

[root@master ~]# salt 'node1' group.getent
node1:
    |_
      ----------
      gid:
          0
      members:
      name:
          root
      passwd:
          x
    |_
      ----------
      gid:
          1
      members:
      name:
          bin
      passwd:
          x
    |_
      ----------
      gid:
          2
      members:
      name:
          daemon
      passwd:
------此处省略n行

2.7.12 group.adduser

添加一个用户到指定组中。(必须是一个已经存在的组和已存在的用户)

[root@master ~]# salt 'node1' group.adduser zz oo
node1:
    True
[root@master ~]# salt 'node1' user.info oo
node1:
    ----------
    fullname:
    gid:
        988
    groups:
        - setroubleshoot
        - zz     //有zz组
    home:
        /home/oo
    homephone:
    name:
        oo
    other:
    passwd:
        x
    roomnumber:
    shell:
        /bin/bash
    uid:
        604
    workphone:

2.7.13 group.deluser

将用户从用户组中移除

[root@master ~]# salt 'node1' group.deluser zz oo
node1:
    True
[root@master ~]# salt 'node1' user.info oo
node1:
    ----------
    fullname:
    gid:
        988
    groups:
        - setroubleshoot    //已从zz组 中移除
    home:
        /home/oo
    homephone:
    name:
        oo
    other:
    passwd:
        x
    roomnumber:
    shell:
        /bin/bash
    uid:
        604
    workphone:

2.7.14 group.delete

移除指定用户组

[root@master ~]# salt 'node1' group.delete zz
node1:
    True
[root@master ~]# salt 'node1' group.info zz
node1:
    ----------
//组zz已无数据

2.8 Saltstack常用模板之cron

2.8.1 cron.set_job

为指定用户设置计划任务

[root@master ~]# salt '*' cron.set_job root 20 10 '*' '*' 7 /bin/bash /scripts/apache_install.sh
master:
    new
node1:
    new
#如果这个用户存在任务,则变成更改任务
[root@master ~]# salt '*' cron.set_job root 20 10 '*' '*' 7 /bin/bash /scripts/nginx_install.sh
master:
    updated
node1:
    updated

[root@master ~]# salt '*' cron.set_job root 20 10 '*' '*' 6 /bin/bash /scripts/nginx_install.sh
master:
    updated
node1:
    updated

2.8.2 cron.list_tab

查看指定用户的计划任务

[root@master ~]# salt '*' cron.list_tab root
minion1:
    ----------
    crons:
        |_
          ----------
          cmd:
              /bin/bash
          comment:
              None
          commented:
              False
          daymonth:
              *
          dayweek:
              7
          hour:
              10
          identifier:
              None
          minute:
              20
          month:
              *
    env:
    pre:
    special:
master:
    ----------
    crons:
        |_
          ----------
          cmd:
              /bin/bash
          comment:
              None
          commented:
              False
          daymonth:
              *
          dayweek:
              7
          hour:
              10
          identifier:
              None
          minute:
              20
          month:
              *
    env:
    pre:
    special:

2.8.3 cron.raw_cron

查看指定用户的计划任务

[root@master ~]# salt '*' cron.raw_cron root
minion1:
    # Lines below here are managed by Salt, do not edit
    20 10 * * 7 /bin/bash
master:
    # Lines below here are managed by Salt, do not edit
    20 10 * * 7 /bin/bash

2.8.4 cron.rm_job

删除指定用户的计划任务、如果这个计划任务指定了时间,则需要指定的参数匹配的时候才会删除

[root@master ~]# salt '*' cron.rm_job root /bin/bash dayweek=7
minion1:
    removed
master:
    removed
[root@master ~]# salt '*' cron.rm_job root /bin/bash
master:
    removed
minion1:
    removed

2.9 SaltStack常用模块之acl

2.9.1 acl.modfacl

为指定的文件添加或修改FACL

[root@master ~]# salt '*' acl.modfacl user test2 rwx /tmp/yy
master:
    True
node1:
    True
[root@master ~]# getfacl /tmp/yy
getfacl: Removing leading '/' from absolute path names
# file: tmp/aa
# owner: root
# group: root
user::rw-
user:test2:rwx
group::r--
mask::rwx
other::r--

[root@master ~]# salt '*' acl.modfacl user test3 7 /tmp/yy
master:
    True
node1:
    True
[root@master ~]# getfacl /tmp/yy
getfacl: Removing leading '/' from absolute path names
# file: tmp/aa
# owner: root
# group: root
user::rw-
user:test2:rwx
user:test3:rwx
group::r--
mask::rwx
other::r--

2.9.2 acl.getfacl

查看指定文件的访问控制列表(FACL)

[root@master ~]# salt '*' acl.getfacl /tmp/aa
master:
    ----------
    /tmp/yy:
        ----------
        comment:
            ----------
            file:
                /tmp/yy
            group:
                root
            owner:
                root
        group:
            |_
              ----------
              root:
                  ----------
                  octal:

                      4
-----此处省略n行

minion1:
    ----------
    /tmp/yy:
        ----------
        comment:
            ----------
            file:
                /tmp/yy
            group:
                root
            owner:
                root              

2.9.3 acl.selfacl

从指定的文件中删除特定的FACL

[root@master ~]# salt '*' acl.delfacl user test2 /tmp/yy
node1:
    True
master:
    True
[root@master ~]# getfacl /tmp/yy
getfacl: Removing leading '/' from absolute path names
# file: tmp/yy
# owner: root
# group: root
user::rw-
user:test3:rwx
group::r--
mask::rwx
other::r--

2.9.4 acl.wipefacls

从指定的文件中删除所有facl

[root@master ~]# salt '*' acl.wipefacls /tmp/yy
node1:
    True
master:
    True
[root@master ~]# getfacl /tmp/yy
getfacl: Removing leading '/' from absolute path names
# file: tmp/yy
# owner: root
# group: root
user::rw-
group::r--
other::r--
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值