SaltStack常用模块

文章目录

1.SaltStack模块介绍

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

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


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


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


//查看指定module的用法

[root@master ~]# salt 'node1' sys.doc cmd
cmd.exec_code:

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

    All parameters from :mod:`cmd.run_all <salt.modules.cmdmod.run_all>` except python_shell can be used.

    CLI Example:

        salt '*' cmd.exec_code ruby 'puts "cheese"'
        salt '*' cmd.exec_code ruby 'puts "cheese"' args='["arg1", "arg2"]' env='{"FOO": "bar"}'
.....//此处省略N行


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


2. SaltStack常用模块

2.1 SaltStack常用模块之network

2.1.1network.active_tcp

//返回所有活动的tcp连接
[root@master ~]# salt '*' network.active_tcp
node1:
    ----------
    0:
        ----------
        local_addr:
            192.168.100.20
        local_port:
            22
        remote_addr:
            192.168.100.1
        remote_port:
            55168
    1:
        ----------
        local_addr:
            192.168.100.20
        local_port:
            22
        remote_addr:
            192.168.100.1
        remote_port:
            53557
    2:
        ----------
        local_addr:
            192.168.100.20
        local_port:
            22
        remote_addr:
            192.168.100.1
        remote_port:
            55159
    3:
        ----------
        local_addr:
            192.168.100.20
        local_port:
            56310
        remote_addr:
            192.168.100.10
        remote_port:
            4505
    4:
        ----------
        local_addr:
            192.168.100.20
        local_port:
            22
        remote_addr:
            192.168.100.1
        remote_port:
            64263
    5:
        ----------
        local_addr:
            192.168.100.20
        local_port:
            22
        remote_addr:
            192.168.100.1
        remote_port:
            55160

2.1.2network.calc_net

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

[root@master ~]# salt '*' network.calc_net 192.168.100.10 255.255.255.0
master:
    192.168.100.0/24
node1:
    192.168.100.0/24

[root@master ~]# salt '*' network.calc_net 192.168.100.10 255.255.255.240
node1:
    192.168.100.0/28
master:
    192.168.100.0/28

2.1.3network.connect

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

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

2.1.4network.default_route

查看默认路由

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

2.1.5 network.get_fqdn

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

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

2.1.6 network.get_hostname

获取主机名

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

2.1.7 network.get_route

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

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

2.1.8 network.hw_addr

返回指定网卡的MAC地址

[root@master ~]# salt '*' network.hw_addr ens33
node1:
    00:0c:29:c8:f3:ae
master:
    00:0c:29:b5:79:e9

2.1.9 network.ifacestartswith

从特定CIDR检索接口名称

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

2.1.10 network.in_subnet

判断指定网卡的信息

[root@master ~]# salt '*' network.ifacestartswith 192.168
master:
    - ens33
node1:
    - ens33
[root@master ~]# salt '*' network.in_subnet 192.168.100.0/24
node1:
    True
master:
    True

2.1.11 network.interface

返回指定网卡的信息

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

2.1.12 network.interface_ip

返回指定网卡的ip地址

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

2.1.13 network.interfaces

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

[root@master ~]# salt '*' network.interfaces
master:
    ----------
    ens33:
        ----------
        hwaddr:
            00:0c:29:b5:79:e9
        inet:
            |_
              ----------
              address:
                  192.168.100.10
              broadcast:
                  192.168.100.255
              label:
                  ens33
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
              address:
                  fe80::ac54:1165:75e1:eb6f
              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
    virbr0:
        ----------
        hwaddr:
            52:54:00:65:3f:2f
        up:
            True
    virbr0-nic:
        ----------
        hwaddr:
            52:54:00:65:3f:2f
        up:
            False
node1:
    ----------
    ens33:
        ----------
        hwaddr:
            00:0c:29:c8:f3:ae
        inet:
            |_
              ----------
              address:
                  192.168.100.20
              broadcast:
                  192.168.100.255
              label:
                  ens33
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
              address:
                  fe80::baf8:3da3:ce41:8484
              prefixlen:
                  64
              scope:
                  link
        up:
            True
    lo:
        ----------
        hwaddr:
            00:00:00:00:00:00
        inet:
            |_
              ----------
              address:
                  127.0.0.1
              broadcast:
                  None
              label:
                  lo
              netmask:
                  255.0.0.0
        inet6:
            |_
              ----------
              address:
                  ::1
              prefixlen:
                  128
              scope:
                  host
        up:
            True

2.1.14 network.ip_addrs

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

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

2.1.15 network.netstat

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

[root@master ~]# salt '*' network.netstat
node1:
    |_
      ----------
      inode:
          28894
      local-address:
          0.0.0.0:22
      program:
          1029/sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          0.0.0.0:*
      send-q:
          0
      state:
          LISTEN
      user:
          0

2.1.16 network.ping

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

[root@master ~]# salt '*' network.ping www.baidu.com
node1:
    PING www.a.shifen.com (112.80.248.76) 56(84) bytes of data.
    64 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=1 ttl=128 time=21.10 ms
    64 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=2 ttl=128 time=29.5 ms
    64 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=3 ttl=128 time=22.1 ms
    64 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=4 ttl=128 time=22.3 ms
    
    --- www.a.shifen.com ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3005ms
    rtt min/avg/max/mdev = 21.950/23.976/29.531/3.211 ms

2.1.17 network.reverse_ip

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

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

2.2 SaltStack常用模块之service

2.2.1 service.available

判断指定的服务是否可用

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

2.2.2 service.get_all

获取所有正在运行的服务

[root@master ~]# salt '*' service.get_all
node1:
    - NetworkManager
    - NetworkManager-dispatcher
    - NetworkManager-wait-online
    - arp-ethers
    - atd
    - auditd
    - autovt@
    - basic.target
    - blk-availability
    - bluetooth.target
    - bolt
    - boot-complete.target
    - chrony-dnssrv@
    - chrony-dnssrv@.timer
    - chrony-wait
    - chronyd
    - cockpit
    - cockpit-motd
    - cockpit-wsinstance-http
    - cockpit-wsinstance-http-redirect
    - cockpit-wsinstance-http-redirect.socket
    - cockpit-wsinstance-http.socket
    - cockpit-wsinstance-https-factory.socket
    - cockpit-wsinstance-https-factory@
    - cockpit-wsinstance-https@
    - cockpit-wsinstance-https@.socket
    - cockpit.socket
    - console-getty
    - container-getty@
    - cpupower
    - crond
    - cryptsetup-pre.target
    - cryptsetup.target
    - ctrl-alt-del.target
    - dbus
    - dbus-org.fedoraproject.FirewallD1
    - dbus-org.freedesktop.hostname1
    - dbus-org.freedesktop.locale1
    - dbus-org.freedesktop.login1
    - dbus-org.freedesktop.nm-dispatcher
    - dbus-org.freedesktop.portable1
    - dbus-org.freedesktop.timedate1
    - dbus.socket
    - debug-shell
    - default.target
    - dev-hugepages.mount
    - dev-mqueue.mount
    - dm-event
    - dm-event.socket
    - dnf-makecache
    - dnf-makecache.timer
    - dracut-cmdline
    - dracut-initqueue
    - dracut-mount
    - dracut-pre-mount
    - dracut-pre-pivot
    - dracut-pre-trigger
    - dracut-pre-udev
    - dracut-shutdown
    - ebtables
    - emergency
    - emergency.target
    - exit.target
    - final.target
    - firewalld
    - fprintd
    - fstrim
    - fstrim.timer
    - getty-pre.target
.....此处省略N行

2.2.3 service.disabled

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

[root@master ~]# salt '*' service.disabled httpd
master:
    True
node1:
    True
//这里的Trun的意思是开机不自启

2.2.4 service.enabled

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

[root@master ~]# salt '*' service.enabled httpd
master:
    False
node1:
    False
//这里的False的意思是开机不自启

2.2.5 service.disable

设置指定服务开机不自启

//minion上的htppd的状态,显示的是开机不自启
[root@node1 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset:>
   Active: active (running) since Mon 2021-07-05 06:44:03 EDT; 1 day 2h ago
     Docs: man:httpd.service(8)
 Main PID: 428952 (httpd)
   Status: "Total requests: 2; Idle/Busy workers 100/0;Requests/sec: 2.09e-05; Byt>
    Tasks: 213 (limit: 11200)
   Memory: 29.3M
   CGroup: /system.slice/httpd.service
           ├─428952 /usr/sbin/httpd -DFOREGROUND
           ├─429599 /usr/sbin/httpd -DFOREGROUND
           ├─429600 /usr/sbin/httpd -DFOREGROUND
           ├─429601 /usr/sbin/httpd -DFOREGROUND
           └─429602 /usr/sbin/httpd -DFOREGROUND

7月 05 06:43:53 node1 systemd[1]: Starting The Apache HTTP Server...


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


2.2.6 service.enable

设置指定服务开机自启

//这里是minion的httpd状态,开机不自启

[root@node1 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset:>
   Active: active (running) since Mon 2021-07-05 06:44:03 EDT; 1 day 2h ago
     Docs: man:httpd.service(8)
 Main PID: 428952 (httpd)
   Status: "Total requests: 2; Idle/Busy workers 100/0;Requests/sec: 2.09e-05; Byt>
    Tasks: 213 (limit: 11200)
   Memory: 29.3M
   CGroup: /system.slice/httpd.service
           ├─428952 /usr/sbin/httpd -DFOREGROUND
           ├─429599 /usr/sbin/httpd -DFOREGROUND
           ├─429600 /usr/sbin/httpd -DFOREGROUND
           ├─429601 /usr/sbin/httpd -DFOREGROUND
           └─429602 /usr/sbin/httpd -DFOREGROUND


[root@master ~]# salt '*' service.enable httpd
master:
    ERROR: Failed to enable unit: Unit file httpd.service does not exist.
node1:
    True
ERROR: Minions returned with non-zero exit code



[root@node1 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: >
   Active: active (running) since Mon 2021-07-05 06:44:03 EDT; 1 day 2h ago
     Docs: man:httpd.service(8)
 Main PID: 428952 (httpd)
   Status: "Total requests: 2; Idle/Busy workers 100/0;Requests/sec: 2.09e-05; Byt>
    Tasks: 213 (limit: 11200)


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

2.2.7 service.reload

重新装载指定服务

[root@master ~]# salt '*' service.reload httpd
master:
    ERROR: Failed to reload httpd.service: Unit httpd.service not found.  //这里的意思是我没有在master这台主机上装httpd服务
node1:
    True
ERROR: Minions returned with non-zero exit code

2.2.8 service.stop

停止指定服务

[root@master ~]# salt '*' service.stop httpd
master:
    False
node1:
    True
ERROR: Minions returned with non-zero exit code

2.2.9 service.start

启动指定服务

[root@master ~]# salt '*' service.start httpd
master:
    ERROR: Failed to start httpd.service: Unit httpd.service not found.
node1:
    True
ERROR: Minions returned with non-zero exit code

2.2.10 service.restart

重启指定服务

[root@master ~]# salt '*' service.restart httpd
master:
    ERROR: Failed to restart httpd.service: Unit httpd.service not found.
node1:
    True
ERROR: Minions returned with non-zero exit code

2.2.11 service.status

查看指定服务的状态

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

2.3 SaltStack常用的模块之pkg

2.3.1 pkg.download

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

[root@master ~]# salt '*' pkg.download vim
node1:
    ----------
    vim:
        /var/cache/yum/packages/vim-enhanced-8.0.1763-15.el8.x86_64.rpm
//下面的路径是下载好的包的存放路径

2.3.2 pkg.file_list

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

[root@master ~]# salt '*' pkg.file_list httpd
node1:
    ----------
    errors:
    files:
        - /etc/httpd/conf
        - /etc/httpd/conf.d/autoindex.conf
        - /etc/httpd/conf.d/userdir.conf
        - /etc/httpd/conf.d/welcome.conf
        - /etc/httpd/conf.modules.d
        - /etc/httpd/conf.modules.d/00-base.conf
        - /etc/httpd/conf.modules.d/00-dav.conf
        - /etc/httpd/conf.modules.d/00-lua.conf
        - /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
        - /etc/httpd/conf.modules.d/README
        - /etc/httpd/conf/httpd.conf
        - /etc/httpd/conf/magic



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


2.3.3 pkg.group_info

查看包组的信息

[root@master ~]# salt '*' pkg.group_info 'Development Tools'
node1:
    ----------
    conditional:
    default:
        - asciidoc
        - byacc
        - ctags
        - diffstat
        - elfutils-libelf-devel
        - git
        - intltool
        - jna
        - ltrace
        - patchutils
        - 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

2.3.4 pkg.group_list

列出系统中所有的包组

[root@master ~]# salt '*' pkg.group_list
node1:
    ----------
    available:
        - Backup Client
        - base-x
        - Conflicts AppStream
        - Debugging Tools
        - Desktop Debugging and Performance Tools
        - .NET Core Development
        - FTP Server
        - GNOME Applications
        - Graphics Creation Tools
        - Guest Agents
        - Guest Desktop Agents
        - Input Methods
        - Internet Applications
        - Internet Browser
        - Java Platform
        - Legacy X Window System Compatibility
        - Multimedia
        - Office Suite and Productivity
        - Atomic Host ostree support
        - KVM platform specific packages
        - Hyper-v platform specific packages
        - Printing Client
        - Remote Desktop Clients
        - RPM Development Tools
        - TeX formatting system
        - Virtualization Client
        - Virtualization Hypervisor
        - Virtualization Platform
        - Virtualization Tools
        - Basic Web Server
        - Additional Development
        - Anaconda tools
        - Base
        - Conflicts BaseOS
        - Development Tools
        - Dial-up Networking Support
        - File and Storage Server
        - Fonts
        - GNOME
        - Graphical Administration Tools
        - Hardware Monitoring Utilities
        - Infiniband Support
        - Large Systems Performance
        - Legacy UNIX Compatibility
        - Mail Server
        - Mainframe Access
        - Network File System Client
        - Network Servers
        - Networking Tools
        - Common NetworkManager submodules
        - Performance Tools
        - Platform Development
        - Python Web
        - Remote Management for Linux
        - Scientific Support
        - Security Tools
        - Smart Card Support
        - Windows File Server
        - System Tools
        - Workstation product core
    available environments:
        - Server with GUI
        - Minimal Install
        - Workstation
        - Virtualization Host
        - Custom Operating System
    available languages:
        ----------
    installed:
        - Container Management
        - VMware platform specific packages
        - Core
        - Hardware Support
        - Headless Management
        - Server product core
        - Standard
    installed environments:
        - Server

2.3.5 pkg.install

安装软件

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

2.3.6 pkg.list_downloaded

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

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

2.3.7 pkg.list_pkgs

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

[root@master ~]# salt '*' pkg.list_pkgs
node1:
    ----------
    NetworkManager:
        1:1.30.0-0.3.el8
    NetworkManager-config-server:
        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
    PackageKit:
        1.1.12-6.el8
    PackageKit-glib:
        1.1.12-6.el8
    abattis-cantarell-fonts:
        0.0.25-4.el8
    acl:
        2.2.53-1.el8
    adcli:
        0.8.2-8.el8
    adwaita-cursor-theme:
        3.28.0-2.el8
    adwaita-icon-theme:
        3.28.0-2.el8
    alsa-sof-firmware:
        1.6-1.el8
    apr:
        1.6.3-11.el8
    apr-util:
        1.6.1-6.el8
    apr-util-bdb:
        1.6.1-6.el8
    apr-util-openssl:
        1.6.1-6.el8
    at:
        3.1.20-11.el8

......//此处省略N行

2.3.8 pkg.owner

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

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

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

2.3.8 pkg.remove

卸载指定软件

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

//如果需要卸载多个文件,中间用逗号隔开就可以

2.3.10 pkg.upgrade

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

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

//如果想要升级所有的软件包,把name的参数去点就可以了

2.4 SaltStack常用模块之state

2.4.1 state.show_highstate

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

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

2.4.2 state.highstate

执行高级状态

[root@master ~]# salt '*' state.highstate web.apache.install
node1:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 09:54:47.972490
    Duration: 899.218 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 09:54:48.874544
    Duration: 53.077 ms
     Changes:   

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

2.4.3 state.show_state_usage

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

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

2.4.4 state.show_top

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

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

2.4.5 state.top

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

[root@master ~]# salt 'node1' state.top install.sls
node1:
----------
          ID: install.sls
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 22:06:30.122684  
    Duration: 968.659 ms
     Changes:

2.4.6 state.show_sls

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

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

2.5 SaltStack常用模块之salt-cp

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

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

[root@master ~]# salt-cp 'node1' /etc/passwd /opt/
node1:
    ----------
    /opt/passwd:
        True

[root@master ~]# salt '*' cmd.run 'ls /opt/'
node1:
    passwd
master:


//拷贝多个文件到目标主机的/opt/目录下
[root@master ~]# salt-cp '*' /etc/shadow /etc/group /opt/
master:
    ----------
    /opt/group:
        True
    /opt/shadow:
        True
node1:
    ----------
    /opt/group:
        True
    /opt/shadow:
        True
[root@master ~]# salt '*' cmd.run 'ls /opt/'
node1:
    group
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值