Common modules of SaltStack

Posted by Flames on Thu, 04 Nov 2021 05:05:00 +0100

Common modules of SaltStack

SaltStack module introduction

Module is one of the components that SaltStack contacts most in daily use. It is used to manage object operations. It is also the entrance for SaltStack to manage by pushing. For example, our daily simple tasks such as executing commands, viewing package installation and service operation are realized through SaltStack Module.

After installing the Master and Minion packages, many modules will be installed on the system. You can view the list of all supported modules through the following command:

To view all modules # of this machine, only some want to view all modules on the official website

[root@server1 ~]# salt 'server1' sys.list_modulesserver1:
    - acl
    - aliases
    - alternatives
    - archive
    - artifactory
    - baredoc
    - beacons
    - bigip
    - btrfs
    - buildout
    - chroot
    - cloud
    - cmd
    - composer
    - config
    - consul
    - container_resource
    - cp
    - cron
    - cryptdev
    - data
    - defaults
    - devinfo
    - devmap
    - disk
    - django
    - dnsmasq
    - dnsutil
    - drbd
    - environ
    - ethtool
    - event
    - extfs
    - file
    - firewalld
    - freezer
    - gem
    - genesis
    - glassfish
    - gnome
    - google_chat
    - grafana4
    - grains
    - group
    - hashutil
    - helm
    - highstate_doc
    - hosts
    - http
    - hue
    - incron
    - ini
    - inspector
    - introspect
    - iosconfig
    - ip
    - ipset
    - iptables
    - jboss7
    - jboss7_cli
    - jinja
    - k8s
    - kernelpkg
    - key
    - keyboard
    - kmod
    - kubeadm
    - libcloud_compute
    - libcloud_dns
    - libcloud_loadbalancer
    - libcloud_storage
    - locale
    - locate
    - log
    - logrotate
    - lowpkg
    - lvm
    - mandrill
    - match
    - mattermost
    - mine
    - minion
    - modjk
    - mount
    - msteams
    - nagios_rpc
    - namecheap_domains
    - namecheap_domains_dns
    - namecheap_domains_ns
    - namecheap_ssl
    - namecheap_users
    - network
    - nexus
    - nftables
    - nova
    - nspawn
    - nxos
    - nxos_api
    - nxos_upgrade
    - openscap
    - openstack_config
    - opsgenie
    - out
    - pagerduty
    - pagerduty_util
    - pam
    - parallels
    - partition
    - peeringdb
    - pillar
    - pip
    - pkg
    - pkg_resource
    - ps
    - publish
    - pushover
    - pyenv
    - random
    - random_org
    - rbenv
    - rest_sample_utils
    - restartcheck
    - ret
    - rvm
    - s3
    - s6
    - salt_proxy
    - salt_version
    - saltcheck
    - saltutil
    - schedule
    - scsi
    - sdb
    - seed
    - serverdensity_device
    - service
    - shadow
    - slack
    - slsutil
    - smbios
    - smtp
    - solrcloud
    - sqlite3
    - ssh
    - state
    - status
    - statuspage
    - supervisord
    - sys
    - sysctl
    - sysfs
    - syslog_ng
    - system
    - telegram
    - telemetry
    - temp
    - test
    - timezone
    - tls
    - travisci
    - tuned
    - udev
    - uptime
    - user
    - vault
    - vbox_guest
    - virtualenv
    - vsphere
    - x509
    - xfs
    - xml
    - zabbix
    - zenoss

View all function s of the specified module

[root@server1 ~]# salt 'server1' sys.list_functions cmd
server1:
    - 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

View the usage of the specified module

[root@server1 ~]# salt 'server1' 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"}'
.......
.......

//SaltStack also supports the execution of multiple modules at a time by default. Module s are separated by commas, and the default parameters are also separated by commas,

[root@server1 ~]# salt 'node1' test.echo,cmd.run,service.status  holle,hostname,salt-minion
node1:
    ----------
    cmd.run:
        node1
    service.status:
        True
    test.echo:
        holle

Common modules of SaltStack

network of common modules of SaltStack

network.active_tcp returns all active connections

[root@server1 ~]# salt 'node1' network.active_tcp
node1:
    ----------
    0:
        ----------
        local_addr:
            192.168.244.135
        local_port:
            33806
        remote_addr:
            192.168.244.131
        remote_port:
            4505
    1:
        ----------
        local_addr:
            192.168.244.135
        local_port:
            22
        remote_addr:
            192.168.244.1
        remote_port:
            52918

network.calc_net calculates the network segment through IP and subnet mask

[root@server1 ~]# salt 'node1' network.calc_net  192.168.244.0 255.255.255.0
node1:
    192.168.244.0/24

network.connect test whether the network from minion to a server is connected

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

network.default_route view default route

[root@server1 ~]# salt 'node1' network.default_route
node1:
    |_
      ----------
      addr_family:
          inet
      destination:
          0.0.0.0
      flags:
          UG
      gateway:
          192.168.244.2
      interface:
          ens160
      netmask:
          0.0.0.0

network.get_fqdn view the FQDN of the host (fully qualified domain name)

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

network.get_hostname get hostname

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

network.get_route queries the routing information of a target network

[root@server1 ~]# salt 'node1' network.get_route 192.168.244.2
node1:
    ----------
    destination:
        192.168.244.2
    gateway:
        None
    interface:
        ens160
    source:
        192.168.244.135

network.hw_addr returns the MAC address of the specified network card

[root@server1 ~]# salt 'node1' network.hw_addr ens160
node1:
    00:0c:29:36:3e:51

Network.ifacostartswitch retrieves the interface name from a specific CIDR

[root@server1 ~]# salt 'node1' network.ifacestartswith 192.168
node1:
    - ens160

network.in_subnet determines whether the current host is in a network segment

[root@server1 ~]# salt 'node1' network.in_subnet 192.168.69.0/24
node1:
    False
[root@server1 ~]# salt 'node1' network.in_subnet 192.168.244.0/24
node1:
    True

network.interface returns the information of the specified network card

[root@server1 ~]# salt 'node1' network.interface ens160
node1:
    |_
      ----------
      address:
          192.168.244.135     #IP address
      broadcast:
          192.168.244.255      #gateway
      label:
          ens160            #adapter name
      netmask:
          255.255.255.0     #Subnet mask

network.interface_ip returns the IP address of the specified network card

[root@server1 ~]# salt 'node1' network.interface_ip ens160
node1:
    192.168.244.135

network.interfaces returns all network card information in the current system

[root@server1 ~]# salt 'node1' network.interfacesnode1:
    ----------
    ens160:
        ----------
        hwaddr:
            00:0c:29:36:3e:51
        inet:
            |_
              ----------
              address:
                  192.168.244.135
              broadcast:
                  192.168.244.255
              label:
                  ens160
              netmask:
                  255.255.255.0
        inet6:
            |_
              ----------
              address:
                  fe80::22a0:ac79:2d1a:18b7
              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 returns a list of IPv4 addresses

This function will ignore the address of 127.0.0.1

[root@server1 ~]# salt 'node1' network.ip_addrs
node1:
    - 192.168.244.135

network.netstat returns all open ports and status

[root@server1 ~]# salt 'node1' network.netstat
node1:
    |_
      ----------
      inode:
          25874
      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
    |_
      ----------
      inode:
          25875
      local-address:
          [::1]:323
      program:
          chronyd
      proto:
          udp
      recv-q:
          0
      remote-address:
          [::]:*
      send-q:
          0
      user:
          0
    |_
      ----------
      inode:
          28970
      local-address:
          0.0.0.0:22
      program:
          sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          0.0.0.0:*
      send-q:
          128
      state:
          LISTEN
      user:
          0
    |_
      ----------
      inode:
          32306
      local-address:
          192.168.244.135:33806
      program:
          salt-minion
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.244.131:4505
      send-q:
          0
      state:
          ESTABLISHED
      user:
          0
    |_
      ----------
      inode:
          0
      local-address:
          192.168.244.135:60566
      program:
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.244.131:4506
      send-q:
          0
      state:
          TIME-WAIT
      user:
          0
    |_
      ----------
      inode:
          1121659
      local-address:
          192.168.244.135:22
      program:
          sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          192.168.244.1:52918
      send-q:
          0
      state:
          ESTABLISHED
      user:
          0
    |_
      ----------
      inode:
          28972
      local-address:
          [::]:22
      program:
          sshd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          [::]:*
      send-q:
          128
      state:
          LISTEN
      user:
          0
    |_
      ----------
      inode:
          29140
      local-address:
          *:80
      program:
          httpd
      proto:
          tcp
      recv-q:
          0
      remote-address:
          *:*
      send-q:
          128
      state:
          LISTEN
      user:
          0

network.ping use the ping command to test the connectivity to a host

[root@server1 ~]# salt 'node1' network.ping baidu.com
node1:
    PING baidu.com (220.181.38.251) 56(84) bytes of data.
    64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=1 ttl=128 time=46.8 ms
    64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=2 ttl=128 time=50.6 ms
    64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=4 ttl=128 time=45.7 ms
    
    --- baidu.com ping statistics ---
    4 packets transmitted, 3 received, 25% packet loss, time 29ms
    rtt min/avg/max/mdev = 45.675/47.679/50.587/2.104 ms

network.reverse_ip returns the reverse address of a specified IP address

[root@server1 ~]# salt 'node1' network.reverse_ip 192.168.244.135
node1:
    135.244.168.192.in-addr.arpa

service of common modules of SaltStack

service.available determines whether the specified service is available

[root@server1 ~]# salt 'node1' service.available sshd
node1:
    True
[root@server1 ~]# salt 'node1' service.available httpd
node1:
    True
[root@server1 ~]# salt 'node1' service.available mysql
node1:
    False   #Mysql is not installed

service.get_all get all running services

[root@server1 ~]# salt 'node1' service.get_all
node1:
    - NetworkManager
    - NetworkManager-dispatcher
    - NetworkManager-wait-online
    - auditd
    - autovt@
    - basic.target
    - blk-availability
    ......
    ......

service.disabled check whether the specified service is started or not

[root@server1 ~]# salt 'node1' service.disabled httpd
node1:
    False

service.enabled checks whether the specified service is started automatically

[root@server1 ~]# salt 'node1' service.enabled httpd 
node1:
    True

The service.disable setting specifies that the service will not start automatically after startup

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

service.enable sets the automatic startup of the specified service after startup

[root@server1 ~]# salt 'node1' service.enable httpd
node1:
    True
[root@server1 ~]#  salt 'node1' service.enabled httpd
node1:
    True

service.reload reloads the specified service

[root@server1 ~]# salt 'node1' service.reload httpd
node1:
    True

service.stop stops the specified service

[root@server1 ~]# salt 'node1' service.stop httpd
node1:
    True

service.start starts the specified service

[root@server1 ~]# salt 'node1' service.start httpd
node1:
    True

service.restart restart the specified service

[root@server1 ~]# salt 'node1' service.restart httpd 
node1:
    True

service.status view the status of the specified service

[root@server1 ~]# salt 'node1' service.status httpdnode1:
    True

pkg of common modules of SaltStack

pkg.download only downloads packages but does not install them

This function will download the specified software package, but you need to install Yum utils on the minion side. You can use cmd.run for remote installation

[root@server1 ~]# salt 'node1' pkg.download wget
node1:
    ----------
    wget:
        /var/cache/yum/packages/wget-1.19.5-10.el8.x86_64.rpm
[root@server1 ~]# salt 'node1' cmd.run "ls /var/cache/yum/packages"
node1:
    wget-1.19.5-10.el8.x86_64.rpm

pkg.file_list lists the files for the specified package or all packages installed on the system

[root@server1 ~]# salt 'node1' service.get_all
node1:
    - NetworkManager
    - NetworkManager-dispatcher
    - NetworkManager-wait-online
    - auditd
    - autovt@
    - basic.target
    - blk-availability
    - bluetooth.target
    - chrony-dnssrv@
.......
.....

pkg.group_info view package group information

[root@server1 ~]# salt 'node1' 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

pkg.group_list lists all package groups in the system

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

pkg.install install software

[root@server1 ~]# salt 'node1' pkg.install  wget
node1:
    ----------
    wget:
        ----------
        new:
            1.19.5-10.el8
        old:
            1.19.5-8.el8_1.1

pkg.list_downloaded lists the packages that have been downloaded locally

pkg.list_pkgs lists the currently installed packages as a dictionary

[root@server1 ~]# salt 'node1' 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
.......
......

pkg.owner lists which package provides the specified file

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

pkg.remove uninstall the specified software

[root@server1 ~]# salt 'node1' cmd.run 'rpm -qa|grep wget'
node1:
    wget-1.19.5-10.el8.x86_64
[root@server1 ~]# salt 'node1' pkg.remove wget
node1:
    ----------
    wget:
        ----------
        new:
        old:
            1.19.5-10.el8
//To unload multiple files, separate them with commas

pkg.upgrade upgrade upgrade all software packages in the system or upgrade the specified software packages

[root@server1 ~]# salt 'node1' pkg.upgrade name=openssl
node1:
    ----------
    openssl:
        ----------
        new:
            1:1.1.1g-15.el8_3
        old:
            1:1.1.1c-15.el8
    openssl-libs:
        ----------
        new:
            1:1.1.1g-15.el8_3
        old:
            1:1.1.1c-15.el8
//If you want to upgrade all software packages in the system, remove the name parameter

state of common SaltStack modules

state.show_highstate displays which advanced states are in the current system

[root@server1 ~]# salt 'node1' 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

state.highstate execute advanced state

[root@server1 ~]# salt 'node1' state.highstate web.apache.apache
node1:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 18:36:34.088424
    Duration: 274.147 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 18:36:34.366147
    Duration: 27.497 ms
     Changes:   

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

state.show_state_usage displays the execution of advanced status in the current system

[root@server1 ~]# salt 'node1' state.show_state_usage
node1:
    ----------
    base:
        ----------
        count_all:
            4
        count_unused:
            3
        count_used:
            1
        unused:
            - top
            - web.apache.apache
            - web.nginx.install
        used:
            - web.apache.install
    dev:
        ----------
        count_all:
            0
        count_unused:
            0
        count_used:
            0
        unused:
        used:
    prod:
        ----------
        count_all:
            0
        count_unused:
            0
        count_used:
            0
        unused:
        used:
    test:
        ----------
        count_all:
            0
        count_unused:
            0
        count_used:
            0
        unused:
        used:

state.show_top returns the top-level data that minion will use for highstate

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

state.top executes the specified top file instead of the default

[root@server1 ~]# salt 'node1' state.top  top.sls
node1:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 18:40:32.176737
    Duration: 267.434 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 18:40:32.445630
    Duration: 27.371 ms
     Changes:   

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

state.show_sls displays the status data in a specific sls or sls file list on the master

[root@server1 ~]# salt 'server1' state.show_sls web.apache.install
server1:
    ----------
    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

user common modules

user.add create user

[root@server1 ~]# salt 'node1' user.add   tom
node1:
    True
[root@node1 ~]# id tom 
uid=1001(tom) gid=1001(tom) groups=1001(tom)

Specify uid group

[root@server1 ~]# salt 'node1' user.add   name=jack uid=1003    groups=tom
node1:
    True
uid=1003(jack) gid=1003(jack) groups=1003(jack),1001(tom)

Topics: saltstack