我正在尝试编写自定义谷物。
我把这段代码放在/etc/salt/grains
#!/usr/bin/env python
def function():
grains = {}
grains['test'] = "test"
return grains
并测试它我创建了这个脚本:
import salt.config
import salt.loader
__opts__ = salt.config.minion_config('/etc/salt/minion')
__grains__ = salt.loader.grains(__opts__)
test = __grains__['test']
print test
我有这个错误:
dirs = __grains__['test']
KeyError: 'test'
这有什么问题?
答案 0 :(得分:9)
/etc/salt/grains
是盐储存由salt targetid grains.setval
和朋友设定的谷物的地方。它是一个扁平的静态yaml文件。
通过python的自定义谷物进入/ srv / salt / _grains,然后与salt \* saltutil.sync_grains
同步到仆从。这是您应该放置python脚本的地方。
以下示例与您的代码进行了类似的记录和验证:
$ cat /srv/salt/_grains/spam.py
#!/usr/bin/env python
import logging
log = logging.getLogger(__name__)
def function():
log.trace('Setting grains["spam"] to "eggs"')
grains = {}
grains['spam'] = "eggs"
return grains
$ sudo salt lead saltutil.sync_grains
lead:
- grains.spam
$ sudo salt-call grains.item spam -l trace 2>&1 \
| egrep '^local|spam'
[TRACE ] Added spam.function to grain
[TRACE ] Setting grains["spam"] to "eggs"
local:
spam: eggs
$ cat /tmp/spam_taster.py
#!/usr/bin/env python
import salt.config
import salt.loader
__opts__ = salt.config.minion_config('/etc/salt/minion')
__grains__ = salt.loader.grains(__opts__)
spam = __grains__['spam']
print spam
$ python /tmp/spam_taster.py
eggs