码迷,mamicode.com
首页 > 其他好文 > 详细

SaltStack配置管理和YAML

时间:2019-04-13 22:07:43      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:group   false   pwd   组件   程序   dom   bsp   cgroup   dir   

配置管理和YAML


配置管理

所谓的配置管理,也称为状态管理,就是可以通过编写文件,文件的内容为安装什么功能、开启什么服务,执行什么任务等信息,然后通过salt的配置管理,指定minion来执行这些操作。其中定义这些状态配置,所用到的文件都是一种.sls格式的文件,文件编写是通过YAML来进行的,salt使用了YAML的子集,包含使用最普遍的数据结构(列表、字典)。

在YAML的编写过程中,要牢记三条规则

  • rule one,缩进,indentation

    在数据和数据之间,通过缩进,来表示他们的层次关系,每一个层次之间,使用两个空格的缩进,表示上下层关系,不要使用tab

  • rule two,冒号,colon

    在YAML和python的字典中,都是一种key-value的表现形式,通过冒号,存储键值对,在冒号前的是key,在冒号后的是value,但是在value前,冒号后,是有一个空格的。格式是这样的:

    my_key: my_value
    

    这就是在python中的字典形式,{‘mykey‘:‘myvalue‘},只不过没有了大括号了。通过缩进的形式编写这个值就是这样的:

    my_key:
      my_value
    

    字典是可以嵌套的,也就是在括号中嵌套括号,通过YAML的编写,利用缩进表示层次关系,下面就是个例子:

    first_level_key:
      second_level_key: value_in_second_level
    
  • rule three,短横线,破折号,dash

    使用短横线,来表示一个列表,在短横线后面,跟着一个空格,在一个列表中,可以有多个相同的值,都是通过短横线来隔开,他们使用的缩进是同样的。

    list:
      \- first_value
      \- second_value
      \- third_value
    

    这实际上是一个字典,字典的value值是一个列表,列表里面有三个值。

配置管理示例

下面将通过使用salt的配置管理,在一个minion上安装apache应用,然后启动。

1. 配置sls文件路径

在master的配置文件中,有设置默认的sls路径,就是file_root,默认为是/srv/salt下,可以自定义修改,也可以保持不变。

# file_roots:
#   base:
#     - /srv/salt/
#   dev:
#     - /srv/salt/dev/services
#     - /srv/salt/dev/states
#   prod:
#     - /srv/salt/prod/services
#     - /srv/salt/prod/states
#
#file_roots:
#  base:
#    - /srv/salt

2. 编写sls文件

[root@master ~]# cd /srv/salt/web/
[root@master web]# ls
apache.sls
[root@master web]# pwd
/srv/salt/web
[root@master web]# cat apache.sls 
apache-install:
  pkg.installed:
    - names:
      - httpd
      - httpd-devel

apache-service:
  service.running:
    - name: httpd
[root@master web]# 

在上面的这个apacle.sls文件中,apacle是该文件的名称,在该文件描述的状态中,定义了两个过程,一个叫apache-install,一个叫apache-service,然后在每个过程里面,都定义了该过程要做的事情,比如pkg.installed就是表示调用pkg模块中的installed方法,安装软件,而软件的名称就是names定义的列表,其实每一个过程,都是一个key-value的字典,在字典中嵌套了字典和列表。

3. 在指定的minion上执行该状态

选择在192.168.64.132上运行该状态,该主机上没有安装过httpd,看看执行完该状态之后,有没有启动httod服务。

由于调用的是yum安装,所以需要在minion配置正确的repo

[root@master salt]# salt ‘192.168.64.132‘ state.sls web.apache
[root@master salt]# salt ‘192.168.64.132‘ state.sls web.apache
192.168.64.132:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: The following packages were installed/updated: httpd
     Started: 20:02:26.129316
    Duration: 5958.326 ms
     Changes:   
              ----------
              httpd:
                  ----------
                  new:
                      2.4.6-67.el7.centos
                  old:
              httpd-tools:
                  ----------
                  new:
                      2.4.6-67.el7.centos
                  old:
              mailcap:
                  ----------
                  new:
                      2.1.41-2.el7
                  old:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd-devel
      Result: True
     Comment: The following packages were installed/updated: httpd-devel
     Started: 20:02:32.162734
    Duration: 4749.879 ms
     Changes:   
              ----------
              apr-devel:
                  ----------
                  new:
                      1.4.8-3.el7
                  old:
              apr-util-devel:
                  ----------
                  new:
                      1.5.2-6.el7
                  old:
              cyrus-sasl:
                  ----------
                  new:
                      2.1.26-21.el7
                  old:
              cyrus-sasl-devel:
                  ----------
                  new:
                      2.1.26-21.el7
                  old:
              expat-devel:
                  ----------
                  new:
                      2.1.0-10.el7_3
                  old:
              httpd-devel:
                  ----------
                  new:
                      2.4.6-67.el7.centos
                  old:
              libdb-devel:
                  ----------
                  new:
                      5.3.21-20.el7
                  old:
              openldap-devel:
                  ----------
                  new:
                      2.4.44-5.el7
                  old:
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Started Service httpd
     Started: 20:02:38.439435
    Duration: 171.827 ms
     Changes:   
              ----------
              httpd:
                  True

Summary for 192.168.64.132
------------
Succeeded: 3 (changed=3)
Failed:    0
------------
Total states run:     3
Total run time:  10.880 s

可以在132上去观察做的操作

[root@localhost ~]# ps -aux|grep yum
root       1770  0.1  0.6 325316 23880 ?        Sl   19:49   0:00 /usr/bin/python /usr/bin/yum --quiet --assumeyes check-update --setopt=autocheck_running_kernel=false
[root@localhost ~]# ps -aux|grep yum
root       1837  3.0  0.6 320448 23348 ?        Sl   19:50   0:00 /usr/bin/python /usr/bin/yum -y install httpd

安装成功之后,可以通过salt命令查看状态

[root@master salt]# salt ‘192.168.64.132‘ cmd.run ‘systemctl status httpd‘
192.168.64.132:
    * httpd.service - The Apache HTTP Server
       Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
       Active: active (running) since Sat 2019-04-13 20:02:38 CST; 2min 56s ago
         Docs: man:httpd(8)
               man:apachectl(8)
     Main PID: 2144 (httpd)
       Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
       CGroup: /system.slice/httpd.service
               |-2144 /usr/sbin/httpd -DFOREGROUND
               |-2145 /usr/sbin/httpd -DFOREGROUND
               |-2146 /usr/sbin/httpd -DFOREGROUND
               |-2147 /usr/sbin/httpd -DFOREGROUND
               |-2148 /usr/sbin/httpd -DFOREGROUND
               `-2149 /usr/sbin/httpd -DFOREGROUND

    Apr 13 20:02:38 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...
    Apr 13 20:02:38 localhost.localdomain httpd[2144]: AH00558: httpd: Could not reliably determine the server‘s fully qualified domain name, using localhost.localdomain. Set the ‘ServerName‘ directive globally to suppress this message
    Apr 13 20:02:38 localhost.localdomain systemd[1]: Started The Apache HTTP Server.

从salt安装的日志中可以看到,之前文件中定义了两个过程,apache-install和apache-service,其中apache-install中定义了安装两个软件包,然后整个过程就分成了三个阶段,安装httpd、安装httpd-devel,和启动httpd服务。最后返回的结果是安装成功,并成功启动,3个任务全部完成。

在minion中,可以看到,之前在master 上定义的状态文件,已经传递到minion上面来了。路径在/var/cache/salt/minion/file/base/web/apache.sls

[root@localhost yum.repos.d]# cd /var/cache/salt/minion/files/base/web/
[root@localhost web]# ls
apache.sls
[root@localhost web]# pwd
/var/cache/salt/minion/files/base/web
[root@localhost web]# cd ..
[root@localhost base]# cd ..
[root@localhost files]# cd ..
[root@localhost minion]# tree
.
├── accumulator
├── extmods
├── files
│   └── base
│       └── web
│           └── apache.sls
├── highstate.cache.p
├── proc
└── sls.p

6 directories, 3 files
[root@localhost minion]# 

以上就是一个简单的通过salt进行程序自动化安装的例子。当在进行批量部署的时候,可以通过编写sls的方式,远程执行,同时安装这些组件。前提条件是使用yum安装,需要有安装源。

顶层设计

在之前的状态管理中,由于是指定minion,执行指定的sls程序,一次只能执行一个,但是并不是所有的minion都安装相同的软件,可能存在不同的minion安装不同的软件和程序,要想一次性的让这些操作同时进行,就需要用到顶层设计,也就是topfile。

高级状态,也就是顶层设计,在该文件中,指定了哪些主机安装哪些软件。

1. 顶层文件位置

顶层文件的位置,定义在master 的配置文件中,top file

# The state system uses a "top" file to tell the minions what environment to
# use and what modules to use. The state_top file is defined relative to the
# root of the base environment as defined in "File Server settings" below.
state_top: top.sls

# If top_file_merging_strategy is set to ‘same‘ and an environment does not
# contain a top file, the top file in the environment specified by default_top
# will be used instead.
default_top: base

这段话的意思,就是在base目录下,存在的文件,是顶层文件,所以建议在编写sls的时候,尽量不要放在base目录下,放在对应的module中,通过module.function的方式进行引用。

2. 编写顶层文件

[root@master salt]# cat top.sls
base:
  ‘192.168.64.132‘:
    - web.apache

在这个顶层文件中,模式是这样的:

  • base是定义了环境,因为在sls中可以有多个环境,都是可以自定义的,代表了一个目录位置,要执行的sls状态文件,就在该路径下,
  • 第二层是minion-id,指定的minion,哪些minion用哪些sls
  • 第三层是状态文件

这三层联系起来,意思就是说,哪些minion,使用那个环境目录下的哪些状态文件。192.168.64.132使用base目录下的web.apache文件执行状态,和之前的salt ‘192.168.64.132‘ state.sls web.apache效果是一样的。

使用该顶层文件的方法如下:

[root@master salt]# salt ‘192.168.64.132‘ state.highstate
192.168.64.132:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: The following packages were installed/updated: httpd
     Started: 21:07:03.275564
    Duration: 3810.474 ms
     Changes:   
              ----------
              httpd:
                  ----------
                  new:
                      2.4.6-67.el7.centos
                  old:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd-devel
      Result: True
     Comment: The following packages were installed/updated: httpd-devel
     Started: 21:07:07.160575
    Duration: 3843.961 ms
     Changes:   
              ----------
              httpd-devel:
                  ----------
                  new:
                      2.4.6-67.el7.centos
                  old:
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Started Service httpd
     Started: 21:07:12.525982
    Duration: 122.376 ms
     Changes:   
              ----------
              httpd:
                  True

Summary for 192.168.64.132
------------
Succeeded: 3 (changed=3)
Failed:    0
------------
Total states run:     3
Total run time:   7.777 s

如果只是想测试该脚本的功能,可以添加参数tes=True,不会在minion上安装执行

[root@master salt]# salt ‘192.168.64.132‘ state.highstate test=True
192.168.64.132:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: None
     Comment: The following packages would be installed/updated: httpd
     Started: 21:12:59.997757
    Duration: 976.399 ms
     Changes:   
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd-devel
      Result: None
     Comment: The following packages would be installed/updated: httpd-devel
     Started: 21:13:00.974517
    Duration: 39.96 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: None
     Comment: Service httpd not present; if created in this state run, it would have been started
     Started: 21:13:01.015587
    Duration: 21.074 ms
     Changes:   

Summary for 192.168.64.132
------------
Succeeded: 3 (unchanged=3)
Failed:    0
------------
Total states run:     3
Total run time:   1.037 s

在minion上,可以看到在base环境下,有一个顶层文件top.sls。

[root@localhost minion]# tree
.
├── accumulator
├── extmods
├── files
│   └── base
│       ├── top.sls
│       └── web
│           └── apache.sls
├── highstate.cache.p
├── module_refresh
├── pkg_refresh
├── proc
└── sls.p

6 directories, 6 files

SaltStack配置管理和YAML

标签:group   false   pwd   组件   程序   dom   bsp   cgroup   dir   

原文地址:https://www.cnblogs.com/bobo137950263/p/10702959.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!