4.2 SaltStack常用模块

文章目录

1. SaltStack模块介绍

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

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

1.1 查看所有module列表

[root@node01-linux ~]# salt 'node01*' sys.list_modules
node01-linux.example.com:
    - acl
    - aliases
    - alternatives
    - ansible
    - archive
    - artifactory
    - 省略N行

1.2 查看指定module的所有function

[root@node01-linux ~]# salt 'node01*' sys.list_functions cmd
node01-linux.example.com:
    - 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

1.3 查看指定module的用法

[root@node01-linux ~]# salt 'node01*' sys.doc cmd
cmd.tty:

    Echo a string to a specific tty
    CLI Example:

        salt '*' cmd.tty tty0 'This is a test'
        salt '*' cmd.tty pts3 'This is a test'

cmd.which:

    Returns the path of an executable available on the minion, None otherwise

    CLI Example:

        salt '*' cmd.which cat

cmd.which_bin:

    Returns the first command found in a list of commands

    CLI Example:

        salt '*' cmd.which_bin '[pip2, pip, pip-python]'

1.4 SaltStack执行多个Module

SaltStack默认也支持一次执行多个Module,Module之间通过逗号隔开,默认传参之间也是用逗号分隔,也支持指定传参分隔符号–args-separator=@即可

[root@node01-linux ~]# salt 'node01*' test.echo,cmd.run,service.status hello,hostname,salt-minion
node01-linux.example.com:
    ----------
    cmd.run:
        node01-linux.example.com
    service.status:
        True
    test.echo:
        hello

2.SaltStack常用模块

以下命令均只查询一台被控机node01-linux,简写为node01*

2.1 SaltStack常用模块之network

2.1.1 network.active_tcp

返回所有活动的tcp连接

[root@node01-linux ~]# salt 'node01*' network.active_tcp
node01-linux.example.com:
    ----------
    0:
        ----------
        local_addr:
            192.168.80.130
        local_port:
            4505
        remote_addr:
            192.168.80.130
        remote_port:
            51142
    1:
        ----------
        local_addr:
            192.168.80.130
        local_port:
            4505
        remote_addr:
            192.168.80.1
        remote_port:
            20746
    2:
        ----------
        local_addr:
            192.168.80.130
        local_port:
            22
        remote_addr:
            192.168.80.1
        remote_port:
            59085
    3:
        ----------
        local_addr:
            192.168.80.130
        local_port:
            51142
        remote_addr:
            192.168.80.130
        remote_port:
            4505
    4:
        ----------
        local_addr:
            192.168.80.130
        local_port:
            4506
        remote_addr:
            192.168.80.1
        remote_port:
            20739

查看端口连接状态

用法:lsof -i :[端口号]

[root@node01-linux ~]# lsof -i :22
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd     1053 root    3u  IPv4  19859      0t0  TCP *:ssh (LISTEN)
sshd     1053 root    4u  IPv6  19868      0t0  TCP *:ssh (LISTEN)
sshd    50962 root    3u  IPv4 161636      0t0  TCP node01-linux.example.com:ssh->192.168.80.1:25279 (ESTABLISHED)

2.1.2 network.calc_net

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

[root@node01-linux ~]# salt 'node01*' network.calc_net 192.168.80.130 255.255.255.0
node01-linux.example.com:
    192.168.80.0/24
[root@node01-linux ~]# salt 'node01*' network.calc_net 192.168.72.132 255.255.255.0
node01-linux.example.com:
    192.168.72.0/24

2.1.3 network.connect

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

[root@node01-linux ~]# salt 'node01*' network.connect www.baidu.com 80
node01-linux.example.com:
    ----------
    comment:
        Successfully connected to www.baidu.com (182.61.200.6) on tcp port 80
    result:
        True

2.1.4 network.default_route

查看默认路由

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

2.1.5 network.get_fqdn

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

[root@node01-linux ~]# salt 'node01*' network.get_fqdn
node01-linux.example.com:
    node01-linux.example.com

2.1.6 network.get_hostname

获取主机名

[root@node01-linux ~]# salt 'node01*' network.get_hostname
node01-linux.example.com:
    node01-linux.example.com

2.1.7 network.get_route

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

[root@node01-linux ~]# salt 'node01*' network.get_route 192.168.80.1
node01-linux.example.com:
    ----------
    destination:
        192.168.80.1
    gateway:
        None
    interface:
        ens33
    source:
        192.168.80.130

2.1.8 network.hw_addr

返回指定网卡的MAC地址

[root@node01-linux ~]# salt 'node01*' network.hw_addr ens33
node01-linux.example.com:
    00:0c:29:e8:c1:1f

2.1.9 network.ifacestartswith

从特定CIDR检索接口名称

[root@node01-linux ~]# salt 'node01*' network.ifacestartswith 192.168
node01-linux.example.com:
    - ens33
[root@node01-linux ~]# salt 'node01*' network.ifacestartswith 127.0.0.1
node01-linux.example.com:
    - lo

2.1.10 network.in_subnet

判断当前主机是否在某一个网段内

[root@node01-linux ~]# salt 'node01*' network.in_subnet 192.168.79.0/24
node01-linux.example.com:
    False
[root@node01-linux ~]# salt 'node01*' network.in_subnet 192.168.80.0/24
node01-linux.example.com:
    True

2.1.11 network.interface

返回指定网卡的信息

[root@node01-linux ~]# salt 'node01*' network.interface ens33
node01-linux.example.com:
    |_
      ----------
      address:
          192.168.80.130
      broadcast:
          192.168.80.255
      label:
          ens33
      netmask:
          255.255.255.0

2.1.12 network.interface_ip

返回指定网卡的IP地址

[root@node01-linux ~]# salt 'node01*' network.interface_ip ens33
node01-linux.example.com:
    192.168.80.130

2.1.13 network.interfaces

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

[root@node01-linux ~]# salt 'node01*' network.interfaces
node01-linux.example.com:
    ----------
    ens33:
        ----------
        hwaddr:
            00:0c:29:e8:c1:1f
        inet:
            |_
              ----------
              address:
                  192.168.80.130
              broadcast:
                  192.168.80.255
              label:
                  ens33
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
              address:
                  fe80::20c:29ff:fee8:c11f
              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@node01-linux ~]# salt 'node01*' network.ip_addrs
node01-linux.example.com:
    - 192.168.80.130

2.1.15 network.netstat

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

[root@node01-linux ~]# salt 'node01*' network.netstat
node01-linux.example.com:
    |_
      ----------
      inode:
          19859
      local-address:
          0.0.0.0:22
      program:
          1053/sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          0.0.0.0:*
      send-q:
          0
      state:
          LISTEN
      user:
          0
    |_
      ----------
      inode:
          56338
      local-address:
          0.0.0.0:4505
      program:
          23663/python
      proto:
          tcp
      recv-q:
          0
      remote-address:
          0.0.0.0:*
      send-q:
          0
      state:
          LISTEN
      user:
          0
         - 以下省略N行

2.1.16 network.ping

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

[root@node01-linux ~]# salt 'node01*' network.ping www.baidu.com
node01-linux.example.com:
    PING www.a.shifen.com (182.61.200.6) 56(84) bytes of data.
    64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=1 ttl=128 time=30.2 ms
    64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=2 ttl=128 time=29.4 ms
    64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=3 ttl=128 time=29.6 ms
    64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=4 ttl=128 time=29.5 ms
    
    --- www.a.shifen.com ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3006ms
    rtt min/avg/max/mdev = 29.463/29.713/30.213/0.344 ms

2.1.17 network.reverse_ip

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

[root@node01-linux ~]# salt 'node01*' network.reverse_ip 192.168.80.1
node01-linux.example.com:
    1.80.168.192.in-addr.arpa

2.2 SaltStack常用模块之service

2.2.1 service.available

判断指定的服务是否可用(判断是否安装该服务,安装即为True)

[root@node01-linux ~]# salt 'node01*' service.available sshd
node01-linux.example.com:
    True
[root@node01-linux ~]# salt 'node01*' service.available httpd
node01-linux.example.com:
    False

2.2.2 service.get_all

获取所有正在运行的服务

[root@node01-linux ~]# salt 'node01*' service.get_all
node01-linux.example.com:
    - NetworkManager
    - NetworkManager-dispatcher
    - NetworkManager-wait-online
    - arp-ethers
    - auditd
    - autovt@
    - basic.target
    - blk-availability
    - bluetooth.target
    - brandbot
    - 省略N行

2.2.3 service.disabled

检查指定服务是否开机不自动启动(不开机自启返回True)

[root@node01-linux ~]# salt 'node01*' service.disabled firewalld
node01-linux.example.com:
    True

2.2.4 service.enabled

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

[root@node01-linux ~]# salt 'node01*' service.enabled firewalld
node01-linux.example.com:
    False

2.2.5 service.disable

设置指定服务开机不自动启动

[root@node01-linux ~]# salt 'node01*' service.disable firewalld
node01-linux.example.com:
    True
[root@node01-linux ~]# salt 'node01*' service.enabled firewalld
node01-linux.example.com:
    False

2.2.6 service.enable

设置指定服务开机自动启动

[root@node01-linux ~]# salt 'node01*' service.enable firewalld
node01-linux.example.com:
    True
[root@node01-linux ~]# salt 'node01*' service.enabled firewalld
node01-linux.example.com:
    True

2.2.7 service.reload

重新加载指定服务(服务必须启动才可执行)

[root@node01-linux ~]# systemctl start httpd
[root@node01-linux ~]# salt 'node01*' service.reload httpd
node01-linux.example.com:
    True

2.2.8 service.stop

停止指定服务

[root@node01-linux ~]# salt 'node01*' service.stop httpd
node01-linux.example.com:
    True

2.2.9 service.start

启动指定服务

[root@node01-linux ~]# salt 'node01*' service.start httpd
node01-linux.example.com:
    True

2.2.10 service.restart

重启指定服务

[root@node01-linux ~]# salt 'node01*' service.restart httpd
node01-linux.example.com:
    True

2.2.11 service.status

查看指定服务的状态

[root@node01-linux ~]# salt 'node01*' service.status httpd
node01-linux.example.com:
    True

2.3 SaltStack常用模块之pkg

2.3.1 pkg.download

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

[root@node01-linux ~]# salt 'node01*' pkg.download wget
node01-linux.example.com:
    ----------
    wget:
        /var/cache/yum/packages/wget-1.14-18.el7_6.1.x86_64.rpm

2.3.2 pkg.file_list

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

提供参数时,列出已安装的apache软件包提供的所有文件
[root@node01-linux ~]# salt 'node01*' pkg.file_list httpd
node01-linux.example.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言是一种广泛使用的编程语言,它具有高效、灵活、可移植性强等特点,被广泛应用于操作系统、嵌入式系统、数据库、编译器等领域的开发。C语言的基本语法包括变量、数据类型、运算符、控制结构(如if语句、循环语句等)、函数、指针等。在编写C程序时,需要注意变量的声明和定义、指针的使用、内存的分配与释放等问题。C语言中常用的数据结构包括: 1. 数组:一种存储同类型数据的结构,可以进行索引访问和修改。 2. 链表:一种存储不同类型数据的结构,每个节点包含数据和指向下一个节点的指针。 3. 栈:一种后进先出(LIFO)的数据结构,可以通过压入(push)和弹出(pop)操作进行数据的存储和取出。 4. 队列:一种先进先出(FIFO)的数据结构,可以通过入队(enqueue)和出队(dequeue)操作进行数据的存储和取出。 5. 树:一种存储具有父子关系的数据结构,可以通过中序遍历、前序遍历和后序遍历等方式进行数据的访问和修改。 6. 图:一种存储具有节点和边关系的数据结构,可以通过广度优先搜索、深度优先搜索等方式进行数据的访问和修改。 这些数据结构在C语言中都有相应的实现方式,可以应用于各种不同的场景。C语言中的各种数据结构都有其优缺点,下面列举一些常见的数据结构的优缺点: 数组: 优点:访问和修改元素的速度非常快,适用于需要频繁读取和修改数据的场合。 缺点:数组的长度是固定的,不适合存储大小不固定的动态数据,另外数组在内存中是连续分配的,当数组较大时可能会导致内存碎片化。 链表: 优点:可以方便地插入和删除元素,适用于需要频繁插入和删除数据的场合。 缺点:访问和修改元素的速度相对较慢,因为需要遍历链表找到指定的节点。 栈: 优点:后进先出(LIFO)的特性使得栈在处理递归和括号匹配等问题时非常方便。 缺点:栈的空间有限,当数据量较大时可能会导致栈溢出。 队列: 优点:先进先出(FIFO)的特性使得

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值