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

salt states examples

时间:2015-04-23 13:51:34      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:salt states

我们先来看下应用了jinja template之后,salt states是如何编译和运行

技术分享


使用state.show_sls可以查看具体的state文件

[root@node-194 ~]# salt ‘*‘ state.show_sls dev.openstack-keystone
node-191:
    ----------
    keystone-add-haproxy:
        ----------
        __env__:
            base
        __sls__:
            dev.openstack-keystone
        salt:
            |_
              ----------
              tgt:
                  node-191,node-192
            |_
              ----------
              tgt_type:
                  list
            |_
              ----------
              sls:
                  - dev.openstack.keystone.keystone-haproxy
            |_
              ----------
              require:
                  |_
                    ----------
                    salt:
                        keystone-service
                  |_
                    ----------
                    salt:
                        haproxy-service
            - state
            |_
              ----------
              order:
                  10004


jinja template注释

技术分享


通过grains区分不同操作系统

技术分享

下面那种写法的好处是:

Dictionaries are useful to effectively "namespace" a collection of variables. This is useful with parameterization (discussed below). Dictionaries are also easily combined and merged. And they can be directly serialized into YAML which is often easier than trying to create valid YAML through templating. 


下面看个通过template方式来创建yaml
{# ---- Bad example ---- #}

haproxy_conf:
  file.managed:
    - name: /etc/haproxy/haproxy.cfg
    - template: jinja
    {% if ‘external_loadbalancer‘ in grains.roles %}
    - source: salt://haproxy/external_haproxy.cfg
    {% elif ‘internal_loadbalancer‘ in grains.roles %}
    - source: salt://haproxy/internal_haproxy.cfg
    {% endif %}
    - context:
        {% if ‘external_loadbalancer‘ in grains.roles %}
        ssl_termination: True
        {% elif ‘internal_loadbalancer‘ in grains.roles %}
        ssl_termination: False
        {% endif %}

{# ---- Better example ---- #}

{% load_yaml as haproxy_defaults %}
common_settings:
  bind_port: 80

internal_loadbalancer:
  source: salt://haproxy/internal_haproxy.cfg
  settings:
    bind_port: 8080
    ssl_termination: False

external_loadbalancer:
  source: salt://haproxy/external_haproxy.cfg
  settings:
    ssl_termination: True
{% endload %}

{% if ‘external_loadbalancer‘ in grains.roles %}
{% set haproxy = haproxy_defaults[‘external_loadbalancer‘] %}
{% elif ‘internal_loadbalancer‘ in grains.roles %}
{% set haproxy = haproxy_defaults[‘internal_loadbalancer‘] %}
{% endif %}

{% do haproxy.settings.update(haproxy_defaults.common_settings) %}

haproxy_conf:
  file.managed:
    - name: /etc/haproxy/haproxy.cfg
    - template: jinja
    - source: {{ haproxy.source }}
    - context: {{ haproxy.settings | yaml() }}




本文出自 “the-way-to-cloud” 博客,请务必保留此出处http://iceyao.blog.51cto.com/9426658/1637463

salt states examples

标签:salt states

原文地址:http://iceyao.blog.51cto.com/9426658/1637463

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