Excute Commands with SaltStack

2021-08-11 17:05:50 浏览数 (2)

前言

SaltStack 是一款高性能的自动化运维工具

类似的工具还有 Puppet、Chef、Ansible,他们之间可以相互替代,但是哪一个更好,我就不在此引发圣战了

这里分享一下 SaltStack 执行命令的方法

参考 EXECUTE COMMANDS

Tip: 当前的版本为 Latest release: 2017.7.3 (February 5, 2018)


操作

环境

代码语言:javascript复制
[root@h209 ~]# hostnamectl
   Static hostname: h209
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 33dc28f7e76c4903ad9b603b77e29a7c
           Boot ID: 6ce363851e6d4a519c97c067a58296ae
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-514.21.1.el7.x86_64
      Architecture: x86-64
[root@h209 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:0b:e9:0b brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3
       valid_lft 85529sec preferred_lft 85529sec
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:36:8b:0c brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.209/24 brd 192.168.56.255 scope global enp0s8
       valid_lft forever preferred_lft forever
4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether 52:54:00:16:5e:11 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
5: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
    link/ether 52:54:00:16:5e:11 brd ff:ff:ff:ff:ff:ff
[root@h209 ~]# salt -V
Salt Version:
           Salt: 2017.7.3

Dependency Versions:
           cffi: 1.6.0
       cherrypy: Not Installed
       dateutil: 1.5
      docker-py: Not Installed
          gitdb: Not Installed
      gitpython: Not Installed
          ioflo: Not Installed
         Jinja2: 2.7.2
        libgit2: Not Installed
        libnacl: Not Installed
       M2Crypto: Not Installed
           Mako: 0.8.1
   msgpack-pure: Not Installed
 msgpack-python: 0.5.1
   mysql-python: Not Installed
      pycparser: 2.14
       pycrypto: 2.6.1
   pycryptodome: Not Installed
         pygit2: Not Installed
         Python: 2.7.5 (default, Nov  6 2016, 00:28:07)
   python-gnupg: Not Installed
         PyYAML: 3.11
          PyZMQ: 15.3.0
           RAET: Not Installed
          smmap: Not Installed
        timelib: Not Installed
        Tornado: 4.2.1
            ZMQ: 4.1.4

System Versions:
           dist: centos 7.3.1611 Core
         locale: UTF-8
        machine: x86_64
        release: 3.10.0-514.21.1.el7.x86_64
         system: Linux
        version: CentOS Linux 7.3.1611 Core

[root@h209 ~]#

命令语法

代码语言:javascript复制
[root@h209 ~]# salt-key -L
Accepted Keys:
much
test-master
test-minion
Denied Keys:
Unaccepted Keys:
Rejected Keys:
[root@h209 ~]# salt "*" cmd.run "echo test"
much:
    test
test-master:
    test
test-minion:
    test
[root@h209 ~]#

一般而言,语法如下

代码语言:javascript复制
salt <target> <module.function> [argument]
  • target: 指定哪些系统来执行以下命令
  • command (module.function): 命令由模块和方法构成,这是 salt 的处理逻辑
  • arguments: 处理逻辑需要的参数,有些没有参数,就不必添加,有些参数具备默认值,也可以不必加,但是可以通过改变参数值来调整处理逻辑

salt执行对象模块.方法参数

查看磁盘用量

代码语言:javascript复制
[root@h209 ~]# salt "*" disk.usage
test-minion:
    ----------
    /:
        ----------
        1K-blocks:
            40137576
        available:
            33086520
        capacity:
            18%
        filesystem:
            /dev/mapper/cl-root
        used:
            7051056
    /boot:
        ----------
        1K-blocks:
            1038336
        available:
            841580
        capacity:
            19%
        filesystem:
            /dev/sda1
        used:
            196756
    /dev:
        ----------
        1K-blocks:
            2008008
        available:
            2008008
        capacity:
            0%
        filesystem:
            devtmpfs
        used:
            0
    /dev/shm:
        ----------
        1K-blocks:
            2023420
        available:
            2023408
        capacity:
            1%
        filesystem:
            tmpfs
        used:
            12
    /home:
        ----------
        1K-blocks:
            19593216
        available:
            16607368
        capacity:
            16%
        filesystem:
            /dev/mapper/cl-home
        used:
            2985848
    /media/cdrom:
        ----------
        1K-blocks:
            8086368
        available:
            0
        capacity:
            100%
        filesystem:
            /dev/sr0
        used:
            8086368
    /media/sf_script:
        ----------
        1K-blocks:
            269029008
        available:
            39847740
        capacity:
            86%
        filesystem:
            script
        used:
            229181268
    /run:
        ----------
        1K-blocks:
            2023420
        available:
            2014552
        capacity:
            1%
        filesystem:
            tmpfs
        used:
            8868
    /sys/fs/cgroup:
        ----------
        1K-blocks:
            2023420
        available:
            2023420
        capacity:
            0%
        filesystem:
            tmpfs
        used:
            0
test-master:
    ----------
    /:
        ----------
        1K-blocks:
            40137576
        available:
            31538344
        capacity:
            22%
        filesystem:
            /dev/mapper/cl-root
        used:
            8599232
    /boot:
        ----------
        1K-blocks:
            1038336
        available:
            841580
        capacity:
            19%
        filesystem:
            /dev/sda1
        used:
            196756
    /dev:
        ----------
        1K-blocks:
            2008008
        available:
            2008008
        capacity:
            0%
        filesystem:
            devtmpfs
        used:
            0
    /dev/shm:
        ----------
        1K-blocks:
            2023420
        available:
            2023392
        capacity:
            1%
        filesystem:
            tmpfs
        used:
            28
    /home:
        ----------
        1K-blocks:
            19593216
        available:
            16607320
        capacity:
            16%
        filesystem:
            /dev/mapper/cl-home
        used:
            2985896
    /media/cdrom:
        ----------
        1K-blocks:
            8086368
        available:
            0
        capacity:
            100%
        filesystem:
            /dev/sr0
        used:
            8086368
    /media/sf_usb:
        ----------
        1K-blocks:
            269029008
        available:
            39847740
        capacity:
            86%
        filesystem:
            usb
        used:
            229181268
    /run:
        ----------
        1K-blocks:
            2023420
        available:
            2014500
        capacity:
            1%
        filesystem:
            tmpfs
        used:
            8920
    /run/user/0:
        ----------
        1K-blocks:
            404684
        available:
            404684
        capacity:
            0%
        filesystem:
            tmpfs
        used:
            0
    /run/user/1004:
        ----------
        1K-blocks:
            404684
        available:
            404684
        capacity:
            0%
        filesystem:
            tmpfs
        used:
            0
    /sys/fs/cgroup:
        ----------
        1K-blocks:
            2023420
        available:
            2023420
        capacity:
            0%
        filesystem:
            tmpfs
        used:
            0
much:
    ----------
    /:
        ----------
        1K-blocks:
            40137576
        available:
            34442576
        capacity:
            15%
        filesystem:
            /dev/mapper/cl-root
        used:
            5695000
    /boot:
        ----------
        1K-blocks:
            1038336
        available:
            841580
        capacity:
            19%
        filesystem:
            /dev/sda1
        used:
            196756
    /dev:
        ----------
        1K-blocks:
            2008008
        available:
            2008008
        capacity:
            0%
        filesystem:
            devtmpfs
        used:
            0
    /dev/shm:
        ----------
        1K-blocks:
            2023420
        available:
            2023408
        capacity:
            1%
        filesystem:
            tmpfs
        used:
            12
    /home:
        ----------
        1K-blocks:
            19593216
        available:
            16607368
        capacity:
            16%
        filesystem:
            /dev/mapper/cl-home
        used:
            2985848
    /media/cdrom:
        ----------
        1K-blocks:
            8086368
        available:
            0
        capacity:
            100%
        filesystem:
            /dev/sr0
        used:
            8086368
    /run:
        ----------
        1K-blocks:
            2023420
        available:
            2014588
        capacity:
            1%
        filesystem:
            tmpfs
        used:
            8832
    /sys/fs/cgroup:
        ----------
        1K-blocks:
            2023420
        available:
            2023420
        capacity:
            0%
        filesystem:
            tmpfs
        used:
            0
[root@h209 ~]#

disk.usage 就没有参数

查看网络配置

代码语言:javascript复制
[root@h209 ~]# salt '*' network.interfaces
much:
    ----------
    enp0s3:
        ----------
        hwaddr:
            08:00:27:e3:df:87
        inet:
            |_
              ----------
              address:
                  10.0.2.15
              broadcast:
                  10.0.2.255
              label:
                  enp0s3
              netmask:
                  255.255.255.0
        up:
            True
    enp0s8:
        ----------
        hwaddr:
            08:00:27:d3:ec:e7
        inet:
            |_
              ----------
              address:
                  192.168.56.207
              broadcast:
                  192.168.56.255
              label:
                  enp0s8
              netmask:
                  255.255.255.0
        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
        up:
            True
    virbr0:
        ----------
        hwaddr:
            52:54:00:16:5e:11
        inet:
            |_
              ----------
              address:
                  192.168.122.1
              broadcast:
                  192.168.122.255
              label:
                  virbr0
              netmask:
                  255.255.255.0
        up:
            True
    virbr0-nic:
        ----------
        hwaddr:
            52:54:00:16:5e:11
        up:
            False
test-master:
    ----------
    enp0s3:
        ----------
        hwaddr:
            08:00:27:0b:e9:0b
        inet:
            |_
              ----------
              address:
                  10.0.2.15
              broadcast:
                  10.0.2.255
              label:
                  enp0s3
              netmask:
                  255.255.255.0
        up:
            True
    enp0s8:
        ----------
        hwaddr:
            08:00:27:36:8b:0c
        inet:
            |_
              ----------
              address:
                  192.168.56.209
              broadcast:
                  192.168.56.255
              label:
                  enp0s8
              netmask:
                  255.255.255.0
        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
        up:
            True
    virbr0:
        ----------
        hwaddr:
            52:54:00:16:5e:11
        inet:
            |_
              ----------
              address:
                  192.168.122.1
              broadcast:
                  192.168.122.255
              label:
                  virbr0
              netmask:
                  255.255.255.0
        up:
            True
    virbr0-nic:
        ----------
        hwaddr:
            52:54:00:16:5e:11
        up:
            False
test-minion:
    ----------
    enp0s3:
        ----------
        hwaddr:
            08:00:27:e3:df:87
        inet:
            |_
              ----------
              address:
                  10.0.2.15
              broadcast:
                  10.0.2.255
              label:
                  enp0s3
              netmask:
                  255.255.255.0
        up:
            True
    enp0s8:
        ----------
        hwaddr:
            08:00:27:d3:ec:e7
        inet:
            |_
              ----------
              address:
                  192.168.56.208
              broadcast:
                  192.168.56.255
              label:
                  enp0s8
              netmask:
                  255.255.255.0
        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
        up:
            True
    virbr0:
        ----------
        hwaddr:
            52:54:00:16:5e:11
        inet:
            |_
              ----------
              address:
                  192.168.122.1
              broadcast:
                  192.168.122.255
              label:
                  virbr0
              netmask:
                  255.255.255.0
        up:
            True
    virbr0-nic:
        ----------
        hwaddr:
            52:54:00:16:5e:11
        up:
            False
[root@h209 ~]#

安装软件

代码语言:javascript复制
[root@h209 ~]# salt "*" cmd.run "rpm -qa | grep cowsay"
test-minion:
much:
test-master:
    cowsay-3.04-4.el7.noarch
ERROR: Minions returned with non-zero exit code
[root@h209 ~]# salt "*" pkg.install cowsay
test-master:
    ----------
much:
    ----------
    cowsay:
        ----------
        new:
            3.04-4.el7
        old:
test-minion:
    ----------
    cowsay:
        ----------
        new:
            3.04-4.el7
        old:
[root@h209 ~]# salt "*" cmd.run "rpm -qa | grep cowsay"
much:
    cowsay-3.04-4.el7.noarch
test-minion:
    cowsay-3.04-4.el7.noarch
test-master:
    cowsay-3.04-4.el7.noarch
[root@h209 ~]#

执行新安装的命令进行测试

代码语言:javascript复制
[root@h209 ~]# salt "*" cmd.run "cowsay welcome"
much:
     _________
    < welcome >
     ---------
               ^__^
               (oo)_______
                (__)       )/
                    ||----w |
                    ||     ||
test-minion:
     _________
    < welcome >
     ---------
               ^__^
               (oo)_______
                (__)       )/
                    ||----w |
                    ||     ||
test-master:
     _________
    < welcome >
     ---------
               ^__^
               (oo)_______
                (__)       )/
                    ||----w |
                    ||     ||
[root@h209 ~]#

获取文档

可以通过 sys.doc 来获取模块方法信息

0 人点赞