SaltStack常用的模块

目录

  • 1. SaltStack模块介绍
  • 2. SaltStack常用模块
    • 2.1 SaltStack常用模块之network

      • 2.1.1 network.active_tcp
      • 2.1.2 network.calc_net
      • 2.1.3 network.connect
      • 2.1.4 network.default_route
      • 2.1.5 network.get_fqdn
      • 2.1.6 network.get_hostname
      • 2.1.7 network.get_route
      • 2.1.8 network.hw_addr
      • 2.1.9 network.ifacestartswith
      • 2.1.10 network.in_subnet
      • 2.1.11 network.interface
      • 2.1.12 network.interface_ip
      • 2.1.13 network.interfaces
      • 2.1.14 network.ip_addrs
      • 2.1.15 network.netstat
      • 2.1.16 network.ping
      • 2.1.17 network.reverse_ip
    • 2.2 SaltStack常用模块之service
      • 2.2.1 service.available
      • 2.2.2 service.get_all
      • 2.2.3 service.disabled
      • 2.2.4 service.enabled
      • 2.2.5 service.enable
      • 2.2.6 service.disable

1. SaltStack模块介绍

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

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

//查看所有module列表
[[email protected] ~]# salt 'node1*' sys.list_modules
node1:
    - acl
    - aliases
    - alternatives
    - ansible
    - archive
    - artifactory
    - beacons
    - bigip
    - btrfs
    - buildout
    - chroot
    - cloud
    - cmd
    - composer
    - config
    - consul
...

//查看指定module的所有function
[[email protected] ~]# salt 'node1*' 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

//查看指定module的用法
[[email protected] ~]# 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.

    CLI Example:

        salt '*' cmd.exec_code ruby 'puts "cheese"'
...

2. SaltStack常用模块

2.1 SaltStack常用模块之network

2.1.1 network.active_tcp

[[email protected] ~]# salt '*' network.active_tcp
node1:
    ----------
    0:
        ----------
        local_addr:
            192.168.136.164
        local_port:
            4505
        remote_addr:
            192.168.136.164
        remote_port:
            41790
    1:
        ----------
        local_addr:
            192.168.136.164
        local_port:
            4505
        remote_addr:
            192.168.136.165
        remote_port:
            56486
    2:
        ----------
        local_addr:
            192.168.136.164
        local_port:
            41790
        remote_addr:
            192.168.136.164
        remote_port:
            4505
    3:
        ----------
        local_addr:
            192.168.136.164
        local_port:
            22
        remote_addr:
            192.168.136.1
        remote_port:
            52454
node2:
    ----------
    0:
        ----------
        local_addr:
            192.168.136.165
        local_port:
            56486
        remote_addr:
            192.168.136.164
        remote_port:
            4505
    1:
        ----------
        local_addr:
            192.168.136.165
        local_port:
            22
        remote_addr:
            192.168.136.1
        remote_port:
            52458

2.1.2 network.calc_net

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

[[email protected] ~]# salt '*' network.calc_net 192.168.136.164 255.255.255.0
node2:
    192.168.136.0/24
node1:
    192.168.136.0/24

2.1.3 network.connect

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

[[email protected] ~]# salt '*' network.connect baidu.com 80
node1:
    ----------
    comment:
        Successfully connected to baidu.com (220.181.38.148) on tcp port 80
    result:
        True
node2:
    ----------
    comment:
        Successfully connected to baidu.com (220.181.38.148) on tcp port 80
    result:
        True

2.1.4 network.default_route

查看默认路由

[[email protected] ~]# salt '*' network.default_route
node2:
    |_
      ----------
      addr_family:
          inet
      destination:
          0.0.0.0
      flags:
          UG
      gateway:
          192.168.136.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:
node1:
    |_
      ----------
      addr_family:
          inet
      destination:
          0.0.0.0
      flags:
          UG
      gateway:
          192.168.136.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(完全限定域名)

[[email protected] ~]# salt '*' network.get_fqdn
node2:
    node2
node1:
    node1

2.1.6 network.get_hostname

获取主机名

[[email protected] ~]# salt '*' network.get_hostname
node2:
    node2
node1:
    node1

2.1.7 network.get_route

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

[[email protected] ~]# salt '*' network.get_route 192.168.136.164
node2:
    ----------
    destination:
        192.168.136.164
    gateway:
        None
    interface:
        ens33
    source:
        192.168.136.165
node1:
    ----------
    destination:
        192.168.136.164
    gateway:
        None
    interface:
        lo
    source:
        192.168.136.164

2.1.8 network.hw_addr

返回指定网卡的MAC地址

[[email protected] ~]# salt '*' network.hw_addr ens33
node2:
    00:0c:29:00:52:6e
node1:
    00:0c:29:26:a4:52

2.1.9 network.ifacestartswith

从特定CIDR检索接口名称

[[email protected] ~]# salt '*' network.ifacestartswith 192.168
node2:
    - ens33
node1:
    - ens33

2.1.10 network.in_subnet

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

[[email protected] ~]# salt '*' network.in_subnet 192.168.136.0/24
node2:
    True
node1:
    True

2.1.11 network.interface

返回指定网卡的信息

[[email protected] ~]# salt '*' network.interface ens33
node1:
    |_
      ----------
      address:
          192.168.136.164
      broadcast:
          192.168.136.255
      label:
          ens33
      netmask:
          255.255.255.0
node2:
    |_
      ----------
      address:
          192.168.136.165
      broadcast:
          192.168.136.255
      label:
          ens33
      netmask:
          255.255.255.0

2.1.12 network.interface_ip

返回指定网卡的IP地址

[[email protected] ~]# salt '*' network.interface_ip ens33
node2:
    192.168.136.165
node1:
    192.168.136.164

2.1.13 network.interfaces

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

[[email protected] ~]# salt '*' network.interfaces
node1:
    ----------
    ens33:
        ----------
        hwaddr:
            00:0c:29:26:a4:52
        inet:
            |_
              ----------
              address:
                  192.168.136.164
              broadcast:
                  192.168.136.255
              label:
                  ens33
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
              address:
                  fe80::ff7a:d77e:ff2:64bd
              prefixlen:
                  64
              scope:
                  link
        up:
            True
    ens38:
        ----------
        hwaddr:
            00:0c:29:26:a4:5c
        up:
            True
    ens39:
        ----------
        hwaddr:
            00:0c:29:26:a4:66
        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
node2:
    ----------
    ens33:
        ----------
        hwaddr:
            00:0c:29:00:52:6e
        inet:
            |_
              ----------
              address:
                  192.168.136.165
              broadcast:
                  192.168.136.255
              label:
                  ens33
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
              address:
                  fe80::cb81:16ba:de26:872d
              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的地址

[[email protected] ~]# salt '*' network.ip_addrs
node2:
    - 192.168.136.165
node1:
    - 192.168.136.164

2.1.15 network.netstat

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

[[email protected] ~]# salt '*' network.netstat
node2:
    |_
      ----------
      inode:
          19461
      local-address:
          0.0.0.0:22
      program:
          1005/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命令测试到某主机的连通性

[[email protected] ~]# salt '*' network.ping baidu.com
node1:
    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=36.9 ms
    64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=128 time=92.2 ms
    64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=3 ttl=128 time=42.6 ms
    64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=4 ttl=128 time=98.2 ms

    --- baidu.com ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3006ms
    rtt min/avg/max/mdev = 36.962/67.500/98.216/27.863 ms
node2:
    PING baidu.com (39.156.69.79) 56(84) bytes of data.
    64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=128 time=36.8 ms
    64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=128 time=92.1 ms
    64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=128 time=42.5 ms
    64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=4 ttl=128 time=94.8 ms

    --- baidu.com ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3006ms
    rtt min/avg/max/mdev = 36.842/66.605/94.882/27.003 ms

2.1.17 network.reverse_ip

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

[[email protected] ~]# salt '*' network.reverse_ip 192.168.136.164
node2:
    164.136.168.192.in-addr.arpa
node1:
    164.136.168.192.in-addr.arpa

2.2 SaltStack常用模块之service

2.2.1 service.available

判断指定的服务是否可用

[[email protected] ~]# salt '*' service.available sshd
node1:
    True
node2:
    True
[[email protected] ~]# salt '*' service.available vsftpd
node1:
    False
node2:
    False

2.2.2 service.get_all

获取所有正在运行的服务

[[email protected] ~]# salt '*' service.get_all
node1:
    - NetworkManager
    - NetworkManager-dispatcher
    - NetworkManager-wait-online
    - arp-ethers
    - auditd
    - [email protected]
    - basic.target
    - blk-availability
    - bluetooth.target
    - brandbot
    - brandbot.path
    - [email protected]
    - [email protected]
    - chrony-wait
    - chronyd
    - console-getty
    - console-shell
    - [email protected]
    - cpupower
    - crond
    - cryptsetup-pre.target
    - cryptsetup.target
    - ctrl-alt-del.target
...

2.2.3 service.disabled

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

[[email protected] ~]# salt '*' service.disabled firewalld
node1:
    True
node2:
    True

2.2.4 service.enabled

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

[[email protected] ~]# salt '*' service.enabled firewalld
node2:
    False
node1:
    False

2.2.5 service.enable

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

[[email protected] ~]# salt '*' service.enable firewalld
node2:
    True
node1:
    True

2.2.6 service.disable

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

[[email protected] ~]# salt '*' service.disable firewalld
node2:
    True
node1:
    True

原文地址:https://www.cnblogs.com/liping0826/p/12336967.html

时间: 2024-10-11 22:35:14

SaltStack常用的模块的相关文章

Saltstack 常用的模块及API

Saltstack提供了非常丰富的功能模块,设计操作系统的基础功能,常用工具支持等, 官网模块介绍 http://docs.saltstack.com/ref/modules/all/index.html 一 列出当前版本支持的模块 [[email protected]020 ~]# salt '*' sys.list_modules hzbj-tomcat-021: - acl - aliases - alternatives - apache - archive - artifactory

saltstack使用指南----常用执行模块

saltstack常用执行模块: cron模块 archive模块 cmd模块 cp模块 dnsutil模块 file模块 group模块 network模块 service模块 pkg模块 user模块 一.cron模块: 功能:实现被控主机的crontab操作 [[email protected] ~]# salt '*' sys.list_functions cron izwz9f8xrvty50quc2gq50z: - cron.list_tab - cron.ls - cron.raw

saltstack常用模块及组件备忘

Saltstack分为主控端和被控端.主控端的salt服务启动:systemctl start salt-master,被控端的服务启动:systemctl start salt-minion1.Saltstack的防火墙配置主控端防火墙允许TCP4505和4506端口,被控端不需要配置.原理是被控端直接与主控端的zeromq建立长链接,接受广播到的任务信息并执行. iptables -I INPUT -m state --state new -m tcp -p tcp --dport 4506

Linux驱动学习之常用的模块操作命令

1.常用的模块操作命令 (1)lsmod(list module,将模块列表显示),功能是打印出当前内核中已经安装的模块列表 (2)insmod(install module,安装模块),功能是向当前内核中去安装一个模块,用法是insmod xxx.ko (3)modinfo(module information,模块信息),功能是打印出一个内核模块的自带信息.,用法是modinfo xxx.ko,注意要加.ko,也就是说是一个静态的文件形式. (4)rmmod(remove module,卸载

常用的模块 一 --sys --os ---haslib摘要算法

模块的加载顺序 ===>> python 解释器 ====>>> 第三方模块 =====>>> 自定义模块 常用的模块  1 import sys print(sys.modules) # sys.modules中找到当前已经加载的模块,sys.modules是一个字典 ============   sys 模块============ sys与python解释其交互相关的接口 import sys # sys与python解释其交互相关的接口 print(

详解Ansible服务常用命令模块

ansible可以使用命令行方式进行自动化管理,基本语法如下: ansible 主机名 -m 模块名称 -a 模块特有参数 ansible的命令行管理工具都是由一系列模块.参数所支持的,可以在命令后面加上-h或--help获取帮助.如使用ansible-doc -h或者ansible-doc --help查看其帮助信息ansible-doc是用来查看模块帮助信息的工具,最主要的选项 -l用来列出可使用的模块, -s用来列出某个模块的描述信息和使用示例. 以下是我列出yum模块的描述信息和操作动作

Python学习手册之__main__,常用第三方模块和打包发布

在上一篇文章中,我们介绍了 Python 的 元组拆包.三元运算符和对 Python 的 else 语句进行了深入讲解,现在我们介绍 Python 的 __main__ 模块.常用第三方模块和打包发布.查看上一篇文章请点击:https://www.cnblogs.com/dustman/p/10057482.html __main__大多数 Python 代码要么是导入的模块,要么是执行某些任务的脚本.但是,有时需要使一个文件既可以作为模块导入,也可以作为脚本运行. def sayHello()

Saltstack常用模块及API(4)

Saltstack提供了非常丰富的功能模块(用python所写),涉及操作系统的基础功能.常用工具支持等,当然也可以通过sys模块列出当前版本支持的模块 #salt '*' sys.list_modules 接下来抽取常见的模块进行介绍,同时也会列举模块的API使用方法.API的原理是通过调用master client模块,实例化一个LocalClient对象,再调用cmda()方法来实现的.以下是API实现test.ping的示例: import salt.client client = sa

Saltstack系列3:Saltstack常用模块及API

说明 salt '*' sys.list_modules #列出当前版本支持的模块 API原理是通过调用master client模块,实例化一个LocalClient对象,再调用cmd()方法来实现的.以下API实现test.ping的示例:(其他API调用只需要改变cmd即可) import salt.client client = salt.client.LocalClient() ret = client.cmd('*','test.ping) #cmd内格式:'<操作目标>','&l