如何在jinja2的if语句中为saltstack比较嵌套的支柱键值 [英] How to compared a nested pillar key value in an if statement in jinja2 for saltstack
问题描述
我正在用一些盐包裹在jinja2中的盐堆状态.
I am working on a saltstack state with some salt wrapped in jinja2.
当我尝试使用jinja2比较支柱中的值时,参数似乎没有任何结果.
When I attempt to compare a value from a pillar using jinja2 it appears argument evaluates to nothing.
如果我使用salt cli查询值,它将返回期望值.
If I query the value using salt cli, it returns the expected value.
我希望我在jinja2的if语句中错误地引用了该值.
I expect I am referencing the value incorrectly in the if statement with jinja2.
这里是了解和查看此问题的所有必需信息:
Here is all the needed info to understand and look at this problem:
Salt Master ID是salt-dev
Salt Master id is salt-dev
Salt Minion ID在同一实例上,并且是salt-dev
Salt Minion id is on same instance and is salt-dev
这是最重要的文件:
base:
'salt-dev':
- docker-daemon.docker-daemon
这是位于/srv/pillar/docker-daemon/docker-daemon.sls
docker-daemon:
- action: start
- runlevel: enabled
这是salt cli命令的输出,返回小仆salt-dev的支柱内容:
Here is the output of the salt cli command returning the content of the pillar for the minion salt-dev:
# salt 'salt-dev' pillar.items
salt-dev:
----------
docker-daemon:
|_
----------
action:
start
|_
----------
runlevel:
enabled
这是我在if语句中使用的值的输出,其中该值在jinja2中不返回任何内容,但在此处使用cli返回了预期的结果:
Here is the output of the value I am using in the if statement where the value returns nothing with jinja2, but returns as expected here with cli:
# salt 'salt-dev' pillar.get docker-daemon:action
salt-dev:
start
jinja2的错误行是:
The line of jinja2 that is incorrect is:
{% if salt['pillar.get']('docker-daemon:action') == 'start' %}
它看起来:salt['pillar.get']('docker-daemon:action')
什么也不返回,但是从上面显示的cli确实返回了什么.
It appears: salt['pillar.get']('docker-daemon:action')
returns nothing, but from cli as shown above it does return something.
如果我添加一个默认值,则在此arg返回任何内容的情况下也将使用该默认值.
Also if I add a default value, which is used in the event this arg returned nothing it also works.
添加默认值的示例是:
{% if salt['pillar.get']('docker-daemon:action', 'def_value') == 'start' %}
我已经在下面的上下文中显示了它:
I have shown it in context below:
这是状态文件,其中if语句存在相同的问题:
Here is the state file where the if statements are having the same issue:
{% if ( (grains['osfinger'] == 'Oracle Linux Server-6') and (grains['osarch'] == 'x86_64')) %}
sync_docker-init:
file.managed:
- name: /etc/init.d/docker
- source: salt://docker-daemon/templates/docker-init
- user: root
- group: root
- mode: 755
action_docker-init:
{% if salt['pillar.get']('docker-daemon:action') == 'start' %}
service.running:
{% endif %}
{% if salt['pillar.get']('docker-daemon:action') == 'stop' %}
service.dead:
{% endif %}
- name: docker
- require:
- pkg: install_docker-engine
- watch:
- file: sync_docker-init
{% if salt['pillar.get']('docker-daemon:runlevel') == 'enabled' %}
-- enable: True
{% endif %}
{% if salt['pillar.get']('docker-daemon:runlevel') == 'disabled' %}
-- enable: False
{% endif %}
{% else %}
event.send:
- tag: 'salt/custom/docker-init/failure'
- data: "Management of docker init failed, OS not permitted."
{% endif %}
目前我对Salt和jinja2还是很陌生,所以这是101种东西,但是我会很感谢您的帮助,几个小时后我什么都没发现.
I am quite new at the moment to salt and jinja2, so this is 101 stuff, but I would appreciate some help, I have found nothing for some hours yet.
我试图回声一下,看来我只是空白行
I attempted to echo this out and it seemed I just get a blank line
推荐答案
我找到了解决方案.
支柱文件/srv/pillar/docker-daemon/docker-daemon.sls
形成为列表而不是地图.
The pillar file /srv/pillar/docker-daemon/docker-daemon.sls
was formed as a list instead of a map.
我将其更改为:
docker-daemon:
action: restart
runlevel: disabled
这篇关于如何在jinja2的if语句中为saltstack比较嵌套的支柱键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!