所有网络接口的盐矿

salt mine for all network interfaces

我在通过 salt 使用 interface 插件配置 collectd.conf 时遇到困难。 collectd.conf 需要一个网络接口列表来监控,如:

<Plugin interface>
  Interface "em1"
  Interface "em2"
</Plugin>

我发现我需要使用 salt mine 将谷物拉入 master - 这是通过 pillar sls 实现的,如下所示:

mine_functions:
  network.interfaces: []

在我的 collectd.conf 中我有:

<Plugin interface>
{% for host, info in salt['mine.get']('*','network.interfaces').items() %}
 {% if host == grains['id'] %}
  {% for interface in info %}
  Interface "{{ interface }}"
  {% endfor %}
 {% endif %}
{% endif %}
{% endfor %}
</Plugin>

但是,它似乎对我不起作用:(

如果您正在寻找本地主机上的接口(我认为您是因为您正在过滤 grains['id'])那么您不需要我的接口。您可以从 grains 获取本地主机上的接口名称:

{%- for iface in grains['ip_interfaces'].keys() %}
Interface "{{ iface }}"
{%- endfor %}

如果您想要本地机器地址列表而不是接口名称,您可以循环遍历 grains['ipv4']。没有.keys(); grain 是一个简单的列表。

如果您需要 其他 主机的接口信息,那么您可以使用我的。请注意,network.interfaces returns 是一个嵌套字典,这可能就是它对您不起作用的原因——您正在循环它,就好像它是一个列表,(至少当我刚才测试它时)产生不可读的混乱。

要从其他服务器获取接口名称,您实际上会使用更像这样的东西:

{%- for minion, ifaces in salt['mine.get']('*', 'network.interfaces').items() %}
{{ minion }}:
{%-   for iface in ifaces.keys() %}
  - {{ iface }}
{%-   endfor %}
{%- endfor %}

应该输出:

minion1:
  - eth0
  - eth1
  - lo
minion2:
  - eth0
  - eth1
  - lo
...and so on.

这有点像你问的,但看起来不像你真正想做的。