标签:讲解 failed default .com 没有 博客 log comm htm
先转载下jinja模板中使用变量的方法,后文主要讲解pillar的变量使用方法
一、jinja模版的使用方法:
1、file状态使用template参数
- template:jinja
2、模版文件里面变量使用{{名称}},例如{{PORT}}
变量使用Grains:{{ grains[‘fqdn_ip4‘] }}
变量使用执行模块:{{ salt[‘network.hw_addr‘](‘eth0‘) }}
变量使用Pillar:{{ pillar[‘apache‘][‘PORT‘] }}
3、变量列表
- defaults: PORT:8080
pillar的变量使用方法
二、master环境情况:
[root@master ~]# grep -Ev "#|^$" /etc/salt/master
interface: 0.0.0.0
file_roots:
base:
- /srv/salt/base
prod:
- /srv/salt/prod
pillar_roots:
base:
- /srv/pillar/base
prod:
- /srv/pillar/prod
自定义pillar数据
[root@master ~]# cat /srv/pillar/base/top.sls
base:
‘*‘:
- t
[root@master ~]# cat /srv/pillar/base/t.sls
a1: a11
b1:
b2: b22
b3:
b33: b333
b333: b3333
c1:
- c11
- c111
检验pillar
[root@master ~]# salt host102 pillar.items
host102:
----------
a1:
a11
b1:
----------
b2:
b22
b3:
----------
b33:
b333
b333:
b3333
c1:
- c11
- c111
要同步的文件one
[root@master ~]# cat /srv/salt/base/init/one
a1:{{ a1 }}
b2:{{ b2 }}
b33:{{ b33 }}
b333:{{ b333 }}
c1: {{ c1 }}
c2: {{ c2 }}
获取方式:
1.{{ pillar[‘name‘] }}
[root@master ~]# cat /srv/salt/base/init/one.sls
/tmp/one:
file.managed:
- source: salt://init/one
- template: jinja
- defaults:
a1: {{ pillar[‘a1‘] }}
b2: {{ pillar[‘b1‘][‘b2‘] }}
b33: {{ pillar[‘b1‘][‘b3‘][‘b33‘] }}
b333: {{ pillar[‘b1‘][‘b3‘][‘b333‘] }}
c1: {{ pillar[‘c1‘][0] }}
c2: {{ pillar[‘c1‘][1] }}
2.{{ pillar.get() }} or {{ salt[‘pillar.get‘]() }}
官方说明
https://docs.saltstack.com/en/latest/topics/pillar/
个人实验及理解:{{ pillar.get() }}只适用于一级变量,{{ salt[‘pillar.get‘]() }}可以适用多级变量
It should be noted that within templating, the pillar variable is just a dictionary. This means that calling pillar.get() inside of a template will just use the default dictionary .get() function which does not include the extra : delimiter functionality. It must be called using the above syntax (salt[‘pillar.get‘](‘foo:bar:baz‘, ‘qux‘)) to get the salt function, instead of the default dictionary behavior.
例1:说明{{ pillar.get() }}只适用于一级变量
[root@master ~]# cat /srv/salt/base/init/one.sls
/tmp/one:
file.managed:
- source: salt://init/one
- template: jinja
- defaults:
a1: {{ pillar.get(‘a1‘) }}
# b2: {{pillar.get(‘b1:b2‘) }} 这种方式返回None,pillar.get()并不能使用多级变量
b2: {{ salt[‘pillar.get‘](‘b1:b2‘) }}
b33: {{ salt[‘pillar.get‘](‘b1:b3:b33‘) }}
b333: {{ salt[‘pillar.get‘](‘b1:b3:b333‘) }}
c1: {{ pillar.get(‘c1‘)[0] }}
c2: {{ salt[‘pillar.get‘](‘c1‘)[1] }}
[root@master ~]# salt host102 state.sls init.one test=true
host102:
----------
ID: /tmp/one
Function: file.managed
Result: None
Comment: The file /tmp/one is set to be changed
Started: 10:31:00.913548
Duration: 8.761 ms
Changes:
----------
diff:
---
+++
@@ -1,5 +1,5 @@
a1:a11
-b2:b22
+b2:None
b33:b333
b333:b3333
c1: c11
Summary
------------
Succeeded: 1 (unchanged=1, changed=1)
Failed: 0
------------
Total states run: 1
例2:顺便看下默认值的问题
[root@master ~]# cat /srv/salt/base/init/one.sls
/tmp/one:
file.managed:
- source: salt://init/one
- template: jinja
- defaults:
a1: {{ pillar.get(‘a11‘,{}) }}
b2: {{ salt[‘pillar.get‘](‘b1:b22‘,‘‘) }}
b33: {{ salt[‘pillar.get‘](‘b1:b3:b33‘) }}
b333: {{ salt[‘pillar.get‘](‘b1:b3:b333‘) }}
c1: {{ pillar.get(‘c1‘)[0] }}
c2: {{ salt[‘pillar.get‘](‘c1‘)[1] }}
[root@master ~]# salt host102 state.sls init.one test=true
host102:
----------
ID: /tmp/one
Function: file.managed
Result: None
Comment: The file /tmp/one is set to be changed
Started: 10:34:22.505086
Duration: 9.101 ms
Changes:
----------
diff:
---
+++
@@ -1,5 +1,5 @@
-a1:a11
-b2:b22
+a1:{}
+b2:None
b33:b333
b333:b3333
c1: c11
Summary
------------
Succeeded: 1 (unchanged=1, changed=1)
Failed: 0
------------
Total states run: 1
{{ pillar.get(‘name‘,‘‘) }}不存在name键时,值为None
{{ pillar.get(‘name‘,{}) }}不存在name键时,值为{}
{{ salt[‘pillar.get‘](‘name‘) }}同理,网上看到一篇博客说‘‘与{}是一样的。
官方找到的例子
https://docs.saltstack.com/en/latest/topics/tutorials/pillar.html
{% for user, uid in pillar.get(‘users‘, {}).items() %}
可惜并没有‘‘的例子,倒是有{{ salt[‘pillar.get‘](‘pkgs:apache‘, ‘httpd‘) }}
例3:多个变量for循环用法,关于iteritems与items的区别,那是python的问题,请自行搜索,现在还不熟悉python。
{% for user, args in pillar[‘users‘].iteritems() %}
{% for user, args in pillar[‘users‘].items() %}
标签:讲解 failed default .com 没有 博客 log comm htm
原文地址:http://www.cnblogs.com/hjfeng1988/p/6958331.html