自动化运维之saltstackstates介绍及使用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自动化运维之saltstackstates介绍及使用相关的知识,希望对你有一定的参考价值。

一、什么是Salt States?

Salt States是Salt模块的扩展,主系统使用的状态系统叫SLS系统,SLS代表Saltstack State,Salt是一些状态文件,其中包含有关如何配置Salt子节点的信息,这些状态被存放在一个目录下,可以用很多不同的格式来书写,我们可以把这些Salt States当做是对minion的管理脚本的配置模式,通过配置SLS文件,指定目标minions,可以实现在master上对minions的运行状态进行管理。


二、Salt State树

跟系统文件一样,salt state文件也有自己的树系统,这通过配置文件中的file_roots配置,默认配置如下:

#在/etc/salt/master中配置下面选项,指定salt state的根目录在/srv/salt

[[email protected] salt]# grep -A3 file_roots: /etc/salt/master
# file_roots:
#   base:
#     - /srv/salt/
#   dev:
--
file_roots:
  base:
    - /srv/salt
[[email protected] salt]#


修改master配置文件后要重启,使得配置文件生效

[[email protected] salt]# systemctl restart salt-master.service



Salt State的top文件

top.sls文件是SaltState的默认sls文件,再未作特别指定的时候,salt会默认调用top.sls来执行。例如

salt ‘*‘ state.highstate

top.sls文件需要手动创建,位置则是在Salt State树定义的目录/srv/salt下:

创建一个top.sls文件

[[email protected] salt]# vim top.sls 
# 添加内容如下,注意sls文件是用两个空格来作为缩进
# base 匹配状态数的base目录,即/srv/salt
# ‘*‘ 表示匹配所有的minions,即所有认证通过的客户端
#  - webserver:表示执行base目录下webserver.sls状态文件或是webserver/init.sls文件
base:
  ‘*‘:
    - webserver

例如:创建一个Salt State文件

上面的top.sls文件中定义了一个webserver文件下,下面来添加一个webserver文件

在文件/srv/salt/webserver.sls文件中添加如下内容

# Apache:ID声明,跟yum install Apache指定的名称一样

#  pkg: 状态声明,说明下面的操作是什么状态操作

#    - installed:函数声明,说明该sls文件要指定执行操作的函数

[[email protected] salt]# vim webserver.sls
httpd:
  pkg:
    - installed
[[email protected] salt]#



执行我们定义的Salt State文件,更新所有的minion的状态

[[email protected] salt]# salt ‘*‘ state.highstate
server02:
----------
          ID: httpd
    Function: pkg.installed
      Result: True
     Comment: Package httpd is already installed.
     Started: 02:56:15.414698
    Duration: 1114.898 ms
     Changes:   
Summary
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
server03:
----------
          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: 1
Failed:    0
------------
Total states run:     1
[[email protected] salt]#

说明:根据上面top.sls以及webserver.sls的内容,所有的minion都会按照Apache服务,如果在top.sls中修改minion的匹配,则仅会匹配到的minion进行操作。当然也可以特别指定sls文件来执行,这里用sls文件为state.sls

[[email protected] salt]# salt ‘server02‘ state.sls webserver
server02:
----------
          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: 1
Failed:    0
------------
Total states run:     1
[[email protected] salt]#


#开启debug日志(minion端执行)

[[email protected] ~]# salt-minion -l debug

#设置默认超时(服务端执行)

[[email protected] salt]# salt ‘*‘ state.highstate -t 60


[[email protected] salt]# salt ‘*‘ state.highstate -t 60
server03:
----------
          ID: httpd
    Function: pkg.installed
      Result: True
     Comment: Package httpd is already installed.
     Started: 03:03:24.248749
    Duration: 900.103 ms
     Changes:   
Summary
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
server02:
----------
          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: 1
Failed:    0
------------
Total states run:     1
[[email protected] salt]#

三、实战操作

经过上面的简单介绍,下面来演示一下文件分发和软件安装

1、分发/etc/hosts文件

1)准备sls文件

[[email protected] salt]# pwd
/srv/salt
[[email protected] salt]# cat hosts.sls
/etc/hosts:
  file.managed:
    - source:  salt://testfiles/hosts
    - user:  root
    - group:  root
    - mode:  644
[[email protected] salt]#
[[email protected] salt]# cat  top.sls
base:
  ‘*‘:
    - webserver
    - hosts
[[email protected] salt]#
[[email protected] salt]# mkdir testfiles
[[email protected] salt]# cp /etc/hosts testfiles/
[[email protected] salt]# ll
total 12
-rw-r--r-- 1 root root 120 Jun 12 21:21 hosts.sls
drwxr-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
[[email protected] salt]# tree
.
├── hosts.sls
├── testfiles
│   └── hosts
├── top.sls
└── webserver.sls
1 directory, 4 files
[[email protected] salt]#


a、使用state.highstate调用

注意:使用state.highstate调用的前提是存在top.sls文件,因此需要提前写好top.sls文件。

[[email protected] salt]# salt ‘*‘ state.highstate
server03:
----------
          ID: httpd
    Function: pkg.installed
      Result: True
     Comment: Package httpd is already installed.
     Started: 03:11: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: 03:11:08.984360
    Duration: 5.308 ms
     Changes:   
Summary
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
server02:
----------
          ID: httpd
    Function: pkg.installed
      Result: True
     Comment: Package httpd is already installed.
     Started: 03:11:14.019185
    Duration: 905.721 ms
     Changes:   
----------
          ID: /etc/hosts
    Function: file.managed
      Result: True
     Comment: File /etc/hosts is in the correct state
     Started: 03:11:14.927716
    Duration: 4.522 ms
     Changes:   
Summary
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
[[email protected] salt]#


b、使用state.sls调用

[[email protected] salt]# salt ‘*‘ state.sls hosts
server03:
----------
          ID: /etc/hosts
    Function: file.managed
      Result: True
     Comment: File /etc/hosts is in the correct state
     Started: 03:12:05.888857
    Duration: 17.182 ms
     Changes:   
Summary
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
server02:
----------
          ID: /etc/hosts
    Function: file.managed
      Result: True
     Comment: File /etc/hosts is in the correct state
     Started: 03:12:05.903366
    Duration: 16.217 ms
     Changes:   
Summary
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
[[email protected] salt]#


2、安装软件包

1)准备sls文件

[[email protected] salt]# vim package.sls
pkg-install:
  pkg.installed:
    - names:
      - gcc
      - lrzsz
      - tree
      - dos2unix
[[email protected] salt]# 
[[email protected] salt]# vim top.sls
base:
  ‘server02‘:
    - package
[[email protected] salt]#

a、使用state.highstate调用

[[email protected] salt]# salt ‘server02‘ state.highstate
server02:
----------
          ID: pkg-install
    Function: pkg.installed
        Name: gcc
      Result: True
     Comment: Package gcc is already installed.
     Started: 03:16:42.645005
    Duration: 888.648 ms
     Changes:   
----------
          ID: pkg-install
    Function: pkg.installed
        Name: tree
      Result: True
     Comment: Package tree is already installed.
     Started: 03:16:43.533802
    Duration: 0.405 ms
     Changes:   
----------
          ID: pkg-install
    Function: pkg.installed
        Name: dos2unix
      Result: True
     Comment: The following packages were installed/updated: dos2unix
     Started: 03:16: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:19:52.435148
    Duration: 0.436 ms
     Changes:   
Summary
------------
Succeeded: 4 (changed=1)
Failed:    0
------------
Total states run:     4
[[email protected] salt]#


b、使用state.sls调用

[[email protected] salt]# salt ‘server02‘ state.sls package
server02:
----------
          ID: pkg-install
    Function: pkg.installed
        Name: gcc
      Result: True
     Comment: Package gcc is already installed.
     Started: 03:23:31.945630
    Duration: 849.86 ms
     Changes:   
----------
          ID: pkg-install
    Function: pkg.installed
        Name: tree
      Result: True
     Comment: Package tree is already installed.
     Started: 03:23:32.795666
    Duration: 0.58 ms
     Changes:   
----------
          ID: pkg-install
    Function: pkg.installed
        Name: dos2unix
      Result: True
     Comment: Package dos2unix is already installed.
     Started: 03:23:32.796341
    Duration: 0.328 ms
     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: 4
Failed:    0
------------
Total states run:     4
[[email protected] salt]#


其他日常使用方法

3、管理定时任务

1)准备sls文件(添加定时任务)

[[email protected] 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‘
[[email protected] salt]#


2)使用state.sls调用

[[email protected] salt]# salt ‘server02‘ state.sls cron
server02:
----------
          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: 1
Failed:    0
------------
Total states run:     1
[[email protected] salt]#


#检查执行结果

[[email protected] salt]# salt ‘server02‘ cron.raw_cron root
server02:
    # 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
[[email protected] salt]#


3)准备sls文件(删除定时任务)

[[email protected] 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‘
[[email protected] salt]# cat top.sls
base:
  ‘server02‘:
    - cron
[[email protected] salt]#

4)使用state.highstate调用

[[email protected] salt]# salt ‘server02‘ state.highstate
server02:
----------
          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: 03:45: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>&1
Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
[[email protected] salt]#

#检查执行结果

[[email protected] salt]# salt ‘server02‘ cron.raw_cron root
server02:
    # Lines below here are managed by Salt, do not edit
[[email protected] salt]#


本文出自 “平平淡淡才是真” 博客,请务必保留此出处http://ucode.blog.51cto.com/10837891/1935002

以上是关于自动化运维之saltstackstates介绍及使用的主要内容,如果未能解决你的问题,请参考以下文章

自动化运维之Ansible基础元素介绍

Ansible自动化运维之YAML介绍

自动化运维之puppet

python自动化运维之面向对象基础

Ansible自动化运维之playbook详解(含yaml介绍)及ansible的加密控制

自动化运维之cobbler安装多版本系统