关闭

SaltStack实战之配置管理-YAML编写技巧

标签: yamlsaltstack
123人阅读 评论(0) 收藏 举报
分类:

SaltStack实战之配置管理-YAML编写技巧

@(学习)[SaltStack]

1. 概述和内容

salt包含一个健壮且灵活的配置管理框架,它构建在远程执行核心上。这个框架运行于minion上,它通过特定呈现语言的state文件,即可轻松地同时配置成千上万的主机。

后面配置管理所介绍的内容有:
* file_roots
设置状态文件的位置

  • env
    Base环境
    开发、测试、预生产环境、生产环境

  • SLS
    YAML
    Jinja
    编写技巧

  • state模块
    file
    pkg
    service
    cmd

  • state关系
    require
    require_in
    watch
    watch_in
    unless
    onlyif

  • 实践案例
    LAMP
    LNMP
    Zabbix
    Haproxy+keepalived

  • 项目实战
    OpenStack自动化部署

2. YAML编写技巧

2.1 缩进

  • YAML使用一个固定的缩进风格表示数据层结构关系。salt需要每个缩进级别使用2个空格。
  • 不要使用table键。

2.2 冒号

冒号2边表示key和value。

my_key: my_value
    second_key: second_value

2.3 短横线

想要表示列表项,使用一个短横杠加一个空格。多个项使用同样的缩进级别作为同一列表的一部分。

my_dictionary:
  - list_value_one
  - list_value_two
  - list_value_three

3. 同步文件实战

下面介绍同步Linux下dns配置文件/etc/resolv.conf

vim /etc/salt/master

state_top: top.sls

file_roots:
  base:
    - /srv/salt
  dev:
    - /srv/salt/dev
  test:
    - /srv/salt/test
  prod:
    - /srv/salt/prod
[root@salt-master112 salt]# mkdir dev test prod
[root@salt-master112 salt]# cat top.sls 
yaml
base:
  '*':
    - init.dns
[root@salt-master112 salt]# cat init/dns.sls 
/etc/resolv.conf:
  file.managed:
    - source:
      - salt://init/files/resolv.conf
    - user: root
    - group: root
    - mode: 644
[root@salt-master112 salt]# cat init/files/resolv.conf 
nameserver 10.1.0.2
nameserver 114.114.114.114

执行文件同步

[root@salt-master112 salt]# salt '*' state.highstate
node1.test.com:
----------
          ID: /etc/resolv.conf
    Function: file.managed
      Result: True
     Comment: File /etc/resolv.conf updated
     Started: 17:50:20.016276
    Duration: 39.221 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -1,3 +1,2 @@
                  -# Generated by NetworkManager
                   nameserver 10.1.0.2
                   nameserver 114.114.114.114

Summary for node1.test.com
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  39.221 ms
master.test.com:
----------
          ID: /etc/resolv.conf
    Function: file.managed
      Result: True
     Comment: File /etc/resolv.conf updated
     Started: 01:47:36.747919
    Duration: 32.041 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -1,3 +1,2 @@
                  -# Generated by NetworkManager
                   nameserver 10.1.0.2
                   nameserver 114.114.114.114

Summary for master.test.com
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  32.041 ms
node2.test.com:
----------
          ID: /etc/resolv.conf
    Function: file.managed
      Result: True
     Comment: File /etc/resolv.conf updated
     Started: 17:50:20.642481
    Duration: 58.623 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -1,3 +1,2 @@
                  -# Generated by NetworkManager
                   nameserver 10.1.0.2
                   nameserver 114.114.114.114

Summary for node2.test.com
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  58.623 ms
[root@salt-master112 salt]# .test.com:
----------
          ID: /etc/resolv.conf
    Function: file.managed
      Result: True
     Comment: File /etc/resolv.conf updated
     Started: 01:40:04.210096
    Duration: 29.966 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -1,2 +1,3 @@
                   # Generated by NetworkManager
                   nameserver 10.1.0.2
                  +nameserver 114.114.114.114

Summary for master.test.com
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  29.966 ms
node1.test.com:
----------
          ID: /etc/resolv.conf
    Function: file.managed
      Result: True
     Comment: File /etc/resolv.conf updated
     Started: 17:42:47.647214
    Duration: 50.04 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -1,2 +1,3 @@
                  -
                  +# Generated by NetworkManager
                   nameserver 10.1.0.2
                  +nameserver 114.114.114.114

Summary for node1.test.com
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  50.040 ms
node2.test.com:
----------
          ID: /etc/resolv.conf
    Function: file.managed
      Result: True
     Comment: File /etc/resolv.conf updated
     Started: 17:42:48.218295
    Duration: 70.125 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -1,2 +1,3 @@
                  -
                  +# Generated by NetworkManager
                   nameserver 10.1.0.2
                  +nameserver 114.114.114.114

Summary for node2.test.com
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  70.125 ms

在minion端查看下/etc/resolv.conf

[root@im109 ~]# cat /etc/resolv.conf
nameserver 10.1.0.2
nameserver 114.114.114.114
[root@im109 ~]# 

发现文件已更新。

0
0

猜你在找
【直播】机器学习&深度学习系统实战(唐宇迪)
【直播】Kaggle 神器:XGBoost 从基础到实战(冒教授)
【直播回放】深度学习基础与TensorFlow实践(王琛)
【直播】计算机视觉原理及实战(屈教授)
【直播】机器学习之凸优化(马博士)
【直播】机器学习之矩阵(黄博士)
【直播】机器学习之概率与统计推断(冒教授)
【直播】机器学习之数学基础
【直播】TensorFlow实战进阶(智亮)
【直播】深度学习30天系统实训(唐宇迪)
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:17570次
    • 积分:514
    • 等级:
    • 排名:千里之外
    • 原创:75篇
    • 转载:3篇
    • 译文:0篇
    • 评论:2条