salt mine for all network interfaces

北城以北 提交于 2019-12-12 01:22:43

问题


I'm having difficulty configuring collectd.conf using the interface plugin through salt. The collectd.conf expects a list of network interfaces to monitor like:

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

I've worked out that I need to use a salt mine to pull the grains into the master - which is achieved through a pillar sls like the following:

mine_functions:
  network.interfaces: []

and in my collectd.conf I have:

<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>

However, it doesn't appear to work for me :(


回答1:


If you're looking for interfaces on the local host (which I gather you are since you're filtering on grains['id']) then you don't need mine for this. You can get the interface names on the local host from grains:

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

If you want a list of the local machine's addresses instead of interface names, you can loop over grains['ipv4'] instead. No .keys(); that grain is a simple list.

If you need interface information for other hosts, then you use mine. Note that network.interfaces returns a nested dictionary, which may be why it's not working for you -- you are looping over it as if it were a list, which (at least when I tested it just now) produces an unreadable mess.

To get interface names from other servers, you would actually use something more like:

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

Which should output:

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

This is sort of what you asked but doesn't seem like what you're actually trying to do.



来源:https://stackoverflow.com/questions/30931154/salt-mine-for-all-network-interfaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!