Saltstack的配置管理salt.states

来自linux中国网wiki
Evan讨论 | 贡献2021年8月3日 (二) 13:12的版本 →‎see also
跳到导航 跳到搜索

最简单的sls state


vim /srv/salt/top.sls
#正则匹配
base:
  '*':
    - webserver
    
vim /srv/salt/webserver.sls
lynx:                 # 标签定义
  pkg:                  # state declaration
    - installed         # function declaration
    

第一行被称为(ID declaration) 标签定义,在这里被定义为安装包的名。注意:在不同发行版软件包命名不同,比如 fedora 中叫httpd的包 Debian/Ubuntu中叫apache2
第二行被称为(state declaration)状态定义, 在这里定义使用(pkg state module)
第三行被称为(function declaration)函数定义, 在这里定义使用(pkg state module)调用 installed 函数

    
 salt '*' state.highstate -v test=True
salt '*' state.highstate 
   

2.9  配置管理从这里开始  p27

1.查看支持的所有states 列表

 salt 'gitlabzbx' sys.list_state_modules
gitlabzbx:
    - alias
    - alternatives
    - apt
    - archive
    - artifactory
    - beacon
    - bigip


5. 从一个简单的实例理解states 


 cat /srv/salt/one.sls 
/tmp/foo.conf:
    file.managed:
        - source: salt://foo.conf
        - user: root
        - group: root
        - mode: 644
        - backup: minion


root@myxp:~# echo "salt books" > /srv/salt/foo.conf



这样就把 master上的 /src/salt/foo.conf 分发到 minon的 /tmp/foo.conf 

root@myxps:/srv/salt# salt '*' state.sls one 
zbxAgent:
----------
          ID: /tmp/foo.conf
    Function: file.managed
      Result: True
     Comment: File /tmp/foo.conf updated
     Started: 08:31:01.737749
    Duration: 45.764 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644

Summary for zbxAgent
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  45.764 ms
gitlabzbx:
----------
          ID: /tmp/foo.conf


salt系统初始化例子

https://github.com/evan886/saltops


下面介绍 使用top.sls 入口文件同时对多台服务器进行的配置管理

salt-master --log-level=debug

环境配置 配置file_roots and pillar_roots 正式例子

# Example:  注意  各空两格下去   级级下去  我搞了半天 呀  一开始
 file_roots:
   base:
#     - /srv/salt/base
#   dev:
#     - /srv/salt/dev/services
#     - /srv/salt/dev/states
   prod:
     - /srv/salt/prod/
#     - /srv/salt/prod/states


  pillar_roots:
    base:
      - /srv/pillar/base
#
    prod:
      - /srv/pillar/prod
    

 mkdir  -p /srv/salt/base /srv/salt/prod

 mkdir  -p /srv/pillar/base /srv/pillar/prod

/etc/init.d/salt-master restart

系统初始化

dns config

cat /srv/salt/base/init/dns.sls
/etc/resolv.conf:
     file.managed:
       - source: salt://init/files/resolv.conf
       - user: root
       - group: root
       - mode: 644


 cat  /srv/salt/base/init/files/resolv.conf 
# Generated by NetworkManager
nameserver 192.168.88.1
nameserver fd67:f843:530::1


history记录时间

 cat /srv/salt/base/init/history.sls 
  /etc/bashrc:
    file.append:
      - text:
        - HISTTIMEFORMAT="%F %T `whoamii` "
    cmd.run:
      - name: source /etc/bashrc

# or 
 cat /srv/salt/base/init/history.sls 
  /etc/profile:
    file.append:
      - text:
        - HISTTIMEFORMAT="%F %T `whoamii` "
    cmd.run:
      - name: source /etc/bashrc


===初始化环境引用  ===
<pre>
 cat /srv/salt/base/init/env_init.sls 
include:
    - init.dns
    - init.history


 tree /srv/salt/base/
/srv/salt/base/
├── init
│   ├── dns.sls
│   ├── dns.slsbak
│   ├── env_init.sls
│   ├── files
│   │   └── resolv.conf
│   └── history.sls
├── top.sls


 cat /srv/salt/base/top.sls
base:
  '*':
    - init.env_init

salt '*' state.highstate test=True
 salt '*' state.highstate 

站内资源

Saltstack文件管理

trouble shooting

trouble 
    Rendering SLS 'base:init.dns' failed: mapping values are not allowed here; line 2

 cat init/dns.sls 
/etc/resolv.conf: 少了这个的原因 ? 老报错  不有 第二行 要空4格  这书本 看起来是空了3格 
    file.managed:
        - source: salt://init/files/resolv.conf
	- user: root
	- group: root
	- mode: 644

参考一下别人发现的错 

see also

https://docs.saltproject.io/en/latest/ref/states/all/salt.states.file.html

Salt 系统初始化

saltstack 的日常使用(一)


saltstack 的日常使用 (三)