Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Saltstack of automatic Operation and maintenance (2) introduction and use of states

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

What is Salt States?

Salt States is an extension of the Salt module. The state system used by the main system is called the SLS system. SLS represents the Saltstack State,Salt is a number of state files that contain information about how to configure the Salt child nodes. These states are stored in a directory and can be written in many different formats. We can think of these Salt States as the configuration mode of the minion management script, and specify the target minions by configuring the SLS file. It is possible to manage the running state of minions on master.

2. Salt State tree

Like system files, salt state files have their own tree system, which is configured through the file_roots configuration in the configuration file, and the default configuration is as follows:

# configure the following options in / etc/salt/master to specify that the root directory of salt state is / srv/salt

[root@server01 salt] # grep-A3 file_roots: / etc/salt/master# file_roots:# base:#-/ srv/salt/# dev:--file_roots: base:-/ srv/salt [root@server01 salt] #

Restart after modifying the master configuration file to make the configuration file effective

[root@server01 salt] # systemctl restart salt-master.service

Top file for Salt State

The top.sls file is the default sls file for SaltState. When it is not specifically specified, salt will call top.sls by default to execute. For example

Salt'* 'state.highstate

The top.sls file needs to be created manually, and the location is under the directory / srv/salt defined by the Salt State tree:

Create a top.sls file

[root@server01 salt] # vim top.sls # add the following. Note that the sls file uses two spaces as the base directory indented # base matching status, that is, / srv/salt#'* 'indicates matching all minions, that is, all certified clients #-webserver: execute the webserver.sls status file or webserver/init.sls file under the base directory base:' *':-webserver

For example: create a Salt State file

A webserver file is defined in the above top.sls file, so let's add a webserver file

Add the following to the file / srv/salt/webserver.sls file

# Apache:ID declaration, same as the name specified by yum install Apache

# pkg: status declaration, indicating what the following actions are

#-installed: function declaration, indicating that the sls file specifies the function to perform the operation

[root@server01 salt] # vim webserver.slshttpd: pkg:-installed [root@server01 salt] #

Execute the Salt State file we defined to update the status of all minion

[root@server01 salt] # salt'* 'state.highstateserver02:- ID: httpd Function: pkg.installed Result: True Comment: Package httpd is already installed. Started: 02 ms Changes 56 Summary-Succeeded: 15.414698 Duration: 1114.898 ms Changes: Summary-Succeeded: 1Failed: 0-Total states run: 1 server03 True Comment-ID: httpd Function: pkg.installed Result: True Comment: Package httpd is already installed. Started: 02:56:15.448065 Duration: 1129.985 ms Changes: Summary-Succeeded: 1Failed: 0-Total states run: 1 [root@server01 salt] #

Note: according to the contents of top.sls and webserver.sls above, all minion will install Apache service. If you modify the matching of minion in top.sls, only the matched minion will operate. Of course, you can also specify the sls file to execute, here the sls file is state.sls

[root@server01 salt] # salt 'server02' state.sls webserverserver02:- ID: httpd Function: pkg.installed Result: True Comment: Package httpd is already installed. Started: 02:58:59.038307 Duration: 885.997 ms Changes: Summary-Succeeded: 1Failed: 0-Total states run: 1 [root@server01 salt] #

# enable debug log (executed on minion)

[root@server02 ~] # salt-minion-l debug

# set default timeout (server execution)

[root@server01 salt] # salt'* 'state.highstate-t 60

[root@server01 salt] # salt'* 'state.highstate-t 60server03 httpd Function-ID: pkg.installed Result: True Comment: Package httpd is already installed. Duration: 900.103 ms Changes: Summary-Succeeded: 1Failed: 0-Total states run: 1 server02 Started-ID: httpd Function: pkg.installed Result: True Comment: Package httpd is already installed. Started: 03:03:30.229662 Duration: 966.72 ms Changes: Summary-Succeeded: 1Failed: 0-Total states run: 1 [root@server01 salt] #

III. Actual combat operation

After a brief introduction above, let's demonstrate file distribution and software installation.

1. Distribute / etc/hosts files

1) prepare the sls file

[root@server01 salt] # pwd/srv/salt [root@server01 salt] # cat hosts.sls/etc/hosts: file.managed:-source: salt://testfiles/hosts-user: root-group: root-mode: 644 [root@server01 salt] # [root@server01 salt] # cat top.slsbase:'*':-webserver-hosts [root@server01 salt] # [root@server01 salt] # mkdir testfiles [root@server01 salt ] # cp / etc/hosts testfiles/ [root@server01 salt] # lltotal 12 root root Jun 12 21:21 hosts.slsdrwxr-xr-x 2 root root 19 Jun 13 03:08 testfiles-rw-r--r-- 1 root root 41 Jun 13 03:07 top.sls-rw-r--r-- 1 root root 30 Jun 13 02:56 webserver.sls [root@server01 salt] # tree. ├── hosts.sls ├── testfiles │ └── Hosts ├── top.sls └── webserver.sls1 directory 4 files [root@server01 salt] #

A. Use state.highstate to call

Note: the premise of using state.highstate calls is that the top.sls file exists, so you need to write the top.sls file in advance.

[root@server01 salt] # salt'* 'state.highstateserver03:- ID: httpd Function: pkg.installed Result: True Comment: Package httpd is already installed. Started: 03Vl11 Started: 08.018494 Duration: 937.237 ms Changes:-ID: / etc/hosts Function: file.managed Result: True Comment: File / etc/hosts is in the correct state Started: 03VO3Parade 11VO8.984360 Duration: 5.308 ms Changes: Summary-Succeeded: 2Failed: 0- -Total states run: 2 server02 httpd Function-ID: pkg.installed Result: True Comment: Package httpd is already installed. Started: 03ms Changes:-ID: / etc/hosts Function: file.managed Result: True Comment: File / etc/hosts is in the correct state Started: 03ms Changes:-ID: / etc/hosts Function: 114,927716 Duration: 4.522 ms Changes: Summary-Succeeded: 2Failed: 0- -Total states run: 2 [root@server01 salt] #

B. Use state.sls to call

[root@server01 salt] # salt'* 'state.sls hostsserver03:- ID: / etc/hosts Function: file.managed Result: True Comment: File / etc/hosts is in the correct state Started: 03VO3VOUL12 1Failed 05.888857 Duration: 17.182 ms Changes: Summary-Succeeded: 1Failed: 0-Total states Run: 1server02file.managed Result-ID: / etc/hosts Function: file.managed Result: True Comment: File / etc/hosts is in the correct state Started: 03VO3VO12 Vera 05.903366 Duration: 16.217 ms Changes: Summary-Succeeded: 1Failed: 0-Total states run: 1 [root@server01 salt] #

2. Install the software package

1) prepare the sls file

[root@server01 salt] # vim package.slspkg-install: pkg.installed:-names:-gcc-lrzsz-tree-dos2unix [root@server01 salt] # [root@server01 salt] # vim top.slsbase: 'server02':-package [root@server01 salt] #

A. Use state.highstate to call

[root@server01 salt] # salt 'server02' state.highstateserver02:- ID: pkg-install Function: pkg.installed Name: gcc Result: True Comment: Package gcc is already installed. Started: 03V 16V Duration: 888.648 ms Changes:-ID: pkg-install Function: pkg.installed Name: tree Result: True Comment: Package tree is already installed. Started: 03153802 Duration: 0.405 ms Changes:-ID: pkg-install Function: pkg.installed Name: dos2unix Result: True Comment: The following packages were installed/updated: dos2unix Started: 03VO3VO3VO116VOUD 43.534278 Duration: 188849.681 ms Changes:- Dos2unix:-new: 6.0.3-4.el7 old:- ID: pkg-install Function: pkg.installed Name: lrzsz Result: True Comment: Package lrzsz is already installed. Started: 03 ms Changes 1915 Duration: 0.436 ms Changes: Summary-Succeeded: 4 (changed=1) Failed: 0-Total states run: 4 [root@server01 salt] #

B. Use state.sls to call

[root@server01 salt] # salt 'server02' state.sls packageserver02:- ID: pkg-install Function: pkg.installed Name: gcc Result: True Comment: Package gcc is already installed. Started: 03ms Changes:-ID: pkg-install Function: pkg.installed Name: tree Result: True Comment: Package tree is already installed. Started: 03ms Changes:-ID: pkg.installed Name: dos2unix Result: True Comment: Package dos2unix is already installed. Started: 03ms Changes:-ID: pkg-install Function: pkg.installed Name: lrzsz Result: True Comment: Package lrzsz is already installed. Started: 03:23:32.796745 Duration: 0.359 ms Changes: Summary-Succeeded: 4Failed: 0-Total states run: 4 [root@server01 salt] #

Other methods of daily use

3. Manage scheduled tasks

1) prepare the sls file (add scheduled tasks)

[root@server01 salt] # vim cron.sls/usr/sbin/ntpdate 210.72.145.44 64.147.116.229 time.nist.gov > / dev/null 2 > & 1: cron.present:-identifier: SUPERCRON-user: root-minute:'0' [root@server01 salt] #

2) use state.sls to call

[root@server01 salt] # salt 'server02' state.sls cronserver02:- ID: / usr/sbin/ntpdate 210.72.145.44 64.147.116.229 time.nist.gov > / dev/null 2 > & 1 Function: cron.present Result: True Comment: Cron / usr/sbin/ntpdate 210.72.145.44 64.147.116.229 time.nist.gov > / dev/null 2 > & 1 already present Started: 03:41:04.035604 Duration: 10.506 ms Changes: Summary-Succeeded: 1Failed: 0-Total states run: 1 [root@server01 salt] #

# check the execution result

[root@server01 salt] # salt 'server02' cron.raw_cron rootserver02: # Lines below here are managed by Salt, do not edit # SALT_CRON_IDENTIFIER:SUPERCRON 0 * / usr/sbin/ntpdate 210.72.145.44 64.147.116.229 time.nist.gov > / dev/null 2 > & 1 [root@server01 salt] #

3) prepare sls files (delete scheduled tasks)

[root@server01 salt] # vim cron.sls / usr/sbin/ntpdate 210.72.145.44 64.147.116.229 time.nist.gov > / dev/null 2 > & 1: cron.absent:-identifier: SUPERCRON-user: root-minute:'0' [root@server01 salt] # cat top.slsbase: 'server02':-cron [root@server01 salt] #

4) use state.highstate to call

[root@server01 salt] # salt 'server02' state.highstateserver02:- ID: / usr/sbin/ntpdate 210.72.145.44 64.147.116.229 time.nist.gov > / dev/null 2 > & 1 Function: cron.absent Result: True Comment: Cron / usr/sbin/ntpdate 210.72.145.44 64.147.116.229 time.nist.gov > / dev/null 2 > & 1 removed from root's Crontab Started: 03ms Changes 4515 13.267202 Duration: 26.493 ms Changes:-root: / usr/sbin/ntpdate 210.72.145.44 64.147.116.229 time.nist.gov > / dev/null 2 > & 1Summary-Succeeded: 1 (changed=1) Failed: 0Muir- -Total states run: 1 [root@server01 salt] #

# check the execution result

[root@server01 salt] # salt 'server02' cron.raw_cron rootserver02: # Lines below here are managed by Salt, do not edit [root@server01 salt] #

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report