文章目录

  • SaltStack模块介绍
  • SaltStack常用模块network
  • SaltStack常用模块之service
  • SaltStack常用模块之pkg
  • SaltStack常用模块之state
  • SaltStack常用模块之user
  • SaltStack常用模块之cron
  • SaltStack常用模块之acl
  • SaltStack常用模块之file
  • Saltstack的模块之lvs
  • Saltstack常用模块之mysql


SaltStack模块介绍

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

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

//查看所有module列表
[root@master salt]# salt 'minion' sys.list_modules
minion:
    - acl
    - aliases
    - alternatives
    - ansible
    - archive
    - artifactory
    - baredoc
    - beacons
    - bigip
    - btrfs
    - buildout
    - chroot
    - cloud
    - cmd
    - composer
···········下面省略了很多

//查看指定module的所有function
[root@master salt]# salt 'minion' sys.list_functions cmd
minion:
    - 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]# salt 'minion' 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.

    All parameters from :mod:`cmd.run_all <salt.modules.cmdmod.run_all>` except python_shell can be used.
········省略n行

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

SaltStack常用模块network

SaltStack常用模块之network
network.active_tcp

//返回所有活动的tcp连接数
[root@master ~]#  salt '*' network.active_tcp
minion:
    ----------
    0:
        ----------
        local_addr:
            192.168.31.139
        local_port:
            22
        remote_addr:
            192.168.31.1
        remote_port:
            50531
    1:
        ----------
············省略

network.calc_net

#通过IP和子网掩码计算出网段
[root@master ~]# salt '*' network.calc_net 192.168.69.202 255.255.255.0
minion:
    192.168.69.0/24
master:
    192.168.69.0/24
[root@master ~]# salt '*' network.calc_net 192.168.31.250 255.255.255.0
master:
    192.168.31.0/24
minion:
    192.168.31.0/24

network.connect

#测试minion至某一台服务器的网络是否连通
[root@master ~]# salt '*' network.connect baidu.com 80
master:
    ----------
    comment:
        Successfully connected to baidu.com (220.181.38.148) on tcp port 80
    result:
        True
minion:
    ----------
    comment:
        Successfully connected to baidu.com (39.156.69.79) on tcp port 80
    result:
        True

network.default_route

#查看默认路由
[root@master ~]# salt '*' network.default_route
minion:
    |_
      ----------
      addr_family:
          inet
      destination:
          0.0.0.0
      flags:
          UG
      gateway:
          192.168.31.2
      interface:
          ens33
      netmask:
          0.0.0.0
master:
    |_
      ----------
      addr_family:
          inet
      destination:
          0.0.0.0
      flags:
          UG
      gateway:
          192.168.31.2
      interface:
          ens33
      netmask:
          0.0.0.0

network.get_fqdn

#查看主机的fqdn(完全限定域名)
[root@master ~]# salt '*' network.get_fqdn
master:
    master
minion:
    minion

network.get_hostname

获取主机名
[root@master ~]# salt '*' network.get_hostname
minion:
    minion

network.get_route

#查询到一个目标网络的路由信息
[root@master ~]# salt '*' network.get_route 192.168.31.200
minion:
    ----------
    destination:
        192.168.31.200
    gateway:
        192.168.31.2
    interface:
        ens33
    source:
        192.168.31.139

network.hw_addr

#返回指定网卡的MAC地址
[root@master ~]# salt '*' network.hw_addr ens33
minion:
    00:0c:29:ad:28:07

network.ifacestartswith

#从特定CIDR检索接口名称
[root@master ~]# salt '*' network.ifacestartswith 192.168
minion:
    - ens33

network.in_subnet

#判断当前主机是否在某一个网段内
[root@master ~]# salt '*' network.in_subnet 192.168.31.0/24
minion:
    True

network.interface

#返回指定网卡的信息
[root@master ~]# salt '*' network.interface ens33
minion:
    |_
      ----------
      address:
          192.168.31.139
      broadcast:
          192.168.31.255
      label:
          ens33
      netmask:
          255.255.255.0

network.interface_ip

#返回指定网卡的IP地址
[root@master ~]# salt '*' network.interface_ip ens33
minion:
    192.168.31.139

network.interfaces

#返回当前系统中所有的网卡信息
[root@master ~]# salt '*' network.interfaces
minion:
    ----------
    ens33:
        ----------
        hwaddr:
            00:0c:29:ad:28:07
        inet:
            |_
              ----------
              address:
                  192.168.31.139
              broadcast:
                  192.168.31.255
              label:
                  ens33
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
              address:
                  fe80::ad88:29b1:d3a:3039
              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
minion:
    - 192.168.31.139

network.netstat

#返回所有打开的端口和状态
[root@master ~]# salt '*' network.netstat
minion:
    |_
      ----------
      inode:
          25228
      local-address:
          127.0.0.1:323
      program:
          chronyd
      proto:
          udp
      recv-q:
          0
      remote-address:
          0.0.0.0:*
      send-q:
          0
      user:
          0
    ········省略

network.ping

#使用ping命令测试到某主机的连通性
[root@master ~]# salt '*' network.ping baidu.com
minion:
    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=26.9 ms
    64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=128 time=26.7 ms
    64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=3 ttl=128 time=26.3 ms
    64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=4 ttl=128 time=24.9 ms
    
    --- baidu.com ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3006ms
    rtt min/avg/max/mdev = 24.913/26.213/26.909/0.807 ms

network.reverse_ip

返回一个指定的IP地址的反向地址
[root@master ~]# salt '*' network.reverse_ip 192.168.69.30
minion:
    30.69.168.192.in-addr.arpa

SaltStack常用模块之service

service.available

#判断指定的服务是否可用
[root@master ~]#  salt '*' service.available sshd
minion:
    True
[root@master ~]# salt '*' service.available vsftpd
minion:
    False

service.get_all

#获取所有正在运行的服务
[root@master ~]# salt '*' service.get_all
minion:
    - NetworkManager
    - NetworkManager-dispatcher
    - NetworkManager-wait-online
    - auditd
    - autovt@
    - basic.target
    - blk-availability
    - bluetooth.target
    - boot-complete.target
    - chrony-dnssrv@
    - chrony-dnssrv@.timer
    - chrony-wait
    - chronyd
    - console-getty
    - container-getty@
    - cpupower
    - crond
·······省略好多行

service.disabled

#检查指定服务是否开机不自动启动
[root@master ~]#  salt '*' service.disabled httpd
minion:
    True      是开机不自动启动

service.enabled

#检查指定服务是否开机自动启动
[root@master ~]#  salt '*' service.enabled httpd
minion:
    False       不是开机自动启动

service.disable

#设置指定服务开机不自动启动
[root@master ~]# salt '*' service.disable httpd
minion:
    True

[root@minion ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; ven>
   Active: inactive (dead)
     Docs: man:httpd.service(8)

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

service.enable

#设置指定服务开机自动启动
[root@master ~]# salt '*' service.enable httpd
minion:
    True

[root@minion ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vend>
   Active: inactive (dead)
     Docs: man:httpd.service(8)

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

service.reload

#重新加载指定服务
[root@master ~]# salt '*' service.reload httpd
minion:
    ERROR: httpd.service is not active, cannot reload.
ERROR: Minions returned with non-zero exit code
[root@master ~]# salt '*' service.reload httpd
minion:
    True

service.stop

停止指定服务
[root@master ~]# salt '*' service.stop httpd
minion:
    True
[root@minion ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vend>
   Active: inactive (dead) since Tue 2021-07-06 11:12:38 EDT; 9s ago
     Docs: man:httpd.service(8)
  Process: 310100 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code>
  Process: 309144 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code>
 Main PID: 309144 (code=exited, status=0/SUCCESS)
   Status: "Running, listening on: port 80"

7月 06 11:11:08 minion httpd[309144]: AH00558: httpd: Could not reliabl>
7月 06 11:11:08 minion systemd[1]: Started The Apache HTTP Server.
7月 06 11:11:18 minion httpd[309144]: Server configured, listening on: >
7月 06 11:11:19 minion systemd[1]: Reloading The Apache HTTP Server.
7月 06 11:11:34 minion httpd[310100]: AH00558: httpd: Could not reliabl>
7月 06 11:11:45 minion httpd[309144]: Server configured, listening on: >
7月 06 11:11:45 minion systemd[1]: Reloaded The Apache HTTP Server.
7月 06 11:12:37 minion systemd[1]: Stopping The Apache HTTP Server...
7月 06 11:12:38 minion systemd[1]: httpd.service: Succeeded.
7月 06 11:12:38 minion systemd[1]: Stopped The Apache HTTP Server.

service.start

启动指定服务
[root@master ~]# salt '*' service.start httpd
minion:
    True
[root@minion ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vend>
   Active: reloading (reload) since Tue 2021-07-06 11:13:50 EDT; 8s ago
     Docs: man:httpd.service(8)
  Process: 310100 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code>
 Main PID: 314072 (httpd)
   Status: "Reading configuration..."
    Tasks: 1 (limit: 11201)
   Memory: 3.6M
   CGroup: /system.slice/httpd.service
           └─314072 /usr/sbin/httpd -DFOREGROUND

7月 06 11:13:35 minion systemd[1]: Starting The Apache HTTP Server...
7月 06 11:13:50 minion httpd[314072]: AH00558: httpd: Could not reliabl>
7月 06 11:13:50 minion systemd[1]: Started The Apache HTTP Server.

service.restart

#重启指定服务
[root@master ~]# salt '*' service.restart httpd
minion:
    True

service.status

查看指定服务的状态
[root@master ~]# salt '*' service.status httpd
minion:
    True

SaltStack常用模块之pkg

pkg.download

#只下载软件包但不安装  此功能将会下载指定的软件包,但是需要在minion端安装yum-utils,可以使用 cmd.run 进行远程安装
[root@master ~]# salt '*' pkg.download wget
minion:
    ----------
    wget:
        /var/cache/yum/packages/wget-1.19.5-10.el8.x86_64.rpm   //下载好包的位置,可以传给指定的被控机

pkg.file_list

列出指定包或系统中已安装的所有包的文件
[root@master ~]# salt '*' pkg.file_list httpd     //列出已安装的apache软件包提供的所有文件
minion: 
    ----------
    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
        - /etc/httpd/conf.modules.d/00-mpm.conf
        - /etc/httpd/conf.modules.d/00-optional.conf
        - /etc/httpd/conf.modules.d/00-proxy.conf
        - /etc/httpd/conf.modules.d/00-systemd.conf
        - /etc/httpd/conf.modules.d/01-cgi.conf
··········后面还有好多

pkg.group_info

#查看包组的信息
[root@master ~]# salt '*' pkg.group_info 'Development Tools'
minion:
    ----------
    conditional:
    default:
        - asciidoc
        - byacc
        - ctags
        - diffstat
        - elfutils-libelf-devel
        - git
        - intltool
        - jna
        - ltrace
        - patchutils
        - perl-Fedora-VSP
        - perl-Sys-Syslog
        - perl-generators
        - pesign
        - source-highlight
        - systemtap
        - valgrind
        - valgrind-devel
    description:
        A basic development environment.
    group:
        Development Tools
    id:
        None
    mandatory:
        - autoconf
        - automake
        - binutils
        - bison
        - flex
        - gcc
        - gcc-c++
        - gdb
        - glibc-devel
        - libtool
        - make
        - pkgconf
        - pkgconf-m4
        - pkgconf-pkg-config
        - redhat-rpm-config
        - rpm-build
        - rpm-sign
        - strace
    optional:
        - cmake
        - expect
        - rpmdevtools
        - rpmlint
    type:
        package group

pkg.group_list

#列出系统中所有的包组
[root@master ~]# salt '*' pkg.group_list
minion:
    ----------
    available:
        - Backup Client
        - base-x
        - Conflicts AppStream
        - Container Management
        - Debugging Tools
        - Desktop Debugging and Performance Tools
        - .NET Core Development
        - FTP Server
        - GNOME Applications
        - Graphics Creation Tools
        - Guest Agents
        - Guest Desktop Agents
        - Input Methods
············省略

pkg.install

#安装软件
[root@master ~]#  salt '*' pkg.install wget
minion:
    ----------
    wget:
        ----------
        new:
            1.19.5-10.el8
        old:

pkg.list_downloaded

列出已下载到本地的软件包
[root@master ~]# salt '*' pkg.list_downloaded
minion:
    ----------

pkg.list_pkgs

#以字典的方式列出当前已安装的软件包
[root@master ~]# salt '*' pkg.list_pkgs
minion:
    ----------
    NetworkManager:
        1:1.30.0-0.3.el8
    NetworkManager-libnm:
        1:1.30.0-0.3.el8
    NetworkManager-team:
        1:1.30.0-0.3.el8
    NetworkManager-tui:
        1:1.30.0-0.3.el8
    abattis-cantarell-fonts:
        0.0.25-4.el8
    acl:
        2.2.53-1.el8
·········省略好多

pkg.remove

#卸载指定软件
[root@master ~]# salt '*' cmd.run 'rpm -qa|grep wget'
minion:
    wget-1.19.5-10.el8.x86_64
[root@master ~]# salt '*' pkg.remove wget
minion:
    ----------
    wget:
        ----------
        new:
        old:
            1.19.5-10.el8

pkg.upgrade

#升级系统中所有的软件包或升级指定的软件包
[root@master ~]#  salt '*' pkg.upgrade name=openssl
minion:
    ----------
    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
    minion:
        ----------
        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 '*' state.highstate web.apache.install
    minion:
    ----------
              ID: apache-install
        Function: pkg.installed
            Name: httpd
          Result: True
         Comment: All specified packages are already installed
         Started: 06:48:38.913647
        Duration: 1290.103 ms
         Changes:   
    ----------
              ID: apache-service
        Function: service.running
            Name: httpd
          Result: True
         Comment: The service httpd is already running
         Started: 06:48:40.249714
        Duration: 60.23 ms
         Changes:   

    Summary for minion1
    ------------
    Succeeded: 2
    Failed:    0
    ------------
    Total states run:     2
    Total run time:   1.350 s
    master:
    ----------
              ID: apache-install
        Function: pkg.installed
            Name: httpd
          Result: True
         Comment: All specified packages are already installed
         Started: 06:48:39.152690
        Duration: 1038.651 ms
         Changes:   
    ----------
              ID: apache-service
        Function: service.running
            Name: httpd
          Result: True
         Comment: The service httpd is already running
         Started: 06:48:40.266710
        Duration: 158.588 ms
         Changes:   

    Summary for master
    ------------
    Succeeded: 2
    Failed:    0
    ------------
    Total states run:     2
    Total run time:   1.197 s

state.show_state_usage

#显示当前系统中的高级状态执行情况
现在好像用不了,没效果。

state.show_top

返回minion将用于highstate的顶级数据
    [root@master ~]# salt '*' state.show_top
    minion:
        ----------
        base:
            - web.apache.install
    master:
        ----------
        base:
            - web.apache.install

state.top

#执行指定的top file,而不是默认的
    [root@master ~]# cd /srv/salt/base/
    [root@master base]# ls
    top.sls  web
    [root@master base]# mv top.sls aa.sls
    [root@master base]# salt '*' state.top aa.sls
   minion:
    ----------
              ID: apache-install
        Function: pkg.installed
            Name: httpd
          Result: True
         Comment: All specified packages are already installed
         Started: 06:52:13.194528
        Duration: 1040.863 ms
         Changes:   
    ----------
              ID: apache-service
        Function: service.running
            Name: httpd
          Result: True
         Comment: The service httpd is already running
         Started: 06:52:14.236902
        Duration: 67.992 ms
         Changes:   

    Summary for minion1
    ------------
    Succeeded: 2
    Failed:    0
    ------------
    Total states run:     2
    Total run time:   1.109 s

state.show_sls

#显示 master 上特定sls或sls文件列表中的状态数据
[root@master base]# salt '*' state.show_sls web.apache.install
minion:
    ----------
    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-cp '*' /root/abc /tmp
minion:
    ----------
    /tmp/abc:
        True

[root@minion ~]# ls /tmp/
abc
systemd-private-670bd06b4ff94b01bbdba9d5da5de17a-chronyd.service-dRD4vO
systemd-private-670bd06b4ff94b01bbdba9d5da5de17a-httpd.service-UrV5p2
vmware-root_903-3979774182

#拷贝多个文件到目标主机的/tmp/目录下
[root@master ~]# ls
aaa  abc  anaconda-ks.cfg
[root@master ~]# salt-cp '*' /root/abc /root/aaa /tmp
minion:
    ----------
    /tmp/aaa:
        True
    /tmp/abc:
        True
[root@minion ~]# ls /tmp/
aaa
abc

SaltStack常用模块之user

#user.add
#创建用户,可以指定uid,gid,groups,home,shell
#语法
salt '*' user.add name <uid> <gid> <groups> <home> <shell>
#创建一个test用户,其它都和useradd一样默认
[root@master ~]# salt '*' user.add tom
minion:
    True
[root@minion ~]# id tom
uid=1000(tom) gid=1000(tom) 组=1000(tom)

#创建用户时指定shell
[root@master ~]# salt '*' user.add test shell=/sbin/nologin
minion:
    True

#创建用户是指定不创建家目录
[root@master ~]# salt '*' user.add jerry createhome=False
minion:
    True
master:
    True
[root@minion ~]# ls /home/
test  tom

#创建用户时指定附加组
[root@master ~]# salt '*' user.add zhangsan groups=nginx
minion:
    True

user.chgroups

#将tom用户加入到nginx组,此为附加组
    [root@master ~]# salt '*' user.chgroups tom nginx
    minion:
        True
        [root@minion ~]# id tom
uid=1000(tom) gid=1000(tom) 组=1000(tom),1004(nginx)

user.list_groups test

#查看test用户的所有的组
[root@master ~]#  salt '*' user.list_groups tom
minion:
    - nginx
    - tom

user.delete

#删除用户
[root@master ~]# salt '*' user.delete tom remove=True
minion:
    True

user.list_users

#查看所有用户
[root@master ~]# salt '*' user.list_users
minion:
    - adm
    - apache
    - bin
    - chrony
    - daemon
    - dbus
    - ftp
    - games
    - halt
    - jerry
    - lp
    - mail
    - nginx
    - nobody
   ·······省略一些

SaltStack常用模块之cron

cron.set_job

#为指定用户设置计划任务
[root@master ~]# salt '*' cron.set_job root 30 20 '*' '*' 7 /bin/bash /scripts/apache_install.sh
minion:
    new
[root@master ~]#  salt '*' cron.set_job root 30 20 '*' '*' 7 /bin/bash /scripts/nginx_install.sh
minion:
    updated
[root@master ~]# salt '*' cron.set_job root 30 20 '*' '*' 6 /bin/bash /scripts/nginx_install.sh
minion:
    updated

cron.list_tab

#查看指定用户的计划任务
[root@master ~]# salt '*' cron.list_tab root
minion:
    ----------
    crons:
        |_
          ----------
          cmd:
              /bin/bash
          comment:
              None
          commented:
              False
          daymonth:
              *
          dayweek:
              6
          hour:
              20
          identifier:
              None
          minute:
              30
          month:
              *
    env:
    pre:
    special:

cron.raw_cron

#查看指定用户的计划任务
[root@master ~]# salt '*' cron.raw_cron root
minion:
    # Lines below here are managed by Salt, do not edit
    30 20 * * 6 /bin/bash

cron.rm_job

#删除指定用户的计划任务、如果这个计划任务指定了时间,则需要指定的参数匹配的时候才会删除
[root@master ~]# salt '*' cron.rm_job root
minion:
    Passed invalid arguments to cron.rm_job: rm_job() missing 1 required positional argument: 'cmd'
    
        Remove a cron job for a specified user. If any of the day/time params are
        specified, the job will only be removed if the specified params match.
    
        CLI Example:
    
        .. code-block:: bash
    
            salt '*' cron.rm_job root /usr/local/weekly
            salt '*' cron.rm_job root /usr/bin/foo dayweek=1
        
ERROR: Minions returned with non-zero exit code
//报错了

[root@master ~]# salt '*' cron.rm_job root /bin/bash
minion:
    removed

SaltStack常用模块之acl

acl.modfacl

#为指定的文件添加或修改FACL
[root@master ~]# salt '*' acl.modfacl user jerry rwx /tmp/aaa
minion:
    True
[root@minion ~]# getfacl /tmp/aaa
getfacl: Removing leading '/' from absolute path names
# file: tmp/aaa
# owner: root
# group: root
user::rw-
user:jerry:rwx
group::r--
mask::rwx
other::r--

[root@master ~]# salt '*' acl.modfacl user jerry 7 /tmp/aaa
minion:
    True

acl.getfacl

#查看指定文件的访问控制列表(FACL)
[root@master ~]# salt '*' acl.getfacl /tmp/aaa
minion:
    ----------
    /tmp/aaa:
        ----------
        comment:
            ----------
            file:
                /tmp/aaa
            group:
                root
            owner:
                root
        group:
············省略n行

acl.delfacl

#从指定的文件中删除特定的FACL
[root@master ~]# salt '*' acl.delfacl user jerry /tmp/aaa
minion:
    True

[root@minion ~]# getfacl /tmp/aaa
getfacl: Removing leading '/' from absolute path names
# file: tmp/aaa
# owner: root
# group: root
user::rw-
group::r--
mask::r--
other::r--

acl.wipefacls

#从指定的文件中删除所有facl
[root@master ~]# salt '*' acl.wipefacls /tmp/aaa
minion:
    True
[root@minion ~]# getfacl /tmp/aaa
getfacl: Removing leading '/' from absolute path names
# file: tmp/aaa
# owner: root
# group: root
user::rw-
group::r--
other::r--

SaltStack常用模块之file

file.access

#检查指定路径是否存在
[root@master ~]#  salt '*' cmd.run 'ls /usr/src'
minion:
    debug
    kernels
[root@minion ~]# ls /usr/src/
debug  kernels
[root@master ~]# salt '*' file.access /usr/src/passwd f
minion:
    False
[root@master ~]# salt '*' file.access /usr/src/debug f
minion:
    True
#检查指定文件的权限信息
[root@master ~]# salt '*' cmd.run 'ls -l /usr/src/'
minion:
    total 0
    drwxr-xr-x. 2 root root 6 May 18  2020 debug
    drwxr-xr-x. 2 root root 6 May 18  2020 kernels

[root@minion opt]# ll
总用量 0
-rw-r--r--. 1 root root 0 7月   7 06:17 aa
drwxr-xr-x. 2 root root 6 7月   7 06:17 bb
[root@master ~]# salt '*' file.access /opt/aa r 
minion:
    True

file.append

#往一个文件里追加内容,若此文件不存在则会报异常
[root@master ~]# salt '*' file.append /opt/aa "hello world" "haha" "xixi"
minion:
    Wrote 3 lines to "/opt/aa"
[root@minion opt]# cat aa
hello world
haha
xixi

file.basename

#获取指定路径的基名
[root@master ~]# salt '*' file.basename '/root/zabbix/abc/a'
minion:
    a

file.dirname

#获取指定路径的目录名
[root@master ~]# salt '*' file.dirname '/root/zabbix/abc/a'
minion:
    /root/zabbix/abc

file.check_hash

#检查指定的文件与hash字符串是否匹配,匹配则返回 True 否则返回 False
[root@master ~]# salt '*' cmd.run 'md5sum /opt/aa'
minion:
    671ded4ec86c82a8779c8df17823f810  /opt/aa
[root@master ~]# salt '*' file.check_hash /opt/aa 671ded4ec86c82a8779c8df17823f810
minion:
    True

file.chattr

属性

对文件的意义

对目录的意义

a

只允许在这个文件之后追加数据,

不允许任何进程覆盖或截断这个文件

只允许在这个目录下建立和修改文件,

而不允许删除任何文件

i

不允许对这个文件进行任何的修改,

不能删除、更改、移动

任何的进程只能修改目录之下的文件,

不允许建立和删除文件

#修改指定文件的属性
[root@master ~]# salt '*' file.chattr /root/a operator=add attributes=a
minion:
    True
[root@master ~]# salt '*' file.chattr /root/ab operator=add attributes=i
minion:
    True
[root@minion ~]# lsattr /root
-------------------- /root/anaconda-ks.cfg
----ia-------------- /root/abc
-----a-------------- /root/a
----i--------------- /root/ab
[root@minion ~]# rm -rf ab
rm: 无法删除'ab': 不允许的操作
[root@minion ~]# rm -rf abc
rm: 无法删除'abc': 不允许的操作
[root@minion ~]# echo 'aa' > ab
-bash: ab: 不允许的操作
[root@minion ~]# echo 'aa' > a
-bash: a: 不允许的操作
[root@minion ~]# rm -rf a
#给指定文件去除属性
[root@master ~]# salt '*' file.chattr /root/a operator=remove attributes=ia
minion:
    True
[root@master ~]# salt '*' file.chattr /root/ab operator=remove attributes=i
minion:
    True
[root@minion ~]# lsattr /root
-------------------- /root/anaconda-ks.cfg
----ia-------------- /root/abc
-------------------- /root/a
-------------------- /root/ab
-------------------- /root/a~
-------------------- /root/z~

file.chown

#设置指定文件的属主、属组信息
[root@master ~]# salt '*' cmd.run 'ls -l /root/'
minion:
    total 4
    -rw-r--r--. 1 root root    0 Jul  7 06:38 a
    -rw-r--r--. 1 root root    0 Jul  7 06:40 ab
    -rw-r--r--. 1 root root    0 Jul  7 06:38 abc
    -rw-------. 1 root root 1069 May 11 04:14 anaconda-ks.cfg
[root@master ~]# salt '*' file.chown /root/a test test
minion:
    None
[root@master ~]# salt '*' cmd.run 'ls -l /root/'
minion:
    total 4
    -rw-r--r--. 1 test test    0 Jul  7 06:38 a
    -rw-r--r--. 1 root root    0 Jul  7 06:40 ab
    -rw-r--r--. 1 root root    0 Jul  7 06:38 abc
    -rw-------. 1 root root 1069 May 11 04:14 anaconda-ks.cfg

file.copy

#在远程主机上复制文件或目录
[root@master ~]# salt '*' cmd.run 'ls -l /root'
minion:
    total 4
    -rw-r--r--. 1 test test    0 Jul  7 06:38 a
    -rw-r--r--. 1 root root    0 Jul  7 06:40 ab
    -rw-r--r--. 1 root root    0 Jul  7 06:38 abc
    -rw-------. 1 root root 1069 May 11 04:14 anaconda-ks.cfg
[root@master ~]# salt '*' file.copy /root/a /root/cc
minion:
    True
[root@master ~]#  salt '*' cmd.run 'ls -l /root'
minion:
    total 4
    -rw-r--r--. 1 test test    0 Jul  7 06:38 a
    -rw-r--r--. 1 root root    0 Jul  7 06:40 ab
    -rw-r--r--. 1 root root    0 Jul  7 06:38 abc
    -rw-------. 1 root root 1069 May 11 04:14 anaconda-ks.cfg
    -rw-r--r--. 1 test test    0 Jul  7 11:47 cc
#覆盖并拷贝目录,将会覆盖同名文件或目录
[root@master ~]# salt '*' file.copy /tmp/abc /root/a recurse=True
minion:
    True
#删除目标目录中同名的文件或目录并拷贝新内容至其中
[root@master ~]#  salt '*' file.copy /opt/aaaa /root/aaa/ recurse=True remove_existing=True
minion:
    True
[root@master ~]#  salt '*' cmd.run 'ls -l /root/aaa'
minion:
    total 0
    drwxr-xr-x. 2 root root 6 Jul  7 11:58 1
    drwxr-xr-x. 2 root root 6 Jul  7 11:58 2
    drwxr-xr-x. 2 root root 6 Jul  7 11:58 3
    drwxr-xr-x. 2 root root 6 Jul  7 11:58 4
    drwxr-xr-x. 2 root root 6 Jul  7 11:58 57
[root@master ~]#  salt '*' cmd.run 'ls -l /opt/aaaa'
minion:
    total 0
    drwxr-xr-x. 2 root root 6 Jul  7 11:58 1
    drwxr-xr-x. 2 root root 6 Jul  7 11:58 2
    drwxr-xr-x. 2 root root 6 Jul  7 11:58 3
    drwxr-xr-x. 2 root root 6 Jul  7 11:58 4
    drwxr-xr-x. 2 root root 6 Jul  7 11:58 57
#拷贝目录
[root@master ~]#  salt '*' file.copy /opt/aaaa/ /root/aaa/ recurse=True remove_existing=True
minion:
    True

file.ditectory_exists

#判断指定目录是否存在,存在则返回 True ,否则返回 False
[root@master ~]# salt '*' cmd.run 'ls -l /opt'
minion:
    total 4
    -rw-r--r--. 1 root root 22 Jul  7 06:18 aa
    drwxr-xr-x. 7 root root 52 Jul  7 11:58 aaaa
    -rw-r--r--. 1 root root  0 Jul  7 11:54 abc
    drwxr-xr-x. 2 root root  6 Jul  7 06:17 bb
[root@master ~]#  salt '*' file.directory_exists /opt/aaaa
minion:
    True
[root@master ~]#  salt '*' file.directory_exists /opt/aa
minion:
    False

file.diskusage

#递归计算指定路径的磁盘使用情况并以字节为单位返回
[root@master ~]# salt '*' cmd.run 'du -sb /opt'
minion:
    159 /opt
[root@master ~]# salt '*' file.diskusage /opt
minion:
    22

file.file_exists

#判断指定文件是否存在
[root@master ~]# salt '*' cmd.run 'ls -l /root/'
minion:
    total 4
    -rw-r--r--. 1 root root    0 Jul  7 11:51 a
    drwxr-xr-x. 7 root root   52 Jul  7 11:58 aaa
    -rw-r--r--. 1 root root    0 Jul  7 06:38 abc
    -rw-------. 1 root root 1069 May 11 04:14 anaconda-ks.cfg
[root@master ~]# salt '*' file.file_exists /root/a
minion:
    True
[root@master ~]#  salt '*' file.file_exists /root/aaa
minion:
    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

例子

[root@master ~]# salt '*' file.find / type=f name=\*.bak size=-10m   //查询/下面以.bak结尾且大小小于10m的文件
minion:
    - /etc/nsswitch.conf.bak
salt '*' file.find /var mtime=+30d size=+10m print=path,size,mtime   //查询/var下时间在30天以前,大小在10M以上的,打印出路径,大小,时间
salt '*' file.find /var/log name=\*.[0-9] mtime=+30d size=+10m delete  //查询/var/log下面,以数字结尾,时间在30天以上大小大于10m的,删除

file.get_gid

#获取指定文件的gid
[root@master ~]#  salt '*' cmd.run 'ls -l /root/a'
minion:
    -rw-r--r--. 1 root root 0 Jul  7 11:51 /root/a
[root@master ~]# salt '*' file.get_gid /root/a
minion:
    0

file.get_group

获取指定文件的组名
[root@master ~]# salt '*' file.get_group /root/a
minion:
    root

file.get_hash

#获取指定文件的hash值,该值通过 sha256 算法得来
[root@master ~]# salt '*' cmd.run 'sha256sum /root/a'
minion:
    e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855  /root/a
[root@master ~]# salt '*' file.get_hash /root/a
minion:
    e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

file.get_mode

获取指定文件的权限,以数字方式显示
[root@master ~]# salt '*' file.get_mode /root/a
minion:
    0644
[root@master ~]# salt '*' cmd.run 'ls -l /root/a'
minion:
    -rw-r--r--. 1 root root 0 Jul  7 11:51 /root/a

file.get_selinux_context

#获取指定文件的 SELINUX 上下文信息
[root@master ~]# salt '*' file.get_selinux_context /root/a
minion:
    unconfined_u:object_r:admin_home_t:s0
[root@master ~]# salt '*' cmd.run 'ls -Z /root/a'
minion:
    unconfined_u:object_r:admin_home_t:s0 /root/a

file.get_sum

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

  • md5
  • sha1
  • sha224
  • sha256 (default)
  • sha384
  • sha512
[root@master ~]# salt '*' cmd.run 'sha256sum /root/a'
minion:
    e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855  /root/a
[root@master ~]# salt '*' file.get_sum /root/a
minion:
    e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
[root@master ~]# salt '*' cmd.run 'md5sum /root/a'
minion:
    d41d8cd98f00b204e9800998ecf8427e  /root/a
[root@master ~]# salt '*' file.get_sum /root/a md5
minion:
    d41d8cd98f00b204e9800998ecf8427e
[root@master ~]# salt '*' file.get_sum /root/a sha1
minion:
    da39a3ee5e6b4b0d3255bfef95601890afd80709
[root@master ~]# salt '*' file.get_sum /root/a sha512
minion:
    cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e

file.get_uid与file.get_user

#获取指定文件的 uid 或 用户名
[root@master ~]# salt '*' cmd.run 'ls -l /root/a'
minion:
    -rw-r--r--. 1 root root 0 Jul  7 11:51 /root/a
[root@master ~]#  salt '*' file.get_uid /root/a
minion:
    0
[root@master ~]# salt '*' file.get_user /root/a
minion:
    root

file.gid_to_group

#将指定的 gid 转换为组名并显示
[root@master ~]#  salt '*' file.gid_to_group 1000
minion:
    1000
[root@master ~]# salt '*' file.gid_to_group 0
minion:
    root

file.group_to_gid

#将指定的组名转换为 gid 并显示
[root@master ~]# salt '*' file.group_to_gid root
minion:
    0
[root@master ~]# salt '*' file.group_to_gid test
minion:
    1001

file.grep

#在指定文件中检索指定内容
#该函数支持通配符,若在指定的路径中用通配符则必须用双引号引起来
[root@master ~]# salt '*' file.grep /etc/passwd nobody
minion:
    ----------
    pid:
        841302
    retcode:
        0
    stderr:
    stdout:
        nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin

[root@master ~]# salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr -- -i
minion:
    ----------
    pid:
        841623
    retcode:
        2
    stderr:
        grep: /etc/sysconfig/network-scripts/ifcfg-eth0: No such file or directory
    stdout:
ERROR: Minions returned with non-zero exit code

[root@master ~]# salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr -- -i -B2
minion:
    ----------
    pid:
        842112
    retcode:
        2
    stderr:
        grep: /etc/sysconfig/network-scripts/ifcfg-eth0: No such file or directory
    stdout:
ERROR: Minions returned with non-zero exit code

[root@master ~]# salt '*' file.grep "/etc/sysconfig/network-scripts/*" ipaddr -- -i -l
minion:
    ----------
    pid:
        842453
    retcode:
        1
    stderr:
    stdout:
ERROR: Minions returned with non-zero exit code

file.is_blkdev

#判断指定的文件是否是块设备文件
[root@master ~]# salt '*' cmd.run 'ls -l /dev/sr0'
minion:
    brw-rw----. 1 root cdrom 11, 0 Jul  6 04:37 /dev/sr0
[root@master ~]# salt '*' file.is_blkdev /dev/sr0
minion:
    True

file.lsattr

检查并显示出指定文件的属性信息
[root@master ~]# salt '*' cmd.run 'lsattr /root/a'
minion:
    -------------------- /root/a
[root@master ~]# salt '*' cmd.run 'chattr +i /root/a'
minion:
[root@master ~]# salt '*' cmd.run 'lsattr /root/a'
minion:
    ----i--------------- /root/a
[root@master ~]# salt '*' file.lsattr /root/a
minion:
    ----------
    /root/a:
        - i

file.mkdir

#创建目录并设置属主、属组及权限
[root@master ~]# salt '*' file.mkdir /root/qwe
minion:
    True
[root@master ~]#  salt '*' cmd.run 'ls -l /root/qwe'
minion:
    total 0
[root@master ~]#  salt '*' cmd.run 'ls -l /root/'
minion:
    total 4
    -rw-r--r--. 1 root root    0 Jul  7 11:51 a
    drwxr-xr-x. 7 root root   52 Jul  7 11:58 aaa
    -rw-r--r--. 1 root root    0 Jul  7 06:38 abc
    -rw-------. 1 root root 1069 May 11 04:14 anaconda-ks.cfg
    drwxr-xr-x. 2 root root    6 Jul  7 12:14 qwe
[root@master ~]# salt '*' file.mkdir /root/haha test test 777
minion:
    True
[root@master ~]#  salt '*' cmd.run 'ls -l /root/'
minion:
    total 4
    -rw-r--r--. 1 root root    0 Jul  7 11:51 a
    drwxr-xr-x. 7 root root   52 Jul  7 11:58 aaa
    -rw-r--r--. 1 root root    0 Jul  7 06:38 abc
    -rw-------. 1 root root 1069 May 11 04:14 anaconda-ks.cfg
    drwxrwxrwx. 2 test test    6 Jul  7 12:15 haha
    drwxr-xr-x. 2 root root    6 Jul  7 12:14 qwe

file.move

#移动或重命名
#重命名
[root@master ~]# salt '*' file.move /root/a /root/b
minion:
    ----------
    comment:
        '/root/a' moved to '/root/b'
    result:
        True
#移动
[root@master ~]# salt '*' file.move /root/cc /opt/
salt '*' file.move /root/cc /opt/
minion:
    ----------
    comment:
        '/root/cc' moved to '/opt/'
    result:
        True

file.prepend

#把文本插入指定文件的开头
[root@master ~]# salt '*' cmd.run 'cat /root/b'
minion:
[root@master ~]# salt '*' file.prepend /root/b "hehe" "xixi" "haha"
minion:
    Prepended 3 lines to "/root/b"
[root@master ~]# salt '*' cmd.run 'cat /root/b'
minion:
    hehe
    xixi
    haha
[root@master ~]# salt '*' file.prepend /root/b hehe \
> xixi
minion:
    Prepended 2 lines to "/root/b"
[root@master ~]# salt '*' cmd.run 'cat /root/b'
minion:
    hehe
    xixi
    hehe
    xixi
    haha

file.sed

#修改文本文件的内容
[root@master ~]# salt '*' cmd.run 'cat /root/b'
minion:
    hehe
    xixi
    hehe
    xixi
    haha
[root@master ~]# salt '*' file.sed /root/b 'hehe' 'runtime'
minion:
    ----------
    pid:
        855515
    retcode:
        0
    stderr:
    stdout:
[root@master ~]# salt '*' cmd.run 'cat /root/b'
minion:
    runtime
    xixi
    runtime
    xixi
    haha
    [root@master ~]# salt '*' cmd.run 'cat /root/b'
minion:
    runtime
    xixi
    runtime
    xixi
    haha
[root@master ~]# salt '*' file.sed /root/b 'runtime' 'xixi' flags=1
minion:
    ----------
    pid:
        857166
    retcode:
        0
    stderr:
    stdout:
[root@master ~]# salt '*' cmd.run 'cat /root/b'
minion:
    xixi
    xixi
    xixi
    xixi
    haha

file.read

#读取文件内容
[root@master ~]# salt '*' cmd.run 'cat /root/b'
minion:
    xixi
    xixi
    xixi
    xixi
    haha
[root@master ~]# salt '*' file.read /root/b
minion:
    xixi
    xixi
    xixi
    xixi
    haha

file.readdir

#列出指定目录下的所有文件或目录,包括隐藏文件
[root@master ~]# salt '*' file.readdir /root
minion:
    - .
    - ..
    - .bash_logout
    - .bash_profile
    - .bashrc
    - .cshrc
    - .tcshrc
    - anaconda-ks.cfg
    - .config
    - .bash_history
    - abc
    - a
    - .viminfo
    - aaa
    - qwe
    - haha
    - e
    - qwer
    - b.bak
    - b

file.remove

#删除指定的文件或目录,若给出的是目录,将递归删除
[root@master ~]# salt '*' file.remove /root/b
minion:
    True

file.rename

#重命名文件或目录
[root@master ~]#  salt '*' file.rename /root/a /root/b
minion:
    True

file.set_mode

#给指定文件设置权限
[root@master ~]# salt '*' file.set_mode /root/b 0400
minion:
    0400
[root@master ~]#  salt '*' cmd.run 'ls -l /root'
minion:
    total 8
    -rw-r--r--. 1 root root    0 Jul  7 11:51 a
    drwxr-xr-x. 7 root root   52 Jul  7 11:58 aaa
    -rw-r--r--. 1 root root    0 Jul  7 06:38 abc
    -rw-------. 1 root root 1069 May 11 04:14 anaconda-ks.cfg
    -r--------. 1 root root   31 Jul  7 12:20 b

file.symlink

#给指定的文件创建软链接
[root@master ~]# salt '*' file.symlink /root/b /opt/a
minion:
    True
[root@minion ~]# ll /opt
总用量 4
lrwxrwxrwx. 1 root root  7 7月   7 12:24 a -> /root/b

file.touch

#创建空文件或更新时间戳
[root@master ~]# salt '*' file.touch /opt/cc
minion:
    True
[root@master ~]# salt '*' file.touch /opt/aa
minion:
    True
[root@minion ~]# ll /opt
总用量 4
lrwxrwxrwx. 1 root root  7 7月   7 12:24 a -> /root/b
-rw-r--r--. 1 root root 22 7月   7 12:25 aa
drwxr-xr-x. 7 root root 52 7月   7 11:58 aaaa
-rw-r--r--. 1 root root  0 7月   7 11:54 abc
drwxr-xr-x. 2 root root  6 7月   7 06:17 bb
-rw-r--r--. 1 root root  0 7月   7 12:25 cc

file.uid_to_user

#将指定的 uid 转换成用户名显示出来
[root@master ~]# salt '*' file.uid_to_user 0
minion:
    root
[root@master ~]# salt '*' file.uid_to_user 1000
minion:
    1000
[root@master ~]# salt '*' file.uid_to_user 1001
minion:
    test

file.user_to_uid

#将指定的用户转换成 uid 并显示出来
[root@master ~]# salt '*' file.user_to_uid test
minion:
    1001
[root@master ~]# salt '*' file.user_to_uid root
minion:
    0

file.write

#往一个指定的文件里覆盖写入指定内容
[root@master ~]# salt '*' file.write /root/b "I'm tom" "haha" "xixi"
minion:
    Wrote 3 lines to "/root/b"
[root@master ~]# salt '*' cmd.run 'cat /root/b'
minion:
    I'm tom
    haha
    xixi

Saltstack的模块之lvs

在受要用的那一端记得安装ipvsadm
lvs.add_service

#添加调度器并设置连接方式和调度算法。调度算法默认为wlc
[root@master ~]# salt minion lvs.add_service tcp 192.168.31.139:80 rr
minion:
    True
[root@minion ~]# ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  minion:http rr

lvs.add_server

#添加RS并设置调度模式和权重
[root@master ~]#  salt minion lvs.add_server tcp 192.168.31.139:80 192.168.31.150:80 nat 1
minion:
    True
[root@minion ~]# ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  minion:http rr
  -> minion:http                  Masq    1      0          0

lvs.check_server

#检查指定的RS是否在指定的调度规则中
[root@master ~]#  salt minion lvs.check_server tcp 192.168.31.139:80 192.168.31.150:80
minion:
    True
[root@master ~]#  salt minion lvs.check_server tcp 192.168.31.139:80 192.168.31.140:80
minion:
    Error: server not exists

lvs.check_service

#检查指定的调度规则是否存在
[root@master ~]# salt minion lvs.check_service tcp 192.168.31.139:80
minion:
    True
[root@master ~]# salt minion lvs.check_service tcp 192.168.31.139:8080
minion:
    Error: service not exists

lvs.clear

#清除ipvsadm规则
[root@master ~]# salt minion lvs.clear
minion:
    True
[root@minion ~]# ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

lvs.delete_server

#将指定的RS从指定调度规则中删除
[root@master ~]#  salt minion lvs.delete_server tcp 192.168.31.139:80 192.168.31.140:80
minion:

    True

lvs.delete_service

#将指定的调度规则删除
[root@master ~]#  salt minion lvs.delete_service tcp 192.168.31.139:80 
minion:
minion:
    True

lvs.edit_server

#修改映射端口、调度模式和权重
[root@master ~]# salt minion lvs.edit_server tcp 192.168.31.139:80 192.168.31.140:80 nat 2
minion:
    True

lvs.edit_service

#修改调度算法
[root@master ~]# salt minion lvs.edit_service tcp 192.168.31.139:80 wlc
minion:
    True

lvs.get_rules

#获取ipvsadm规则
[root@master ~]# salt minion lvs.get_rules
minion:
    -A -t 192.168.31.139:80 -s rr
    -A -t 192.168.31.140:80 -s r

lvs.list

#获取ipvsadm完整信息
[root@master ~]# salt minion lvs.list
minion:
    IP Virtual Server version 1.2.1 (size=4096)
    Prot LocalAddress:Port Scheduler Flags
      -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
    TCP  192.168.31.139:80 rr
    TCP  192.168.31.140:80 rr

lvs.zero

#将所有服务或者指定服务的数据包、字节和速率归零
[root@master ~]# salt minion lvs.zero tcp 192.168.31.139:80
minion:
    True
[root@master ~]# salt minion lvs.list
minion:
    IP Virtual Server version 1.2.1 (size=4096)
    Prot LocalAddress:Port Scheduler Flags
      -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
    TCP  192.168.31.139:80 rr    
    TCP  192.168.31.140:80 rr

Saltstack常用模块之mysql

minion端配置

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

安装不了mysqlclient结束。。。。。。。