Created
September 21, 2017 20:57
-
-
Save jsumners/6caa671d5d77ef7a653708060b00c6c2 to your computer and use it in GitHub Desktop.
Deal with NICs in Ansible without knowing what udev/systemd will call them
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Scenario: | |
You are building a load balancer wherein you want to bond the interfaces that will be used to serve the traffic. | |
You're managing this via Ansible. You don't want to have to boot to an OS to see what udev/system will name the | |
NICs you intend to bond, because that is just stupid. So you decide to work with the MAC addresses. Oops, | |
the `ansible_facts` don't make that easy. | |
Solution: | |
The convoluted trickery published here. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
# put this in your playbook's "library" directory | |
import os | |
import json | |
def file_get_contents(filename): | |
with file(filename) as f: | |
s = f.read() | |
return s | |
ifaceNames = os.listdir('/sys/class/net/') | |
result = dict() | |
for iface in ifaceNames: | |
mac = file_get_contents('/sys/class/net/' + iface + '/address').strip() | |
result[mac] = iface | |
print json.dumps(result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- name: add bond interface | |
template: | |
src: templates/bond-iface.cfg | |
dest: "/etc/sysconfig/network-scripts/ifcfg-{{bond_iface}}" | |
notify: | |
- restart network | |
- name: map macs to iface names | |
macfaces: | |
nothing: 'no input needed' | |
register: ifaces | |
- name: merge iface data | |
set_fact: | |
iface: "{{ dict(device=ifaces[item.mac_address]) | combine(item) }}" | |
register: merged_iface_data | |
with_items: "{{slave_nics}}" | |
- name: add slave interfaces | |
template: | |
src: templates/slave-iface.cfg | |
dest: "/etc/sysconfig/network-scripts/ifcfg-{{iface.device}}" | |
with_items: "{{merged_iface_data.results}}" | |
vars: | |
iface: "{{item.ansible_facts.iface}}" | |
device: "{{iface.device}}" | |
uuid: "{{iface.uuid}}" | |
mac_address: "{{iface.mac_address}}" | |
master: "{{iface.master}}" | |
notify: | |
- restart network |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# These rules are used to configure the bond interface that is used | |
# to handle all traffic on the load balancer. | |
bond_iface: bond0 | |
bond_iface_mac: b3:3b:b8:f3:47:zz | |
bond_ip_address: 172.25.50.zz | |
bond_netmasK: 255.255.255.0 | |
bond_gateway: 172.25.50.1 | |
bond_dns1: 168.28.240.81 | |
bond_dns2: 168.28.240.82 | |
bond_opts: 'mode=balance-alb miimon=100' | |
# These variables define all of the interfaces that will be used by the | |
# bond interface to let it do its thing. Notice that we are not listing the | |
# interface names. We are matching based on MAC since there's no telling | |
# what the names could be. | |
slave_nics: | |
- mac_address: 00:00:00:aa:bb:cc | |
uuid: 1A87ED79-0D4A-4888-96CE-3879B8BFF1zz | |
master: "{{bond_iface}}" | |
- mac_address: 00:00:00:aa:bb:dd | |
uuid: B5E8399D-93D3-4915-9847-E83927260Czz | |
master: "{{bond_iface}}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment