SaltStack常用的模块执行用法

SaltStack常用模块

SaltStack常用模块之network

  • network.active_tcp
  • 返回所有活动的tcp连接
[root@master ~]# salt '*' network.active_tcp
minion1:
    ----------
    0:
        ----------
        local_addr:
            192.168.149.153
        local_port:
            22
        remote_addr:
            192.168.149.200
        remote_port:
            53674
    1:
        ----------
        local_addr:
            192.168.149.153
        local_port:
此处有省略......
  • network.calc_net
  • 通过IP和子网掩码计算出网段
[root@master ~]# salt '*' network.calc_net 192.168.69.202 255.255.255.0
master:
    192.168.69.0/24
minion1:
    192.168.69.0/24
  • 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
minion1:
    ----------
    comment:
        Successfully connected to baidu.com (220.181.38.148) on tcp port 80
    result:
        True
  • network.default_route
  • 查看默认路由
[root@master ~]# salt '*' network.default_route
master:
    |_
      ----------
      addr_family:
          inet
      destination:
          0.0.0.0
      flags:
          UG
      gateway:
          192.168.149.2
      interface:
          ens160
      netmask:
          0.0.0.0
此处有省略......
minion1:
    |_
      ----------
      addr_family:
          inet
      destination:
          0.0.0.0
      flags:
          UG
      gateway:
          192.168.149.2
      interface:
          ens160
      netmask:
          0.0.0.0
    |_
此处有省略......
  • network.get_fqdn
  • 查看主机的fqdn(完全限定域名)
[root@master ~]# salt '*' network.get_fqdn
minion1:
    minion1
master:
    master
  • network.get_hostname
  • 获取主机名
[root@master ~]# salt '*' network.get_hostname
master:
    master
minion1:
    minion1
  • network.get_route
  • 查询到一个目标网络的路由信息
[root@master ~]# salt '*' network.get_route 192.168.149.152
master:
    ----------
    destination:
        192.168.149.152
    gateway:
        None
    interface:
        lo
    source:
        192.168.149.152
minion1:
    ----------
    destination:
        192.168.149.152
    gateway:
        None
    interface:
        ens160
    source:
        192.168.149.153
  • network.hw_addr
  • 返回指定网卡的MAC地址
[root@master ~]# salt '*' network.hw_addr ens160
master:
    00:0c:29:b4:ec:0b
minion1:
    00:0c:29:6e:16:32
  • network.ifacestartswith
  • 从特定CIDR检索接口名称
[root@master ~]# salt '*' network.ifacestartswith 192.168
master:
    - ens160
minion1:
    - ens160
  • network.in_subnet
  • 判断当前主机是否在某一个网段内
[root@master ~]# salt '*' network.in_subnet 192.168.149.0/24
master:
    True
minion1:
    True
  • network.interface
  • 返回指定网卡的信息
[root@master ~]# salt '*' network.interface ens160
master:
    |_
      ----------
      address:
          192.168.149.152
      broadcast:
          192.168.149.255
      label:
          ens160
      netmask:
          255.255.255.0
minion1:
    |_
      ----------
      address:
          192.168.149.153
      broadcast:
          192.168.149.255
      label:
          ens160
      netmask:
          255.255.255.0
  • network.interface_ip
  • 返回指定网卡的IP地址
[root@master ~]# salt '*' network.interface_ip ens160
master:
    192.168.149.152
minion1:
    192.168.149.153
  • network.interfaces
  • 返回当前系统中所有的网卡信息
[root@master ~]# salt '*' network.interfaces
master:
    ----------
    ens160:
        ----------
        hwaddr:
            00:0c:29:b4:ec:0b
        inet:
            |_
              ----------
              address:
                  192.168.149.152
              broadcast:
                  192.168.149.255
              label:
                  ens160
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
              address:
                  fe80::bab5:57cc:73a1:f92c
              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
minion1:
    ----------
    ens160:
        ----------
        hwaddr:
            00:0c:29:6e:16:32
        inet:
            |_
              ----------
              address:
                  192.168.149.153
              broadcast:
                  192.168.149.255
              label:
                  ens160
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
              address:
                  fe80::2b5f:526a:178f:24a9
              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 '*' network.ip_addrs
master:
    - 192.168.149.152
minion1:
    - 192.168.149.153
  • network.netstat
  • 返回所有打开的端口和状态
[root@master ~]# salt '*' network.active_tcp
master:
    ----------
    0:
        ----------
        local_addr:
            192.168.149.152
        local_port:
            4505
        remote_addr:
            192.168.149.153
        remote_port:
            45206
    1:
        ----------
        local_addr:
            192.168.149.152
        local_port:
            22
        remote_addr:
            192.168.149.200
        remote_port:
            53669
    2:
        ----------
        local_addr:
            192.168.149.152
        local_port:
            4505
        remote_addr:
            192.168.149.152
        remote_port:
            48914
    3:
        ----------
        local_addr:
            192.168.149.152
        local_port:
            22
        remote_addr:
            192.168.149.200
        remote_port:
            54742
    4:
        ----------
        local_addr:
            192.168.149.152
        local_port:
            48914
        remote_addr:
            192.168.149.152
        remote_port:
            4505
此处有省略......
  • network.ping
  • 使用ping命令测试到某主机的连通性
[root@master ~]# salt '*' network.ping baidu.com
minion1:
    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=32.10 ms
    64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=128 time=27.6 ms
    64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=128 time=29.2 ms
    64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=4 ttl=128 time=25.5 ms
    
    --- baidu.com ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 54ms
    rtt min/avg/max/mdev = 25.525/28.799/32.953/2.722 ms
master:
    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=49.1 ms
    64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=128 time=40.5 ms
    64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=3 ttl=128 time=42.9 ms
    64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=4 ttl=128 time=43.4 ms
    
    --- baidu.com ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 4062ms
    rtt min/avg/max/mdev = 40.466/43.994/49.127/3.170 ms
  • network.reverse_ip
  • 返回一个指定的IP地址的反向地址
[root@master ~]# salt '*' network.reverse_ip 192.168.149.152
master:
    152.149.168.192.in-addr.arpa
minion1:
    152.149.168.192.in-addr.arpa

SaltStack常用模块之service

  • service.available
  • 判断指定的服务是否可用
[root@master ~]# salt '*' service.available sshd
master:
    True
minion1:
    True
[root@master ~]# salt '*' service.available vsftpd
master:
    False
minion1:
    False
  • service.get_all
  • 获取所有正在运行的服务
[root@master ~]# salt '*' service.get_all
master:
    - NetworkManager
    - NetworkManager-dispatcher
    - NetworkManager-wait-online
    - arp-ethers
    - atd
    - auditd
    - autovt@
    - basic.target
    - blk-availability
    - bluetooth.target
    - boot-complete.target
    - chrony-dnssrv@
    - chrony-dnssrv@.timer
    - chrony-wait
    - chronyd
此处省略......
  • service.disabled
  • 检查指定服务是否开机不自动启动
[root@master ~]# salt '*' service.disabled httpd
master:
    False
minion1:
    False
  • service.enabled
  • 检查指定服务是否开机自动启动
[root@master ~]# salt '*' service.enabled httpd
master:
    True
minion1:
    True
  • service.disable
  • 设置指定服务开机不自动启动
[root@master ~]# salt '*' service.disable httpd
master:
    True
minion1:
    True
[root@master ~]# salt '*' service.enabled httpd
master:
    False
minion1:
    False
  • service.enable
  • 设置指定服务开机自动启动
[root@master ~]# salt '*' service.enable httpd
master:
    True
minion1:
    True
[root@master ~]# salt '*' service.enabled httpd
master:
    True
minion1:
    True
  • service.reload
  • 重新加载指定服务
[root@master ~]# salt '*' service.reload httpd
minion1:
    True
master:
    True
  • service.stop
  • 停止指定服务
[root@master ~]# salt '*' service.stop httpd
master:
    True
minion1:
    True
  • service.start
  • 启动指定服务
[root@master ~]# salt '*' service.start httpd
master:
    True
minion1:
    True
  • service.restart
  • 重启指定服务
[root@master ~]# salt '*' service.restart httpd
minion1:
    True
master:
    True
  • service.status
  • 查看指定服务的状态
[root@master ~]# salt '*' service.status httpd
master:
    True
minion1:
    True

SaltStack常用模块之pkg

  • pkg.download
  • 只下载软件包但不安装
    此功能将会下载指定的软件包,但是需要在minion端安装yum-utils,可以使用 cmd.run 进行远程安装
[root@master ~]# salt '*' pkg.download wget
minion1:
    ----------
    wget:
        /var/cache/yum/packages/wget-1.19.5-10.el8.x86_64.rpm
  • pkg.file_list
  • 列出指定包或系统中已安装的所有包的文件
列出已安装的apache软件包提供的所有文件
[root@master ~]# salt '*' pkg.file_list httpd
master:
    ----------
    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
此处省略......

当不提供参数时,将会列出当前系统中所有已安装软件的文件列表
[root@master ~]# salt '*' pkg.file_list
minion1:
    ----------
    errors:
    files:
        VALUE_TRIMMED
master:
    ----------
    errors:
    files:
        VALUE_TRIMMED
  • pkg.group_info
  • 查看包组的信息
[root@master ~]# salt '*' pkg.group_info 'Development Tools'
master:
    ----------
    conditional:
    default:
        - asciidoc
        - byacc
        - ctags
        - diffstat
        - elfutils-libelf-devel
此处省略......
  • pkg.group_list
  • 列出系统中所有的包组
[root@master ~]# salt '*' pkg.group_list
master:
    ----------
    available:
        - Backup Client
        - base-x
        - Conflicts AppStream
        - Container Management
        - Debugging Tools
        - Desktop Debugging and Performance Tools
        - .NET Core Development
此处省略......
  • pkg.install
  • 安装软件
[root@master ~]# salt '*' pkg.install wget
master:
    ----------
minion1:
    ----------
    wget:
        ----------
        new:
            1.19.5-10.el8
        old:
            1.19.5-8.el8_1.1
此处省略......
  • pkg.list_downloaded
  • 列出已下载到本地的软件包
[root@master ~]# salt '*' pkg.list_downloaded
master:
    ----------
minion1:
    ----------
  • pkg.list_pkgs
  • 以字典的方式列出当前已安装的软件包
[root@master ~]# salt '*' pkg.list_pkgs
minion1:
    ----------
    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
    PackageKit:
        1.1.12-4.el8
此处省略......
  • pkg.owner
  • 列出指定文件是由哪个包提供的
[root@master ~]# salt '*' pkg.owner /usr/sbin/apachectl
master:
    httpd
minion1:
    httpd
    
[root@master ~]# salt '*' pkg.owner /usr/sbin/apachectl /etc/httpd/conf/httpd.conf
master:
    ----------
    /etc/httpd/conf/httpd.conf:
        httpd
    /usr/sbin/apachectl:
        httpd
minion1:
    ----------
    /etc/httpd/conf/httpd.conf:
        httpd
    /usr/sbin/apachectl:
        httpd
  • pkg.remove
  • 卸载指定软件
[root@master ~]# salt '*' cmd.run 'rpm -qa|grep wget'
minion1:
    wget-1.19.5-10.el8.x86_64
master:
    wget-1.19.5-10.el8.x86_64
    
[root@master ~]# salt '*' pkg.remove wget
minion1:
    ----------
    wget:
        ----------
        new:
        old:
            1.19.5-10.el8
master:
    ----------
    wget:
        ----------
        new:
        old:
            1.19.5-10.el8
  • 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
这里我的包是最新的,只是演示            
若想升级系统中所有的软件包则把 name 参数去掉即可

SaltStack常用模块之state

  • state.show_highstate
  • 显示当前系统中有哪些高级状态
[root@master ~]# salt '*' state.show_highstate
master:
    ----------
    apache-install:
        ----------
        __env__:
            base
        __sls__:
            web.apache.install
        pkg:
            |_
              ----------
              name:
                  httpd
            - installed
            |_
              ----------
              order:
                  10000
    apache-service:
        ----------
此处省略......
  • state.highstate
  • 执行高级状态
[root@master ~]# salt '*' state.highstate web.apache.apache
master:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 06:41:47.640492
    Duration: 776.443 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 06:41:48.418897
    Duration: 63.675 ms
     Changes:   
此处省略......     
  • state.show_state_usage

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

  • state.show_top

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


  • state.top
  • 执行指定的top file,而不是默认的
[root@master ~]# salt '*' state.top xialuo.sls
master:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 06:45:40.560477
    Duration: 902.105 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
此处省略......
  • state.show_sls
  • 显示 master 上特定sls或sls文件列表中的状态数据
[root@master ~]# salt '*' state.show_sls web.apache.apache
master:
    ----------
    apache-install:
        ----------
        __env__:
            base
        __sls__:
            web.apache.apache
        pkg:
            |_
              ----------
              name:
                  httpd
            - installed
            |_
              ----------
              order:
                  10000
此处省略.....                  

SaltStack常用模块之salt-cp

  • salt-cp能够很方便的把 master 上的文件批量传到 minion上
拷贝单个文件到目标主机的/usr/src目录下
[root@master ~]# salt '*' cmd.run 'ls /usr/src/'
master:
    debug
    kernels
minion1:
    debug
    kernels
[root@master ~]# salt-cp '*' /etc/passwd /usr/src/
master:
    ----------
    /usr/src/passwd:
        True
minion:
    False
minion1:
    ----------
    /usr/src/passwd:
        True
[root@master ~]# salt '*' cmd.run 'ls /usr/src'
master:
    debug
    kernels
    passwd
minion1:
    debug
    kernels
    passwd

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

用户管理

  • 创建一个test用户
[root@master ~]# salt 'minion1' user.add test
minion1:
    True
  • 创建用户时指定shell
[root@master ~]# salt 'minion1' user.add test1 shell=/sbin/nologin
minion1:
    True
  • 创建用户时指定不创建家目录
[root@master ~]# salt 'minion1' user.add test3 createhome=False
minion1:
    True
  • 创建用户时指定附加组
[root@master ~]# salt 'minion1' user.add test4 groups=jjyy
minion1:
    True
  • 将test用户加入到nginx组,此为附加组
[root@master ~]# salt 'minion1' user.chgroups test nginx
  • 删除test3用户
[root@master ~]# salt 'minion1' user.delete test3 remove=True
minion1:
    True
  • 查看所有用户
[root@master ~]# salt 'minion1' user.list_users
minion1:
    - adm
    - apache
    - bin
    - test
    - test1
    - test4
此处省略......
  • 更改用户的shell
[root@master ~]# salt 'minion1' user.chshell tom /bin/czh
minion1:
    True

权限管理

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

[root@master ~]# salt 'minion1' acl.modfacl user root rwx /opt/xx
minion1:
    True
[root@minion1 opt]# ll
总用量 0
-rw-rwxr--+ 1 root root 0 7月   6 10:13 xx

[root@master ~]#  salt 'minion1' acl.modfacl u tom 7 /opt/xx
minion1:
    True
[root@minion1 opt]# ll
总用量 0
-rw-rwxr--+ 1 root root 0 7月   6 10:13 xx
  • 返回指定文件上facl的(极其详细的)映射
[root@master ~]# salt 'minion1' acl.getfacl /opt/xx
minion1:
    ----------
    /opt/xx:
        ----------
        comment:
            ----------
            file:
                /opt/xx
            group:
                root
            owner:
                root
        group:
            |_
              ----------
              root:
                  ----------
                  octal:
                      4
                  permissions:
                      ----------
                      execute:
                          False
                      read:
                          True
  • 从指定的文件中删除特定的FACL
[root@master ~]# salt 'minion1' acl.delfacl user tom /opt/xx
minion1:
    True
 [root@minion1 opt]# ll
总用量 0
-rw-r--r--+ 1 root root 0 7月   6 10:13 xx  
  • 从指定的文件中删除所有facl
[root@master ~]# salt 'minion1' acl.wipefacls /opt/xx
minion1:
    True
  • 从getfacl——version返回facl版本
[root@master ~]# salt 'minion1' acl.version
minion1:
    2.2.53

计划任务

  • 将文件的内容写入用户的crontab
[root@master ~]# salt 'minion1' cron.write_cron_file root /opt/xx
minion1:
    True
  • 返回用户crontab的内容
[root@master ~]# salt 'minion1' cron.raw_cron root
minion1:
    */1 * * * * usr/bin/date &>/root/opt/xx
  • 删除指定用户的cron作业
[root@master ~]# salt 'minion1' cron.rm_job root /opt/xx
minion1:
    absent
  • 返回指定用户crontab的内容
[root@master ~]# salt 'minion1' cron.list_tab root
minion1:
    ----------
    crons:
    env:
    pre:
        - */1 * * * * usr/bin/date &>/root/opt/xx
    special:
  • 将文件的内容写入用户的crontab并在出错时返回错误消息
[root@master ~]# salt 'minion1' cron.write_cron_file_verbose root /opt/xx
minion1:
    ----------
    pid:
        161870
    retcode:
        0
    stderr:
    stdout:
  • 删除指定用户的cron环境变量。
[root@master ~]#  salt 'minion1' cron.rm_env root MAILTO
minion1:
    absent
  • 为指定用户设置cron作业。
[root@master ~]# salt 'minion1' cron.set_job root '*' '*' '*' '*' 1 /usr/local/weekly
minion1:
    new
  • 在crontab中设置一个环境变量。
[root@master ~]# salt 'minion1' cron.set_env root MAILTO root@example.com
minion1:
    new
  • 删除指定用户的特殊cron作业
[root@master ~]# salt 'minion1' cron.rm_special root /opt/xx
[root@master base]# vim cron.sls
cron_test:
  cron.present:
    - name: /bin/touch /tmp/fansik.txt
    - user: root
    - minute: '*'
    - hour: 20
    - daymonth: 1-27
    - month: '3,5'
    - dayweek: '*'

[root@master base]# vim top.sls
base:
  minion:
    - cron

[root@master base]# salt '*' state.sls cron
minion1:
----------
          ID: cron_test
    Function: cron.present
        Name: /bin/touch /tmp/fansik.txt
      Result: True
     Comment: Cron /bin/touch /tmp/fansik.txt added to root's crontab
     Started: 11:18:46.217872
    Duration: 134.389 ms
     Changes:   
              ----------
              root:
                  /bin/touch /tmp/fansik.txt
此处省略.......                  

Salt 用于定义 cron 作业的各种计时值的参数:

minute

hour

daymonth

month

dayweek(0 至 6 是周日到周六,7 也可以用于星期日)

SaltStack常用模块之file

  • file.access
    检查指定路径是否存在
[root@master ~]# salt 'minion1' cmd.run 'ls /usr/src'
minion1:
    debug
    group
    kernels
    passwd
    shadow
[root@master ~]# salt 'minion1' file.access /usr/src/passwd f 
minion1:
    True

检查指定文件的权限信息
[root@master ~]# salt 'minion1' cmd.run 'ls -l /usr/src/'
minion1:
    total 12
    drwxr-xr-x. 2 root root    6 Aug 12  2018 debug
    -rw-r--r--. 1 root root  660 Jul  6 06:51 group
    drwxr-xr-x. 2 root root    6 Aug 12  2018 kernels
    -rw-r--r--. 1 root root 1517 Jul  6 06:50 passwd
    -rw-r--r--. 1 root root  841 Jul  6 06:51 shadow
[root@master ~]# salt 'minion1' file.access /usr/src/passwd r    是否有读权限
minion1:
    True
[root@master ~]# salt 'minion1' file.access /usr/src/passwd w    是否有写权限
minion1:
    True
[root@master ~]# salt 'minion1' file.access /usr/src/passwd x     是否有执行权限
minion1:
    False
  • file.append
    往一个文件里追加内容,若此文件不存在则会报异常
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/a'
minion1:
    -rw-r--r--. 1 root root 0 Jul  7 05:09 /root/a
[root@master ~]# salt 'minion1' file.append /root/a "hello world" "haha" "xixi"
minion1:
    Wrote 3 lines to "/root/a"
[root@master ~]# salt 'minion1' cmd.run 'cat /root/a'
minion1:
    hello world
    haha
    xixi
  • file.basename
    获取指定路径的基名
[root@master ~]# salt 'minion1' file.basename '/root/zabbix/abc'
minion1:
    abc
  • file.dirname
    获取指定路径的目录名
[root@master ~]# salt 'minion1' file.dirname '/root/zabbix/abc'
minion1:
    /root/zabbix
  • file.check_hash
    检查指定的文件与hash字符串是否匹配,匹配则返回 True 否则返回 False
[root@master ~]# salt 'minion1' cmd.run 'md5sum /etc/passwd'
minion1:
    8ff55a00710ac46454e5daee926ceef9  /etc/passwd
[root@master ~]# salt 'minion1' file.check_hash /etc/passwd 8ff55a00710ac46454e5daee926ceef9
minion1:
    True
  • file.chattr
    修改指定文件的属性
属性对文件的意义对目录的意义
a只允许在这个文件之后追加数据,
不允许任何进程覆盖或截断这个文件
只允许在这个目录下建立和修改文件,
而不允许删除任何文件
i不允许对这个文件进行任何的修改,
不能删除、更改、移动
任何的进程只能修改目录之下的文件,
不允许建立和删除文件
  • 给指定文件添加属性
查看当前属性
[root@master ~]# salt 'minion1' cmd.run 'lsattr /root'
minion1:
    -------------------- /root/anaconda-ks.cfg
    -------------------- /root/haha
    -------------------- /root/a
添加属性
[root@master ~]# salt 'minion1' file.chattr /root/a operator=add attributes=ai
minion1:
    True
[root@master ~]# salt 'minion1' cmd.run 'lsattr /root'
minion1:
    -------------------- /root/anaconda-ks.cfg
    -------------------- /root/haha
    ----ia-------------- /root/a
  • 给指定文件去除属性
[root@master ~]# salt 'minion1' file.chattr /root/a operator=remove attributes=i
minion1:
    True
[root@master ~]# salt 'minion1' cmd.run 'lsattr /root'
minion1:
    -------------------- /root/anaconda-ks.cfg
    -------------------- /root/haha
    -----a-------------- /root/a
  • file.chown
    设置指定文件的属主、属组信息
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/'
minion1:
    total 8
    -rw-r--r--. 1 root root   22 Jul  7 05:10 a
    -rw-------. 1 root root 1195 Jul  5 06:20 anaconda-ks.cfg
    dr--------. 2 tom  tom     6 Jul  7 04:21 haha
[root@master ~]# salt 'minion1' file.chown /opt/123 tom tom
minion1:
    None
[root@master ~]# salt 'minion1' cmd.run 'ls -l /opt/'
minion1:
    total 0
    -rw-r--r--. 1 tom  tom  0 Jul  7 03:50 123
    -rw-r-xr--+ 1 root root 0 Jul  6 10:13 xx
  • file.copy
    在远程主机上复制文件或目录

  • 拷贝文件

[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/'
minion1:
    total 8
    -rw-r--r--. 1 root root   22 Jul  7 05:10 a
    -rw-------. 1 root root 1195 Jul  5 06:20 anaconda-ks.cfg
    dr--------. 2 tom  tom     6 Jul  7 04:21 haha
[root@master ~]# salt 'minion1' file.copy /root/a /root/cc
minion1:
    True
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/'
minion1:
    total 12
    -rw-r--r--. 1 root root   22 Jul  7 05:10 a
    -rw-------. 1 root root 1195 Jul  5 06:20 anaconda-ks.cfg
    -rw-r--r--. 1 root root   22 Jul  7 06:11 cc
    dr--------. 2 tom  tom     6 Jul  7 04:21 haha
  • 覆盖并拷贝目录,将会覆盖同名文件或目录
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/'
minion1:
    total 12
    -rw-r--r--. 1 root root   22 Jul  7 05:10 a
    -rw-------. 1 root root 1195 Jul  5 06:20 anaconda-ks.cfg
    -rw-r--r--. 1 root root   22 Jul  7 06:11 cc
    dr--------. 2 tom  tom     6 Jul  7 04:21 haha
[root@master ~]# salt 'minion1' file.copy /tmp/ /root/abc recurse=True
minion1:
    True
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/'
minion1:
    total 16
    -rw-r--r--.  1 root root   22 Jul  7 05:10 a
    drwxrwxrwt. 14 root root 4096 Jul  7 06:12 abc
    -rw-------.  1 root root 1195 Jul  5 06:20 anaconda-ks.cfg
    -rw-r--r--.  1 root root   22 Jul  7 06:11 cc
    dr--------.  2 tom  tom     6 Jul  7 04:21 haha
  • 删除目标目录中同名的文件或目录并拷贝新内容至其中
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/abc'
minion1:
    total 8
    -rw-r--r--. 1 root root   31 Jul  7 06:12 crontab.xjNo1q
    -rw-r--r--. 1 root root 1379 Jul  7 06:12 ks-script-q0ld_cgi
    drwxr-xr-x. 3 root root   17 Jul  7 06:12 systemd-private-b7e32acdaad04b7b834a77554881af2d-chronyd.service-P58qzL
    drwxr-xr-x. 3 root root   17 Jul  7 06:12 systemd-private-b7e32acdaad04b7b834a77554881af2d-httpd.service-i077xo
    drwxr-xr-x. 2 root root    6 Jul  7 06:12 vmware-root_1002-2957518059
    drwxr-xr-x. 2 root root    6 Jul  7 06:12 vmware-root_948-2688554130
    drwxr-xr-x. 2 root root    6 Jul  7 06:12 vmware-root_956-2722239132
    drwxr-xr-x. 2 root root    6 Jul  7 06:12 vmware-root_988-2991203012
    drwxr-xr-x. 2 root root    6 Jul  7 06:12 vmware-root_990-2999657286
[root@master ~]# salt 'minion1' cmd.run 'ls -l /opt/abc'
minion1:
    total 0
    drwxr-xr-x. 2 root root 6 Jul  7 06:16 cc

拷贝目录    
[root@master ~]# salt 'minion1' file.copy /opt/abc/ /root/abc/ recurse=True remove_existing=True
minion1:
    True
[root@master ~]# salt 'minion1' cmd.run 'ls -l /opt/abc'
minion1:
    total 0
    drwxr-xr-x. 2 root root 6 Jul  7 06:16 cc
  • file.ditectory_exists
    判断指定目录是否存在,存在则返回 True ,否则返回 False
[root@master ~]# salt 'minion1' cmd.run 'ls -l /opt'
minion1:
    total 0
    -rw-r--r--. 1 tom  tom   0 Jul  7 03:50 123
    drwxr-xr-x. 3 root root 16 Jul  7 06:16 abc
    -rw-r-xr--+ 1 root root  0 Jul  6 10:13 xx
[root@master ~]# salt 'minion1' file.directory_exists /opt/abc
minion1:
    True
  • file.diskusage
    递归计算指定路径的磁盘使用情况并以字节为单位返回
[root@master ~]# salt 'minion1' cmd.run 'du -sb /opt'
minion1:
    60  /opt
[root@master ~]# salt 'minion1' file.diskusage /opt
minion1:
    0
  • file.file_exists
    判断指定文件是否存在
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/'
minion1:
    total 12
    -rw-r--r--. 1 root root   22 Jul  7 05:10 a
    drwxr-xr-x. 3 root root   16 Jul  7 06:16 abc
    -rw-------. 1 root root 1195 Jul  5 06:20 anaconda-ks.cfg
    -rw-r--r--. 1 root root   22 Jul  7 06:11 cc
    dr--------. 2 tom  tom     6 Jul  7 04:21 haha
[root@master ~]# salt 'minion1' file.file_exists /root/a
minion1:
    True
[root@master ~]# salt 'minion1' file.file_exists /root/abc
minion1:
    False       返回False是因为abc是目录而非文件
  • 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
salt 'minion1' file.find /opt type=d name=dd exec="rm -rf {}"    删除目录
  • file.get_gid
    获取指定文件的gid
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/a'
minion1:
    -rw-r--r--. 1 root root 22 Jul  7 05:10 /root/a
[root@master ~]# salt 'minion1' file.get_gid /root/a
minion1:
    0
  • file.get_group
    获取指定文件的组名
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/a'
minion1:
    -rw-r--r--. 1 root root 22 Jul  7 05:10 /root/a
[root@master ~]# salt 'minion1' file.get_group /root/a
minion1:
    root
  • file.get_hash
    获取指定文件的hash值,该值通过 sha256 算法得来
[root@master ~]# salt 'minion1' cmd.run 'sha256sum /root/a'
minion1:
    11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a  /root/a
[root@master ~]# salt 'minion1' file.get_hash /root/a
minion1:
    11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a
  • file.get_mode
    获取指定文件的权限,以数字方式显示
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/a'
minion1:
    -rw-r--r--. 1 root root 22 Jul  7 05:10 /root/a
[root@master ~]# salt 'minion1' file.get_mode /root/a
minion1:
    0644
  • file.get_selinux_context
    获取指定文件的 SELINUX 上下文信息
[root@master ~]# salt 'minion1' cmd.run 'ls -Z /root/a'
minion1:
    unconfined_u:object_r:admin_home_t:s0 /root/a
[root@master ~]# salt 'minion1' file.get_selinux_context /root/a
minion1:
    unconfined_u:object_r:admin_home_t:s0
  • file.get_sum
    按照指定的算法计算指定文件的特征码并显示,默认使用的sha256算法。
    该函数可使用的算法参数有:

  • md5

  • sha1

  • sha224

  • sha256 (default)

  • sha384

  • sha512

[root@master ~]# salt 'minion1' cmd.run 'sha256sum /root/a'
minion1:
    11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a  /root/a
[root@master ~]# salt 'minion1' file.get_sum /root/a
minion1:
    11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a
[root@master ~]# salt 'minion1' cmd.run 'md5sum /root/a'
minion1:
    671ded4ec86c82a8779c8df17823f810  /root/a
[root@master ~]# salt 'minion1' file.get_sum /root/a md5
minion1:
    671ded4ec86c82a8779c8df17823f810
  • file.get_uid与file.get_user
    获取指定文件的 uid 或 用户名
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/a'
minion1:
    -rw-r--r--. 1 root root 22 Jul  7 05:10 /root/a
[root@master ~]# salt 'minion1' file.get_uid /root/a
minion1:
    0
[root@master ~]# salt 'minion1' file.get_user /root/a
minion1:
    root
  • file.gid_to_group
    将指定的 gid 转换为组名并显示
[root@master ~]# salt 'minion1' file.gid_to_group 1000
minion1:
    tom
[root@master ~]# salt 'minion1' file.gid_to_group 0
minion1:
    root
  • file.group_to_gid
    将指定的组名转换为 gid 并显示
[root@master ~]# salt 'minion1' file.group_to_gid root
minion1:
    0
[root@master ~]# salt 'minion1' file.group_to_gid tom
minion1:
    1000
  • file.grep
    在指定文件中检索指定内容
    该函数支持通配符,若在指定的路径中用通配符则必须用双引号引起来
[root@master ~]# salt 'minion1' file.grep /etc/passwd nobody
minion1:
    ----------
    pid:
        464155
    retcode:
        0
    stderr:
    stdout:
        nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
[root@master ~]# salt 'minion1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens160 uuid -- -i
minion1:
    ----------
    pid:
        465370
    retcode:
        0
    stderr:
    stdout:
        UUID="67a36c7e-aee8-45af-902a-ecbab68a69ba"
[root@master ~]# salt 'minion1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens160 uuid -- -i -B2
minion1:
    ----------
    pid:
        465888
    retcode:
        0
    stderr:
    stdout:
        IPV6_ADDR_GEN_MODE="stable-privacy"
        NAME="ens160"
        UUID="67a36c7e-aee8-45af-902a-ecbab68a69ba"
[root@master ~]# salt 'minion1' file.grep "/etc/sysconfig/network-scripts/*" uuid -- -i -l
minion1:
    ----------
    pid:
        466741
    retcode:
        0
    stderr:
    stdout:
        /etc/sysconfig/network-scripts/ifcfg-ens160
  • file.is_blkdev
    判断指定的文件是否是块设备文件
[root@master ~]# salt 'minion1' cmd.run 'ls -l /dev/sr0'
minion1:
    brw-rw----. 1 root cdrom 11, 0 Jul  7 02:23 /dev/sr0
[root@master ~]# salt 'minion1' file.is_blkdev /dev/sr0
minion1:
    True
  • file.lsattr
    检查并显示出指定文件的属性信息
[root@master ~]# salt 'minion1' cmd.run 'lsattr /root/a'
minion1:
    -----a-------------- /root/a
[root@master ~]# salt 'minion1' cmd.run 'chattr +i /root/a'
minion1:
[root@master ~]# salt 'minion1' cmd.run 'lsattr /root/a'
minion1:
    ----ia-------------- /root/a
[root@master ~]# salt 'minion1' file.lsattr /root/a
minion1:
    ----------
    /root/a:
        - i
        - a
  • file.mkdir
    创建目录并设置属主、属组及权限
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root'
minion1:
    total 12
    -rw-r--r--. 1 root root   22 Jul  7 05:10 a
    drwxr-xr-x. 3 root root   16 Jul  7 06:16 abc
    -rw-------. 1 root root 1195 Jul  5 06:20 anaconda-ks.cfg
    -rw-r--r--. 1 root root   22 Jul  7 06:11 cc
    dr--------. 2 tom  tom     6 Jul  7 04:21 haha
[root@master ~]# salt 'minion1' file.mkdir /root/ab
minion1:
    True
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root'
minion1:
    total 12
    -rw-r--r--. 1 root root   22 Jul  7 05:10 a
    drwxr-xr-x. 2 root root    6 Jul  7 06:39 ab
    drwxr-xr-x. 3 root root   16 Jul  7 06:16 abc
    -rw-------. 1 root root 1195 Jul  5 06:20 anaconda-ks.cfg
    -rw-r--r--. 1 root root   22 Jul  7 06:11 cc
    dr--------. 2 tom  tom     6 Jul  7 04:21 haha
[root@master ~]# salt '*' file.mkdir /root/xx tom tom 622
minion1:
    True
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/'
minion1:
    total 12
    -rw-r--r--. 1 root root   22 Jul  7 05:10 a
    drwxr-xr-x. 2 root root    6 Jul  7 06:39 ab
    drwxr-xr-x. 3 root root   16 Jul  7 06:16 abc
    -rw-------. 1 root root 1195 Jul  5 06:20 anaconda-ks.cfg
    -rw-r--r--. 1 root root   22 Jul  7 06:11 cc
    dr--------. 2 tom  tom     6 Jul  7 04:21 haha
    drw--w--w-. 2 tom  tom     6 Jul  7 06:40 xx
  • file.move
    移动或重命名
重命名
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/'
minion1:
    total 16
    -rw-r--r--. 1 root root   22 Jul  7 05:10 a
    drwxr-xr-x. 2 root root    6 Jul  7 06:39 ab
    drwxr-xr-x. 3 root root   16 Jul  7 06:16 abc
    -rw-------. 1 root root 1195 Jul  5 06:20 anaconda-ks.cfg
    -rw-r--r--. 1 root root   22 Jul  7 05:10 b
    -rw-r--r--. 1 root root    0 Jul  7 06:48 c
    -rw-r--r--. 1 root root   22 Jul  7 06:11 cc
    dr--------. 2 tom  tom     6 Jul  7 04:21 haha
    drw--w--w-. 2 tom  tom     6 Jul  7 06:40 xx
[root@master ~]# salt 'minion1' file.move /root/b /root/c
minion1:
    ----------
    comment:
        '/root/b' moved to '/root/c'
    result:
        True
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/'
minion1:
    total 16
    -rw-r--r--. 1 root root   22 Jul  7 05:10 a
    drwxr-xr-x. 2 root root    6 Jul  7 06:39 ab
    drwxr-xr-x. 3 root root   16 Jul  7 06:16 abc
    -rw-------. 1 root root 1195 Jul  5 06:20 anaconda-ks.cfg
    -rw-r--r--. 1 root root   22 Jul  7 05:10 c
    -rw-r--r--. 1 root root   22 Jul  7 06:11 cc
    dr--------. 2 tom  tom     6 Jul  7 04:21 haha
    drw--w--w-. 2 tom  tom     6 Jul  7 06:40 xx

移动
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/'
minion1:
    total 8
    -rw-r--r--. 1 root root   22 Jul  7 05:10 a
    drwxr-xr-x. 2 root root    6 Jul  7 06:39 ab
    drwxr-xr-x. 3 root root   16 Jul  7 06:16 abc
    -rw-------. 1 root root 1195 Jul  5 06:20 anaconda-ks.cfg
[root@master ~]# salt 'minion1' cmd.run 'ls -l /opt/'
minion1:
    total 0
    -rw-r--r--. 1 tom  tom   0 Jul  7 03:50 123
    drwxr-xr-x. 3 root root 16 Jul  7 06:16 abc
    -rw-r-xr--+ 1 root root  0 Jul  6 10:13 xx
[root@master ~]# salt 'minion1' file.move /root/ab /opt/
minion1:
    ----------
    comment:
        '/root/ab' moved to '/opt/'
    result:
        True
[root@master ~]# salt 'minion1' cmd.run 'ls -l /opt/'
minion1:
    total 0
    -rw-r--r--. 1 tom  tom   0 Jul  7 03:50 123
    drwxr-xr-x. 2 root root  6 Jul  7 06:39 ab
    drwxr-xr-x. 3 root root 16 Jul  7 06:16 abc
    -rw-r-xr--+ 1 root root  0 Jul  6 10:13 xx
  • file.prepend
    把文本插入指定文件的开头
[root@master ~]# salt 'minion1' cmd.run 'cat /root/a'
minion1:
    hello world
    haha
    xixi
[root@master ~]# salt 'minion1' file.prepend /root/a "hehe"
minion1:
    Prepended 1 lines to "/root/a"
[root@master ~]# salt 'minion1' cmd.run 'cat /root/a'
minion1:
    hehe
    hello world
    haha
    xixi
  • file.sed
    修改文本文件的内容
[root@master ~]# salt 'minion1' cmd.run 'cat /root/a'
minion1:
    hehe
    hello world
    haha
    xixi
[root@master ~]# salt 'minion1' file.sed /root/a 'hello' 'runtime'
minion1:
    ----------
    pid:
        500852
    retcode:
        0
    stderr:
    stdout:
[root@master ~]# salt 'minion1' cmd.run 'cat /root/a'
minion1:
    hehe
    runtime world
    haha
    xixi
[root@master ~]# salt 'minion1' cmd.run 'cat /root/a'
minion1:
    hehe
    runtime world
    haha
    xixi
    hello hello
    hello hello hello
    hello hello hello hello
[root@master ~]# salt 'minion1' file.sed /root/a 'hello' 'runtime' flags=2
minion1:
    ----------
    pid:
        506544
    retcode:
        0
    stderr:
    stdout:
[root@master ~]# salt 'minion1' cmd.run 'cat /root/a'
minion1:
    hehe
    runtime world
    haha
    xixi
    hello runtime
    hello runtime hello
    hello runtime hello hello    
  • file.read
    读取文件内容
[root@master ~]# salt 'minion1' cmd.run 'cat /root/cca'
minion1:
    hello hello 
    hello world
[root@master ~]# salt 'minion1' file.read /root/cca
minion1:
    hello hello 
    hello world 
  • file.readdir
    列出指定目录下的所有文件或目录,包括隐藏文件
[root@master ~]# salt 'minion1' file.readdir /root
minion1:
    - .
    - ..
    - .bash_logout
    - .bash_profile
    - .bashrc
    - .cshrc
    - .tcshrc
    - anaconda-ks.cfg
    - .config
    - .bash_history
    - .viminfo
    - abc
    - a.bak
    - a
    - cca
  • file.remove
    删除指定的文件或目录,若给出的是目录,将递归删除
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/'
minion1:
    total 16
    -rw-r--r--. 1 root root   89 Jul  7 06:58 a
    -rw-r--r--. 1 root root   83 Jul  7 06:57 a.bak
    drwxr-xr-x. 3 root root   16 Jul  7 06:16 abc
    -rw-------. 1 root root 1195 Jul  5 06:20 anaconda-ks.cfg
    -rw-r--r--. 1 root root   26 Jul  7 06:59 cca
[root@master ~]# salt 'minion1' file.remove /root/a
minion1:
    True
[root@master ~]# salt 'minion1' file.remove /root/abc
minion1:
    True
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/'
minion1:
    total 12
    -rw-r--r--. 1 root root   83 Jul  7 06:57 a.bak
    -rw-------. 1 root root 1195 Jul  5 06:20 anaconda-ks.cfg
    -rw-r--r--. 1 root root   26 Jul  7 06:59 cca
  • file.rename
    重命名文件或目录
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/'
minion1:
    total 12
    -rw-r--r--. 1 root root   83 Jul  7 06:57 a.bak
    -rw-------. 1 root root 1195 Jul  5 06:20 anaconda-ks.cfg
    -rw-r--r--. 1 root root   26 Jul  7 06:59 cca
[root@master ~]# salt 'minion1' file.rename /root/a.bak /root/a
minion1:
    True
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/'
minion1:
    total 12
    -rw-r--r--. 1 root root   83 Jul  7 06:57 a
    -rw-------. 1 root root 1195 Jul  5 06:20 anaconda-ks.cfg
    -rw-r--r--. 1 root root   26 Jul  7 06:59 cca
  • file.set_mode
    给指定文件设置权限
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/'
minion1:
    total 12
    -rw-r--r--. 1 root root   83 Jul  7 06:57 a
    -rw-------. 1 root root 1195 Jul  5 06:20 anaconda-ks.cfg
    -rw-r--r--. 1 root root   26 Jul  7 06:59 cca
[root@master ~]# salt 'minion1' file.set_mode /root/a 0600
minion1:
    0600
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/'
minion1:
    total 12
    -rw-------. 1 root root   83 Jul  7 06:57 a
    -rw-------. 1 root root 1195 Jul  5 06:20 anaconda-ks.cfg
    -rw-r--r--. 1 root root   26 Jul  7 06:59 cca
  • file.symlink
    给指定的文件创建软链接
[root@master ~]# salt 'minion1' cmd.run 'ls -l /root/'
minion1:
    total 12
    -rw-------. 1 root root   83 Jul  7 06:57 a
    -rw-------. 1 root root 1195 Jul  5 06:20 anaconda-ks.cfg
    -rw-r--r--. 1 root root   26 Jul  7 06:59 cca
[root@master ~]# salt 'minion1' file.symlink /root/a /opt/a
minion1:
    True
[root@master ~]# salt 'minion1' cmd.run 'ls -l /opt/'
minion1:
    total 0
    -rw-r--r--. 1 tom  tom   0 Jul  7 03:50 123
    lrwxrwxrwx. 1 root root  7 Jul  7 07:05 a -> /root/a
    drwxr-xr-x. 2 root root  6 Jul  7 06:39 ab
    drwxr-xr-x. 3 root root 16 Jul  7 06:16 abc
    -rw-r-xr--+ 1 root root  0 Jul  6 10:13 xx
  • file.touch
    创建空文件或更新时间戳
[root@master ~]# salt 'minion1' cmd.run 'ls -l /opt/'
minion1:
    total 0
    -rw-r--r--. 1 tom  tom   0 Jul  7 03:50 123
    lrwxrwxrwx. 1 root root  7 Jul  7 07:05 a -> /root/a
    drwxr-xr-x. 2 root root  6 Jul  7 06:39 ab
    drwxr-xr-x. 3 root root 16 Jul  7 06:16 abc
    -rw-r-xr--+ 1 root root  0 Jul  6 10:13 xx
[root@master ~]# salt 'minion1' file.touch /opt/cc
minion1:
    True
[root@master ~]# salt 'minion1' file.touch /opt/aa
minion1:
    True
[root@master ~]# salt 'minion1' cmd.run 'ls -l /opt/'
minion1:
    total 0
    -rw-r--r--. 1 tom  tom   0 Jul  7 03:50 123
    lrwxrwxrwx. 1 root root  7 Jul  7 07:05 a -> /root/a
    -rw-r--r--. 1 root root  0 Jul  7 07:07 aa
    drwxr-xr-x. 2 root root  6 Jul  7 06:39 ab
    drwxr-xr-x. 3 root root 16 Jul  7 06:16 abc
    -rw-r--r--. 1 root root  0 Jul  7 07:06 cc
    -rw-r-xr--+ 1 root root  0 Jul  6 10:13 xx   
  • file.uid_to_user
    将指定的 uid 转换成用户名显示出来
[root@master ~]# salt 'minion1' file.uid_to_user 0
minion1:
    root
[root@master ~]# salt 'minion1' file.uid_to_user 1000
minion1:
    tom
  • file.user_to_uid
    将指定的用户转换成 uid 并显示出来
[root@master ~]# salt 'minion1' file.user_to_uid tom
minion1:
    1000
[root@master ~]# salt 'minion1' file.user_to_uid root
minion1:
    0
  • file.write
    往一个指定的文件里覆盖写入指定内容
[root@master ~]# salt 'minion1' cmd.run 'cat /root/a'
minion1:
    hehe
    runtime world
    haha
    xixi
    hello hello
    hello hello hello
    hello hello hello hello
[root@master ~]# salt 'minion1' file.write /root/a "I'm tom" "haha" "xixi"
minion1:
    Wrote 3 lines to "/root/a"
[root@master ~]# salt 'minion1' cmd.run 'cat /root/a'
minion1:
    I'm tom
    haha
    xixi

Saltstack常用模块之mysql

minion端配置

[root@minion ~]# vim /etc/salt/minion
mysql.host: 'localhost'
mysql.port: 3306
mysql.user: 'root'
mysql.pass: '123456'
mysql.db: 'mysql'
mysql.unix_socket: '/tmp/mysql.sock'
mysql.charset: 'utf8'

[root@minion ~]# salt-call --versions-report
Salt Version:
          Salt: 3003.1
 
Dependency Versions:
          cffi: Not Installed
      cherrypy: Not Installed
      dateutil: 2.6.1
     docker-py: Not Installed
         gitdb: Not Installed
     gitpython: Not Installed
        Jinja2: 2.10.1
       libgit2: Not Installed
      M2Crypto: 0.35.2
          Mako: Not Installed
       msgpack: 0.6.2
  msgpack-pure: Not Installed
  mysql-python: Not Installed
     pycparser: Not Installed
      pycrypto: Not Installed
  pycryptodome: Not Installed
        pygit2: Not Installed
        Python: 3.6.8 (default, Dec  3 2020, 18:11:24)
  python-gnupg: Not Installed
        PyYAML: 3.12
         PyZMQ: 19.0.0
         smmap: Not Installed
       timelib: Not Installed
       Tornado: 4.5.3
           ZMQ: 4.3.4
 
System Versions:
          dist: centos 8 
        locale: UTF-8
       machine: x86_64
       release: 4.18.0-257.el8.x86_64
        system: Linux
       version: CentOS Stream 8

Saltstack常用模块之lvs

  • lvs.add_service
    添加调度器并设置连接方式和调度算法。调度算法默认为wlc
[root@master ~]# salt master lvs.add_service tcp 192.168.149.152:80 rr
master:
    True
  • lvs.add_server
    添加RS并设置调度模式和权重
[root@master ~]# salt master lvs.add_server tcp 192.168.149.152:80 192.168.149.140:80 nat 1
master:
    True
[root@master ~]# ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  master:http rr
  -> 192.168.149.140:http         Masq    1      0          0    
  • lvs.check_server
    检查指定的RS是否在指定的调度规则中
[root@master ~]# salt master lvs.check_server tcp 192.168.149.152:80 192.168.149.140:80
master:
    True
  • lvs.check_service
    检查指定的调度规则是否存在
[root@master ~]# salt master lvs.check_service tcp 192.168.149.152:80
master:
    True
  • lvs.clear
    清除ipvsadm规则
[root@master ~]# salt master lvs.clear
master:
    True
[root@master ~]# ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值