在通过谷物过滤的盐模板中获取小兵IP [英] Get minions IP inside a salt template filtered by grains
问题描述
我在这里找到了这个答案,它告诉我如何在模板中获取其他奴才的IP,但我做不到获取host.fqdn
或host.ip
.使用host
时,我会得到完整的域名,而不是IP.当我在控制台上运行它时,它会获得ips,但是在模板内部,它确实会返回str object
I found this answer here that tells how to get the IP of another minions inside a template but i can't get host.fqdn
or host.ip
. When I use host
I get the full domain name and not the IP. When I ran it on console it gets the ips, but inside the template it does return a str object
Unable to manage file: Jinja variable 'str object' has no attribute 'fqdn'
我在这里尝试了这个,但是这个甚至没有显示结果.
I tried this one here but this one didn't even show a result.
我希望能够构建主机文件.每个小兵都有一些角色设置为谷物,例如:
I want to be able to build a hosts file. Each minion have some roles set as grains, like this:
roles:
- backend
- cdn
- ...
我希望能够获得所有roles:backend and not roles:cdn
.但是,当我将此行添加到publish.publish
时,出现此错误:
I want to be able to get all roles:backend and not roles:cdn
. But when I added this line to my publish.publish
I get this error:
Unable to manage file: Jinja error: 'NoneType' object is not iterable
我不知道自己在做什么错,我在master上添加了对等发布,但是它不起作用.我已经阅读了文档,但它们并没有帮助我发现问题.
I don't know what I am doing wrong, I added the peer publish on master but it doesn't work. I've read the docs and they don't help me find the problem.
编辑 版本报告
$ salt-minion --versions-report
Salt: 2015.5.3
Python: 2.7.6 (default, Mar 22 2014, 22:59:56)
Jinja2: 2.7.2
M2Crypto: 0.21.1
msgpack-python: 0.3.0
msgpack-pure: Not Installed
pycrypto: 2.6.1
libnacl: Not Installed
PyYAML: 3.10
ioflo: Not Installed
PyZMQ: 14.0.1
RAET: Not Installed
ZMQ: 4.0.4
Mako: Not Installed
Tornado: Not Installed
Debian source package: 2015.5.3+ds-1trusty1
编辑2个sls代码段
{% for host in salt['publish.publish']('roles:backend', 'network.ip_addrs', 'eth0', 'grain') %}
server {{ host.ip }}; # {{ host.fqdn }}
{% endfor %}
推荐答案
找到了解决方案:
在小兵配置中添加以下代码:
Inside a minion config add this piece of code:
mine_interval: 2
mine_functions:
network.ip_addrs:
- eth1 # or any other eth-n you might want
要匹配所有后端而不是负载均衡器,我们需要一个复合匹配器
To match all backends and not the load_balancers we need a compound matcher
G@roles:backend and not G@roles:load_balancer
之后,只需使用salt.mine
{%- for server, addrs in salt['mine.get']('G@roles:backend and not G@roles:load_balancer', 'network.ip_addrs', 'compound').items() %}
# {{ server }}
server {{ addrs|last }};
{%- endfor %}
这篇关于在通过谷物过滤的盐模板中获取小兵IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!