Description
We have some older python scripts to get the ip address of the servers. Unfortunate in 3001.1, they broke due to that __utils__ was not defined. I saw an bug report about it, that I hoped would be fixed in 3002. But the issue remains. The old bug that is closed now is #57940
[CRITICAL] Failed to load grains defined in grain file internal_interfaces.internal_interfaces in function <function internal_interfaces at 0x7fd415632c80>, error:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/salt/loader.py", line 866, in grains
ret = funcs[key](**kwargs)
File "/var/cache/salt/minion/extmods/grains/internal_interfaces.py", line 10, in internal_interfaces
interfaces = salt.modules.network.interfaces()
File "/usr/lib/python3/dist-packages/salt/modules/network.py", line 1121, in interfaces
return __utils__["network.interfaces"]()
NameError: name '__utils__' is not defined
Setup
(Please provide relevant configs and/or SLS files (be sure to remove sensitive info).
Steps to Reproduce the behavior
(Include debug logs if possible and relevant)
Expected behavior
A clear and concise description of what you expected to happen.
Versions Report
salt --versions-report
Salt Version:
Salt: 3002
Dependency Versions:
cffi: Not Installed
cherrypy: Not Installed
dateutil: 2.7.3
docker-py: Not Installed
gitdb: 2.0.5
gitpython: 2.1.11
Jinja2: 2.10
libgit2: Not Installed
M2Crypto: Not Installed
Mako: Not Installed
msgpack-pure: Not Installed
msgpack-python: 0.5.6
mysql-python: Not Installed
pycparser: Not Installed
pycrypto: Not Installed
pycryptodome: 3.6.1
pygit2: Not Installed
Python: 3.7.3 (default, Jul 25 2020, 13:03:44)
python-gnupg: Not Installed
PyYAML: 3.13
PyZMQ: 17.1.2
smmap: 2.0.5
timelib: Not Installed
Tornado: 4.5.3
ZMQ: 4.3.1
System Versions:
dist: debian 10 buster
locale: UTF-8
machine: x86_64
release: 4.19.0-12-amd64
system: Linux
version: Debian GNU/Linux 10 buster
Additional context
Add any other context about the problem here.
Salt dunders (such as __utils__) is not available when using import salt.modules.network (it needs to go through Salt loader). Your best option would be to execute the salt module through __salt__, but since that dunder is filled only after grains are loaded, you can try use the utils method directly - https://github.com/saltstack/salt/blob/master/salt/utils/network.py#L1011
Thanks, will try to see if I can solve it. Unfortunate this has been handed over to me from an old colleauge. And I can't say I'm particular good at this :)
If you have an idea of how you would redo this script, which ours is based on. Then it would be really helpful. https://lemarchand.io/saltstack-and-internal-network-interfaces/. The important part is that we need to get the ipadresses associated with the specified subnet.
I've looked at the blog and modified to work just with salt.utils.network (it can work either with __utils__ dunder or just by importing salt.utils.network directly, but then you can't override it just as you can with normal modules)
def internal_interfaces():
grains = {}
addrs = __utils__["network.ip_addrs"]()
cidr = "10.0.0.0/8"
internalips = [i for i in addrs if __utils__["network.in_subnet"](cidr, [i])]
interfaces = __utils__["network.interfaces"]()
grains['internal_interfaces'] = []
for interface, data in interfaces.items():
for internalip in internalips:
if 'inet' in data and data['inet'][0]['address'] and data['inet'][0]['address'] == internalip:
grains['internal_interfaces'].append(interface)
return grains
root@sm01:/srv/salt/_grains# salt '*' grains.get internal_interfaces
sm01.lxd:
- eth0
root@sm01:/srv/salt/_grains# salt '*' grains.get saltversion
sm01.lxd:
3002.1
If you just want to get addresses in specific subnet, then its mostly just the __utils__["network.in_subnet"](cidr, [i])] call.
Thanks for the help! This solved the issue and I got the expected behaviour now.
I am also using a modified version of this custom grain for internal interfaces and I was stumped after changing to Python 3. This was very helpful!! Thanks @lukasraska ! I've included a link to this issue in an update to an article I wrote about this custom grain.