salt mine for all network interfaces

2019-08-26 01:36发布

问题:

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.