This is the 19th day of my participation in the August More Text Challenge

A lifelong learner, practitioner and sharer committed to the path of technology, a busy and occasionally lazy original blogger, an occasionally boring and occasionally humorous teenager.

Welcome to dig friends wechat search “Jie Ge’s IT journey” attention!

Use Saltstack to execute commands remotely

First, use Saltstack to remotely execute commands

One of Saltstack’s major advantages is its ability to execute remote commands.

The operation method is similar to funC (fedorahosted.org/func/), which helps O&M personnel achieve a centralized operation platform.

Docs. Saltstack. Cn/switchable viewer/targ…

Salt ‘< action target >’ < method > [parameter]

1.1 Viewing the Memory usage of the controlled PC

[root@saltstack-master master]# salt 'saltstack_web1group_1' cmd.run 'free -m'
saltstack_web1group_1:
                 total       used       free     shared    buffers     cached
    Mem:           230        223          6          0         15         55
    -/+ buffers/cache:        153         77
    Swap:         2083        222       1861
Copy the code

For < operation target >, SaltStack provides multiple methods to filter the controlled end (ID). Common parameters are listed below.

1.2 Checking whether the host ID starting with the SaltStack character on the controlled end is normal

  • -e: –pcre: matches with the regular expression.
[root@saltstack-master master]# salt -E '^saltstack.*' test.ping
saltstack_web1group_1:
    True
saltstack_web1group_2:
    True
Copy the code

1.3 Obtaining the host ids Saltstack_web1group_1 and saltStack_web1group_2 Obtain the full OS release names

  • -l: –list is filtered as a list of host ID names. The format is similar to Python’s, that is, different host ID names are separated by commas.
[root@saltstack-master master]# salt -L 'saltstack_web1group_1,saltstack_web1group_2' grains.item osfullname
saltstack_web1group_1:
    ----------
    osfullname:
        CentOS
saltstack_web1group_2:
    ----------
    osfullname:
        CentOS
Copy the code

1.4 Obtaining the Python version number of the host distribution 6.5

  • -g: — Grain Matches and filters grains based on the information of the controlled host (grains is an important component of saltStack, which collects basic system information of the controlled host) in the format of ‘:’.

For example, if the host whose kernel is Linux is filtered, you can write it as ‘kernel:Linux’. If you also need regular expression support, you can cut it to –grain-pcre.

[root@saltstack-master ~]# salt -G 'osrelease:6.5' cmd.run 'python -V'
saltstack_web1group_1:
    Python 2.6.6
saltstack_web1group_2:
    Python 2.6.6
Copy the code

1.5 Filter all hosts that have the ‘Apache: HTTPD’ pillar value

  • -I: –pillar Filters and matches the information in the format of object Name: Object value based on the pillar of the controlled host (this pillar defines any data related to the controlled host and can be used by other components).

Probe host connectivity with “HTTPD :root:/data” information.

[root@saltstack-master ~]# salt -I 'httpd:root:/data' test.ping saltstack_web1group_1: True saltstack_web1group_2: True The configuration files of the pillar attribute are as follows: HTTPD: root: /dataCopy the code

1.6 Detect connectivity of web1Group (or Web2Group) controlled hosts

  • -n: –nodegroup Filters packets based on the group names in the master configuration file on the main control end.

The following group information (host information supports regular expression, grain, and conditional operators) is divided by service type. Different services have the same characteristics, such as deployment environment, application platform, and configuration file.

@ : Indicates that the following host ids are in a list. Host ids are separated by commas (,). G@ : Specifies the description in grain format. s@ : indicates that the description is in IP subnet or address format.

[root@saltstack-master salt]# vim /etc/salt/master
nodegroups:
  web1group: 'L@saltstack_web1group_1,saltstack_web1group_2'
[root@saltstack-master salt]# salt -N web1group test.ping
saltstack_web1group_2:
    True
saltstack_web1group_1:
    True
Copy the code

1.7 Checking the connectivity of hosts that start with SALT and run Centos

  • -c: –compound Matches host information of different rules based on the condition operators not, and, and or.
[root@saltstack-master salt]# salt -C 'E@^salt.* and G@os:Centos' test.ping
saltstack_web1group_1:
    True
saltstack_web1group_2:
    True
Copy the code

1.8 Detecting host connectivity that does not start with SaltStack

The NOT statement cannot be executed as the first condition, but it can be circumvented by:

[root@saltstack-master salt]# salt -C '* and not E@^Jenkins.*' test.ping
saltstack_web1group_2:
    True
saltstack_web1group_1:
    True
Copy the code

1.9 Checking whether the slave device is in the 192.168.0.0/16 network segment

  • -s: — IPCIDr matches the IP address or IP subnet of the controlled host
[root@saltstack-master salt]# salt. ping saltStack_web1group_1: True saltstack_web1group_2: TrueCopy the code

Recommended reading

Saltstack centralized management platform installation

In this paper, to the end.


Original is not easy, if you think this article is useful to you, please kindly like, comment or forward this article, because this will be my power to output more high-quality articles, thank you!

By the way, please give me some free attention! In case you get lost and don’t find me next time.

See you next time!