State

state是一个描述性文件,类似puppet的脚本,ansible的playbook,描述着需要配置/文件/软件/服务等的最终状态

格式

target_a:                         # ID declaration
  state_a:                        # State declaration
    - state_func_a: some_value    # function declaration
    - state_func_b: some_value    # ...
    - state_func_c: some_value
    - require:                    # requisite declaration
      - pkg: xxx                  # requisite reference
      - file: xxx/xxx.xx
  state_b:                        # Support multiple states

实战安装vim

创建/srv/salt/top.sls

# /srv/salt/top.sls
base:
  '*':
    - tool

创建/srv/salt/tool.sls

# /srv/salt/tool.sls
tool:
  pkg.installed:
   - name: vim-common

执行

➜  salt sudo salt 'AAAAAAAA-0000-0000-BBD1-FA294FBCB7D6' state.highstate 
AAAAAAAA-0000-0000-BBD1-FA294FBCB7D6:
----------
          ID: tool
    Function: pkg.installed
        Name: vim-common
      Result: True
     Comment: All specified packages are already installed
     Started: 09:02:43.874748
    Duration: 283.067 ms
     Changes:   

Summary for AAAAAAAA-0000-0000-BBD1-FA294FBCB7D6
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
Total run time: 283.067 ms

查看结果

➜  salt sudo salt 'AAAAAAAA-0000-0000-BBD1-FA294FBCB7D6' cmd.run 'rpm -qa | grep vim'
AAAAAAAA-0000-0000-BBD1-FA294FBCB7D6:
    vim-minimal-7.4.160-5.el7.x86_64
    vim-filesystem-7.4.629-6.el7.x86_64
    vim-common-7.4.629-6.el7.x86_64

或者在minion端执行

[root@minion2 ~]# salt-call state.highstate 

Jinjia2

实际在执行过程当中,不同的操作系统安装的包也不尽相同,需要在state文件里面加入相应的判断逻辑;

比如Fedora系统当中是 httpd ,但是在Debian/Ubuntu 却是 apache2

所以需要使用jinjia模板进行一些简单判断,如下:

# /srv/salt/webserver.sls
webserver:
  pkg.installed:
    - name: {% if grains['os_family'] =='RedHat' %} httpd {% elif grains['os_family']=='Arch' %} apache2 {% endif %}

也可以借助grainspillar里面的变量数据,因为本质上就是数据存储结构;

apache:
  pkg.installed:
    - name: {{ pillar['pkgs']['apache'] }}

实战部署httpd服务

# /srv/salt/webserver.sls
webserver:         
  pkg.installed:
    - name: {% if grains['os_family'] =='RedHat' %} httpd {% elif grains['os_family']=='Arch' %} apache {% endif %}
    
httpd.service:
  service.running:
    - require:
      - pkg: httpd
      
/var/www/index.html:
  file: 
    - managed
    - source: salt://webserver/index.html
    - require: 
      - pkg: httpd
  1. managed的source当中的全路径为: /srv/salt/webserver/index.html,使用了salt协议,所以需要去掉前缀路径

  2. 怎么知道哪个state下面有哪些function的参数

其他用法