标签: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
原文地址:http://iceyao.blog.51cto.com/9426658/1637463