saltstack常用模块

SaltStack模块介绍

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

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

# 查看所有module列表
[root@master ~]# salt 'node1' sys.list_modules
node1:
    - acl
    - aliases
    - alternatives
    - apache
    - archive
    - artifactory
    - baredoc
    - beacons
    - bigip
    - btrfs
    - buildout
    - chroot
    - cloud
    - cmd
······

# 查看指定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"' 
······

#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

SaltStack常用模块

SaltStack常用模块之network

network.active_tcp:返回所有活动的tcp连接

[root@master ~]# salt 'node1' network.active_tcp
node1:
    ----------
    0:
        ----------
        local_addr:
            192.168.8.131
        local_port:
            47912
        remote_addr:
            192.168.8.130
        remote_port:
            4505
    1:
        ----------
        local_addr:
            192.168.8.131
        local_port:
            22
        remote_addr:
            192.168.8.1
        remote_port:
            52128


network.calc_net:通过IP和子网掩码计算出网段

[root@master ~]# salt 'node1' network.calc_net 192.168.8.131 255.255.255.0
node1:
    192.168.8.0/24

[root@master ~]# salt 'node1' network.calc_net 192.168.8.131 255.255.255.255
node1:
    192.168.8.131/32

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

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

network.default_route:查看默认路由

[root@master ~]# salt 'node1' network.default_route 
node1:
    |_
      ----------
      addr_family:
          inet
      destination:
          0.0.0.0
      flags:
          UG
      gateway:
          192.168.8.2
      interface:
          ens160
      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:

network.get_fqdn:查看主机的fqdn(完全限定域名)

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

network.get_hostname:获取主机名

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

network.get_route:查询到一个目标网络的路由信息

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

etwork.hw_addr:返回指定网卡的MAC地址

[root@master ~]# salt 'node1' network.hw_addr ens160
node1:
    00:0c:29:56:d5:33

network.ifacestartswith:从特定CIDR检索接口名称

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

network.in_subnet:判断当前主机是否在某一个网段内

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

network.interface:返回指定网卡的信息

[root@master ~]# salt 'node1' network.interface ens160
node1:
    |_
      ----------
      address:
          192.168.8.131
      broadcast:
          192.168.8.255
      label:
          ens160
      netmask:
          255.255.255.0

network.interface_ip:返回指定网卡的IP地址

[root@master ~]# salt 'node1' network.interface_ip ens160
node1:
    192.168.8.131

network.interfaces:返回当前系统中所有的网卡信息

[root@master ~]# salt 'node1' network.interfaces
node1:
    ----------
    ens160:
        ----------
        hwaddr:
            00:0c:29:56:d5:33
        inet:
            |_
              ----------
              address:
                  192.168.8.131
              broadcast:
                  192.168.8.255
              label:
                  ens160
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
              address:
                  fe80::20c:29ff:fe56:d533
              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的地址列表,该函数将会忽略掉127.0.0.1的地址

[root@master ~]# salt 'node1' network.ip_addrs
node1:
    - 192.168.8.131

network.netstat:返回所有打开的端口和状态

[root@master ~]# salt 'node1' network.netstat 
node1:
    |_
      ----------
      inode:
          26269
      local-address:
          0.0.0.0:22
      program:
          976/sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          0.0.0.0:*
      send-q:
          0
      state:
          LISTEN
      user:
          0
    |_
      ----------
      inode:
          29408
      local-address:
          192.168.8.131:47912
      program:
          1583/python3.6
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.8.130:4505
      send-q:
          0
      state:
          ESTABLISHED
      user:
          0
    |_
      ----------
      inode:
          29776
      local-address:
          192.168.8.131:22
      program:
          1776/sshd:
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.8.1:52128
      send-q:
          0
      state:
          ESTABLISHED
      user:
          0
    |_
      ----------
      inode:
          27289
      local-address:
          :::80
      program:
          979/httpd
      proto:
          tcp6
      recv-q:
          0
      remote-address:
          :::*
      send-q:
          0
      state:
          LISTEN
      user:
          0
    |_
      ----------
      inode:
          26271
      local-address:
          :::22
      program:
          976/sshd
      proto:
          tcp6
      recv-q:
          0
      remote-address:
          :::*
      send-q:
          0
      state:
          LISTEN
      user:
          0

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

[root@master ~]# salt 'node1' network.ping baidu.com
node1:
    PING baidu.com (220.181.38.148) 56(84) bytes of data.
    64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=128 time=41.0 ms
    64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=128 time=38.8 ms
    64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=3 ttl=128 time=39.1 ms
    64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=4 ttl=128 time=38.10 ms
    
    --- baidu.com ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 8ms
    rtt min/avg/max/mdev = 38.838/39.489/41.020/0.911 ms

network.reverse_ip:返回一个指定的IP地址的反向地址

[root@master ~]# salt 'node1' network.reverse_ip 192.168.8.131
node1:
    131.8.168.192.in-addr.arpa

SaltStack常用模块之service

service.available:判断指定的服务是否可用

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

service.get_all:获取所有正在运行的服务

[root@master ~]# salt 'node1' service.get_all 
node1:
    - NetworkManager
    - NetworkManager-dispatcher
    - NetworkManager-wait-online
    - arp-ethers
    - auditd
    - autovt@
    - basic.target
    - blk-availability
    - bluetooth.target
    - console-getty
    - container-getty@
    - cpupower
    - crond
······

service.disabled:检查指定服务是否开机不自动启动

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

service.disable:设置指定服务开机不自动启动

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

service.enable:设置指定服务开机自动启动

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

service.stop:停止指定服务

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

service.start:启动指定服务

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

service.status:查看指定服务的状态

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

SaltStack常用模块之pkg

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

[root@master ~]# salt 'node1' pkg.download wget
node1:
    ----------
    wget:
        /var/cache/yum/packages/wget-1.19.5-10.el8.x86_64.rpm	#软件存放位置

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

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

#当不提供参数时,将会列出当前系统中所有已安装软件的文件列表
[root@master ~]# salt '*' pkg.file_list
node1:
    ----------
    errors:
    files:
        - /lib/kbd/keymaps/legacy
        - /lib/kbd/keymaps/legacy/amiga
        - /lib/kbd/keymaps/legacy/amiga/amiga-de.map.gz
        - /lib/kbd/keymaps/legacy/amiga/amiga-us.map.gz
        - /lib/kbd/keymaps/legacy/atari
        - /lib/kbd/keymaps/legacy/atari/atari-de.map.gz
 ······      
       

pkg.group_info:查看包组的信息

[root@master ~]# salt 'node1' pkg.group_info 'Development Tools'
node1:
    ----------
    conditional:
    default:
        - asciidoc
        - byacc
        - ctags
        - diffstat
        - elfutils-libelf-devel
        - git
        - intltool
        - jna
        - ltrace
        - patchutils
        - perl-Fedora-VSP
        - perl-Sys-Syslog
        - perl-generators
        - pesign

pkg.group_install

[root@master ~]# salt node1 pkg.group_install 'Development Tools'
···
    rpm-build:
        ----------
        new:
            4.14.3-14.el8_4
        old:
    rpm-build-libs:
        ----------
        new:
            4.14.3-14.el8_4
        old:
            4.14.2-37.el8
    rpm-libs:
        ----------
        new:
            4.14.3-14.el8_4
        old:
            4.14.2-37.el8
    rpm-plugin-selinux:
        ----------
        new:
            4.14.3-14.el8_4
···

pkg.group_list:列出系统中所有的包组

[root@master ~]# salt 'node1' pkg.group_list
node1:
    ----------
    available:
        - Conflicts BaseOS
        - Dial-up Networking Support
        - Hardware Monitoring Utilities
        - Hardware Support
        - Large Systems Performance
        - Legacy UNIX Compatibility
        - Python Web
        - Server product core
        - Windows File Server
        - Additional Development
        - Anaconda tools
        - Backup Client
        - Base

pkg.install:列出已下载到本地的软件包

[root@master ~]# salt '*' pkg.list_downloaded
node:
    ----------
    wget:
        ----------
        1.14-18.el7:
            ----------
            creation_date_time:
                2019-02-22T17:11:52
            creation_date_time_t:
                1550826712
            path:
                /var/cache/yum/packages/wget-1.14-18.el7.x86_64.rpm
            size:
                560152

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

[root@master ~]# salt 'node1' pkg.list_pkgs 
node1:
    ----------
    NetworkManager:
        1:1.22.8-4.el8
    NetworkManager-libnm:
        1:1.22.8-4.el8
    NetworkManager-team:
        1:1.22.8-4.el8
    NetworkManager-tui:
        1:1.22.8-4.el8
    acl:
        2.2.53-1.el8
    apr:
        1.6.3-11.el8
    apr-util:
······

pkg.owner:列出指定文件是由哪个包提供的

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

pkg.remove:卸载指定软件

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

pkg.upgrade:升级系统中所有的软件包或升级指定的软件包

[root@master ~]# salt 'node1' pkg.upgrade name=openssl
node1:
    ----------
    openssl:
        ----------
        new:
            1:1.1.1g-15.el8_3
        old:
            1:1.1.1c-15.el8
    openssl-libs:
        ----------
        new:
            1:1.1.1g-15.el8_3
        old:
            1:1.1.1c-15.el8
#升级系统中所有的软件包则去掉name参数

SaltStack常用模块之state

state.show_highstate:显示当前系统中有哪些高级状态

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

state.highstate:执行高级状态

[root@master ~]# salt 'node1' state.highstate webb.apache.install
node1:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 23:07:21.929361
    Duration: 473.81 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 23:07:22.412620
    Duration: 34.24 ms
     Changes:   

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

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

[root@master ~]# salt 'node1' state.show_state_usage 
node1:
    ----------
    base:
        ----------
        count_all:
            3
        count_unused:
            2
        count_used:
            1
        unused:
            - top
            - web.mysql.install
        used:
            - web.apache.install

state.show_top:返回minion将用于highstate的顶级数据

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

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

[root@master ~]# salt 'node1' state.top top.sls
node1:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 23:09:15.869470
    Duration: 475.13 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 23:09:16.346327
    Duration: 35.697 ms
     Changes:   

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

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

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

SaltStack常用模块之salt-cp

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

#将文件拷贝至目标主机
[root@master ~]# salt 'node1' cmd.run 'ls /usr/src'
node1:
    debug
    kernels
[root@master ~]# salt-cp 'node1' /etc/passwd /usr/src
node1:
    ----------
    /usr/src/passwd:
        True
[root@master ~]# salt 'node1' cmd.run 'ls /usr/src'
node1:
    debug
    kernels
    passwd

SaltStack常用模块之useradd

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

创建用户

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

更改用户名

[root@master ~]# salt 'node1' user.chfullname mike "MIKELL"
node1:
    True

更改用户的默认组

[root@master ~]# salt 'node1' user.chgid mike 1001
node1:
    True

更改此用户所属的组

[root@master ~]# salt 'node1' user.chgroups mike wheel,tom
node1:
    True

更改用户的默认shell

[root@master ~]# salt 'node1' user.chshell mike /bin/zsh
node1:
    True

更改指定用户的 uid

[root@master ~]# salt 'node1' user.chuid mike 1300
node1:
    True

从minion中删除一个用户

[root@master ~]# salt 'node1' user.delete mike remove=True force=True
node1:
    True

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

[root@master ~]# salt node1 user.getent 
node1:
    |_
      ----------
      fullname:
          root
      gid:
          0
      groups:
          - root
      home:
          /root
      homephone:
      name:
          root
      other:
      passwd:
          x
      roomnumber:
      shell:
          /bin/bash
······

返回用户信息

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

返回指定用户所属的组列表

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

更改命名用户的用户名

[root@master ~]# salt 'node1' user.rename tom toming
node1:
    False

SaltStack常用模块之file

file.access:检查指定路径是否存在

[root@master ~]# salt 'node1' file.access /usr/src/passwd f
node1:
    True

检查指定文件的权限信息

[root@master ~]# salt 'node1' cmd.run 'ls -l /usr/src'
node1:
    total 4
    drwxr-xr-x. 2 root root    6 Aug 12  2018 debug
    drwxr-xr-x. 2 root root    6 Aug 12  2018 kernels
    -rw-r--r--  1 root root 1106 Nov  3 23:12 passwd
[root@master ~]# salt 'node1' file.access /usr/src/passwd r
node1:
    True
[root@master ~]# salt 'node1' file.access /usr/src/passwd w
node1:
    True
[root@master ~]# salt 'node1' file.access /usr/src/passwd x
node1:
    False

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

[root@master ~]# salt 'node1' cmd.run 'ls -l /root/abc'
node1:
    -rw-r--r-- 1 root root 0 Nov  3 23:17 /root/abc
[root@master ~]# salt 'node1' file.append /root/abc "hello world"
node1:
    Wrote 1 lines to "/root/abc"
[root@master ~]# salt 'node1' cmd.run 'cat /root/abc'
node1:
    hello world

file.basename:获取指定路径的基名

[root@master ~]# salt 'node1' file.basename "root/abc"
node1:
    abc

file.dirname:获取指定路径的目录名

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

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

[root@master ~]# salt 'node1' file.check_hash /etc/passwd f9f8271eec653d3c8b0d6ac6ae217ecc
node1:
    True

file.chattr:修改指定文件的属性

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

给指定文件添加属性

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

给指定文件去除属性

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

file.chown:设置指定文件的属主、属组信息

[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 8
    -rw-r--r--  1 root root   12 Nov  3 23:18 abc
    -rw-------. 1 root root 1344 Oct 15 09:32 anaconda-ks.cfg
[root@master ~]# salt 'node1' file.chown /root/abc tom tom
node1:
    None
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 8
    -rw-r--r--  1 tom  tom    12 Nov  3 23:18 abc
    -rw-------. 1 root root 1344 Oct 15 09:32 anaconda-ks.cfg

file.copy:在远程主机上复制文件或目录
拷贝文件

[root@master ~]# salt 'node1' file.copy /root/abc /root/zxc
node1:
    True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 12
    -rw-r--r--  1 tom  tom    12 Nov  3 23:18 abc
    -rw-------. 1 root root 1344 Oct 15 09:32 anaconda-ks.cfg
    -rw-r--r--  1 tom  tom    12 Nov  3 23:33 zxc

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

[root@master ~]# salt 'node1' file.copy /tmp/ /root/abc recurse=True
node1:
    ERROR: Could not copy '/tmp/' to '/root/abc'
ERROR: Minions returned with non-zero exit code
[root@master ~]# salt 'node1' file.copy /tmp/ /root/abcd recurse=True
node1:
    True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
    total 16
    -rw-r--r--   1 tom  tom    12 Nov  3 23:18 abc
    drwxrwxrwt  15 root root 4096 Nov  3 23:35 abcd
    -rw-------.  1 root root 1344 Oct 15 09:32 anaconda-ks.cfg
    -rw-r--r--   1 tom  tom    12 Nov  3 23:33 zxc

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

[root@master ~]# salt 'node1' file.directory_exists /root/abcd
node1:
    True

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

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

file.file_exists:判断指定文件是否存在

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

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

示例:

salt '*' file.find / type=f name=\*.bak size=+10m
salt '*' file.find /var mtime=+30d size=+10m print=path,size,mtime
salt '*' file.find /var/log name=\*.[0-9] mtime=+30d size=+10m delete

[root@master ~]# salt 'node1' file.find /root/ name=\*.txt size=-10m   
node2:
    - /root/ABC
    - /root/test

file.get_gid:获取指定文件的gid

[root@master ~]# salt 'node1' cmd.run 'ls -l /root/abc'
node1:
    -rw-r--r-- 1 toming tom 12 Nov  3 23:18 /root/abc
[root@master ~]# salt 'node1' file.get_gid /root/abc
node1:
    1001

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

[root@master ~]# salt 'node1' cmd.run 'sha256sum /root/abc'
node1:
    a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447  /root/abc
[root@master ~]# salt 'node1' file.get_hash /root/abc
node1:
    a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447

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

[root@master ~]# salt 'node1' cmd.run 'ls -l /root/abc'
node1:
    -rw-r--r-- 1 toming tom 12 Nov  3 23:18 /root/abc
[root@master ~]# salt 'node1' file.get_mode /root/abc
node1:
    0644

file.get_selinux_context:获取指定文件的 SELINUX 上下文信息

[root@master ~]# salt 'node1' cmd.run 'ls -Z /root/anaconda-ks.cfg'
.node1:
    system_u:object_r:admin_home_t:s0 /root/anaconda-ks.cfg
[root@master ~]# salt 'node1' file.get_selinux_context /root/anaconda-ks.cfg
node1:
    system_u:object_r:admin_home_t:s0

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

  • md5
  • sha1
  • sha224
  • sha256 (default)
  • sha384
  • sha512
[root@master ~]# salt 'node1' cmd.run 'sha256sum /root/abc'
node1:
    a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447  /root/abc
[root@master ~]# salt 'node1' file.get_sum /root/abc
node1:
    a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447
[root@master ~]# salt 'node1' cmd.run 'md5sum /root/abc'
node1:
    6f5902ac237024bdd0c176cb93063dc4  /root/abc
[root@master ~]# salt 'node1' file.get_sum /root/abc md5
node1:
    6f5902ac237024bdd0c176cb93063dc4

file.get_uid与file.get_user:获取指定文件的 uid 或 用户名

[root@master ~]# salt 'node1' cmd.run 'ls -l /root/abc'
node1:
    -rw-r--r-- 1 toming tom 12 Nov  3 23:18 /root/abc
[root@master ~]# salt 'node1' file.get_uid /root/abc
node1:
    1001
[root@master ~]# salt 'node1' file.get_user /root/abc
node1:
    toming

file.gid_to_group:将指定的 gid 转换为组名并显示

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

file.group_to_gid:将指定的组名转换为 gid 并显示

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

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


[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens160 ipaddr -- -i		#i忽略大小写
node1:
    ----------
    pid:
        2037
    retcode:
        0
    stderr:
    stdout:
        IPADDR=192.168.8.131

[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens160 ipaddr -- -i -B2		#B显示上两行,A显示后两行,C显示前后两后
node1:
    ----------
    pid:
        2060
    retcode:
        0
    stderr:
    stdout:
        DEVICE="ens160"
        ONBOOT="yes"
        IPADDR=192.168.8.131

file.is_blkdev:判断指定的文件是否是块设备文件

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

file.lsattr:检查并显示出指定文件的属性信息

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

file.mkdir:创建目录并设置属主、属组及权限

[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
    total 16
    -rw-r--r--   1 toming tom    12 Nov  3 23:18 abc
    drwxrwxrwt  15 root   root 4096 Nov  3 23:35 abcd
    -rw-------.  1 root   root 1344 Oct 15 09:32 anaconda-ks.cfg
    -rw-r--r--   1 toming tom    12 Nov  3 23:33 zxc
[root@master ~]# salt node1 file.mkdir /root/test
node1:
    True
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
    total 16
    -rw-r--r--   1 toming tom    12 Nov  3 23:18 abc
    drwxrwxrwt  15 root   root 4096 Nov  3 23:35 abcd
    -rw-------.  1 root   root 1344 Oct 15 09:32 anaconda-ks.cfg
    drwxr-xr-x   2 root   root    6 Nov  5 00:07 test
    -rw-r--r--   1 toming tom    12 Nov  3 23:33 zxc
[root@master ~]# salt node1 file.mkdir /root/testone tom tom
node1:
    True
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
    total 16
    -rw-r--r--   1 toming tom    12 Nov  3 23:18 abc
    drwxrwxrwt  15 root   root 4096 Nov  3 23:35 abcd
    -rw-------.  1 root   root 1344 Oct 15 09:32 anaconda-ks.cfg
    drwxr-xr-x   2 root   root    6 Nov  5 00:07 test
    drwxr-xr-x   2 root   root    6 Nov  5 00:08 testone
    -rw-r--r--   1 toming tom    12 Nov  3 23:33 zxc

file.move:移动或重命名

#重命名
[root@master ~]# salt node1 file.move /root/abc /root/ABC
node1:
    ----------
    comment:
        '/root/abc' moved to '/root/ABC'
    result:
        True
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
    total 8
    -rw-r--r--  1 toming tom    12 Nov  3 23:18 ABC
    -rw-------. 1 root   root 1344 Oct 15 09:32 anaconda-ks.cfg
    drwxr-xr-x  2 root   root    6 Nov  5 00:07 test
    drwxr-xr-x  2 root   root    6 Nov  5 00:08 testone

#移动
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
    total 8
    -rw-r--r--  1 toming tom    12 Nov  3 23:18 ABC
    -rw-------. 1 root   root 1344 Oct 15 09:32 anaconda-ks.cfg
    drwxr-xr-x  2 root   root    6 Nov  5 00:07 test
    drwxr-xr-x  2 root   root    6 Nov  5 00:08 testone
[root@master ~]# salt node1 cmd.run 'ls -l /opt'
node1:
    total 0
[root@master ~]# salt node1 file.move /root/test /opt/
node1:
    ----------
    comment:
        '/root/test' moved to '/opt/'
    result:
        True
[root@master ~]# salt node1 cmd.run 'ls -l /opt'
node1:
    total 0
    drwxr-xr-x 2 root root 6 Nov  5 00:07 test

file.prepend:把文本插入指定文件的开头

[root@master ~]# salt node1 cmd.run 'cat /root/ABC'
node1:
    hello world
[root@master ~]# salt node1 file.prepend /root/ABC 'hehe' 'xixi'
node1:
    Prepended 2 lines to "/root/ABC"
[root@master ~]# salt node1 cmd.run 'cat /root/ABC'
node1:
    hehe
    xixi
    hello world

file.sed:修改文本文件的内容

[root@master ~]# salt node1 cmd.run 'cat /root/ABC'
node1:
    hehe
    xixi
    hello world
[root@master ~]# salt node1 file.sed   /root/ABC 'xixi' 'runtime'
node1:
    ----------
    pid:
        2219
    retcode:
        0
    stderr:
    stdout:
[root@master ~]# salt node1 cmd.run 'cat /root/ABC'
node1:
    hehe
    runtime
    hello world

file.prepend:把文本插入指定文件的开头

[root@master ~]# salt node1 cmd.run 'cat /root/ABC'
node1:
    hehe
    runtime
    hello world
[root@master ~]# salt node1 file.prepend /root/ABC '123456'
node1:
    Prepended 1 lines to "/root/ABC"
[root@master ~]# salt node1 cmd.run 'cat /root/ABC'
node1:
    123456
    hehe
    runtime
    hello world

file.read:读取文件内容

[root@master ~]# salt node1 file.read  /root/ABC
node1:
    123456
    hehe
    runtime
    hello world

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

[root@master ~]# salt node1 file.readdir /root
node1:
    - .
    - ..
    - .bash_logout
    - .bash_profile
    - .bashrc
    - .cshrc
    - .tcshrc
    - anaconda-ks.cfg
    - .bash_history
    - .viminfo
    - testone
    - ABC.bak
    - ABC

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

[root@master ~]# salt node1 cmd.run 'ls -l /root/'
node1:
    total 12
    -rw-r--r--  1 toming tom    32 Nov  5 00:19 ABC
    -rw-r--r--  1 toming tom    22 Nov  5 00:13 ABC.bak
    -rw-------. 1 root   root 1344 Oct 15 09:32 anaconda-ks.cfg
    drwxr-xr-x  2 root   root    6 Nov  5 00:08 testone
[root@master ~]# salt node1 file.remove /root/ABC
node1:
    True
[root@master ~]# salt node1 file.remove /root/ABC.bak
node1:
    True
[root@master ~]# salt node1 cmd.run 'ls -l /root/'
node1:
    total 4
    -rw-------. 1 root root 1344 Oct 15 09:32 anaconda-ks.cfg
    drwxr-xr-x  2 root root    6 Nov  5 00:08 testone

file.rename:重命名文件或目录

[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
    total 4
    -rw-r--r--  1 root root    0 Nov  5 22:27 abc
    -rw-------. 1 root root 1344 Oct 15 09:32 anaconda-ks.cfg
    drwxr-xr-x  2 root root    6 Nov  5 00:08 testone
[root@master ~]# salt node1 file.rename /root/abc /root/two
node1:
    True
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
    total 4
    -rw-------. 1 root root 1344 Oct 15 09:32 anaconda-ks.cfg
    drwxr-xr-x  2 root root    6 Nov  5 00:08 testone
    -rw-r--r--  1 root root    0 Nov  5 22:27 two

file.set_mode:给指定文件设置权限

[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
    total 4
    -rw-------. 1 root root 1344 Oct 15 09:32 anaconda-ks.cfg
    drwxr-xr-x  2 root root    6 Nov  5 00:08 testone
    -rw-r--r--  1 root root    0 Nov  5 22:27 two
[root@master ~]# salt node1 file.set_mode /root/two 400
node1:
    0400
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
    total 4
    -rw-------. 1 root root 1344 Oct 15 09:32 anaconda-ks.cfg
    drwxr-xr-x  2 root root    6 Nov  5 00:08 testone
    -r--------  1 root root    0 Nov  5 22:27 two

file.symlink:给指定的文件创建软链接

[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
    total 4
    -rw-------. 1 root root 1344 Oct 15 09:32 anaconda-ks.cfg
    drwxr-xr-x  2 root root    6 Nov  5 00:08 testone
    -r--------  1 root root    0 Nov  5 22:27 two
[root@master ~]# salt node1 file.symlink /root/two /root/abc
node1:
    True
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
    total 4
    lrwxrwxrwx  1 root root    9 Nov  5 22:32 abc -> /root/two
    -rw-------. 1 root root 1344 Oct 15 09:32 anaconda-ks.cfg
    drwxr-xr-x  2 root root    6 Nov  5 00:08 testone
    -r--------  1 root root    0 Nov  5 22:27 two

file.touch:创建空文件或更新时间戳

[root@master ~]# salt node1 file.touch /opt/www
node1:
    True
[root@master ~]# salt node1 cmd.run 'ls -l /opt'
node1:
    total 0
    drwxr-xr-x 2 root root 6 Nov  5 00:07 test
    -rw-r--r-- 1 root root 0 Nov  5 22:33 www

file.write:往一个指定的文件里覆盖写入指定内容

[root@master ~]# salt node1 cmd.run 'cat /root/abc'
node1:
[root@master ~]# salt node1 file.write /root/abc 'hello' 'world' 'runtime'
node1:
    Wrote 3 lines to "/root/abc"
[root@master ~]# salt node1 cmd.run 'cat /root/abc'
node1:
    hello
    world
    runtime

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值