2018-04-11 18:47:08 +02:00
|
|
|
From 0449bead92ff763d186f5e524556f82c618d652c Mon Sep 17 00:00:00 2001
|
2018-01-26 14:46:21 +01:00
|
|
|
From: Michele Bologna <michele.bologna@suse.com>
|
|
|
|
Date: Thu, 14 Dec 2017 18:20:02 +0100
|
|
|
|
Subject: [PATCH] Feat: add grain for all FQDNs
|
|
|
|
|
|
|
|
This PR adds a grain named fqdns to the grains.
|
|
|
|
fqdns represents all the FQDNs known for the system on all available interfaces (excluding lo).
|
|
|
|
|
|
|
|
Note: hostname != FQDN
|
|
|
|
|
|
|
|
hostname is the UNIX name of the machine. A machine can have one and only one hostname.
|
|
|
|
FQDN is host.domain that resolves to an IP address that the machines is answering to.
|
|
|
|
A machine can have 1+ FQDNs.
|
|
|
|
|
|
|
|
Upstream PR:
|
|
|
|
https://github.com/saltstack/salt/pull/45060
|
|
|
|
---
|
|
|
|
salt/grains/core.py | 27 +++++++++++++++++++++++++++
|
|
|
|
tests/integration/modules/test_grains.py | 1 +
|
2018-02-28 16:27:22 +01:00
|
|
|
tests/unit/grains/test_core.py | 1 +
|
|
|
|
3 files changed, 29 insertions(+)
|
2018-01-26 14:46:21 +01:00
|
|
|
|
|
|
|
diff --git a/salt/grains/core.py b/salt/grains/core.py
|
2018-04-11 18:47:08 +02:00
|
|
|
index b7d446676e..96b7ce2cf2 100644
|
2018-01-26 14:46:21 +01:00
|
|
|
--- a/salt/grains/core.py
|
|
|
|
+++ b/salt/grains/core.py
|
2018-04-11 18:47:08 +02:00
|
|
|
@@ -1888,6 +1888,33 @@ def append_domain():
|
2018-01-26 14:46:21 +01:00
|
|
|
return grain
|
|
|
|
|
|
|
|
|
|
|
|
+def fqdns():
|
|
|
|
+ '''
|
|
|
|
+ Return all known FQDNs for the system by enumerating all interfaces and
|
|
|
|
+ then trying to reverse resolve them (excluding 'lo' interface).
|
|
|
|
+ '''
|
|
|
|
+ # Provides:
|
|
|
|
+ # fqdns
|
|
|
|
+
|
|
|
|
+ grains = {}
|
|
|
|
+ fqdns = set()
|
|
|
|
+
|
|
|
|
+ addresses = salt.utils.network.ip_addrs(include_loopback=False,
|
|
|
|
+ interface_data=_INTERFACES)
|
|
|
|
+ addresses.extend(salt.utils.network.ip_addrs6(include_loopback=False,
|
|
|
|
+ interface_data=_INTERFACES))
|
|
|
|
+
|
|
|
|
+ for ip in addresses:
|
|
|
|
+ try:
|
|
|
|
+ fqdns.add(socket.gethostbyaddr(ip)[0])
|
|
|
|
+ except (socket.error, socket.herror,
|
|
|
|
+ socket.gaierror, socket.timeout) as e:
|
|
|
|
+ log.error("Exception during resolving address: " + str(e))
|
|
|
|
+
|
|
|
|
+ grains['fqdns'] = list(fqdns)
|
|
|
|
+ return grains
|
|
|
|
+
|
|
|
|
+
|
|
|
|
def ip_fqdn():
|
|
|
|
'''
|
|
|
|
Return ip address and FQDN grains
|
|
|
|
diff --git a/tests/integration/modules/test_grains.py b/tests/integration/modules/test_grains.py
|
2018-02-28 16:27:22 +01:00
|
|
|
index 709f882b45..aa7bd44202 100644
|
2018-01-26 14:46:21 +01:00
|
|
|
--- a/tests/integration/modules/test_grains.py
|
|
|
|
+++ b/tests/integration/modules/test_grains.py
|
|
|
|
@@ -51,6 +51,7 @@ class TestModulesGrains(ModuleCase):
|
|
|
|
'cpuarch',
|
|
|
|
'domain',
|
|
|
|
'fqdn',
|
|
|
|
+ 'fqdns',
|
|
|
|
'gid',
|
|
|
|
'groupname',
|
|
|
|
'host',
|
|
|
|
diff --git a/tests/unit/grains/test_core.py b/tests/unit/grains/test_core.py
|
2018-04-11 18:47:08 +02:00
|
|
|
index 50babe3ed3..47c9cdd35b 100644
|
2018-01-26 14:46:21 +01:00
|
|
|
--- a/tests/unit/grains/test_core.py
|
|
|
|
+++ b/tests/unit/grains/test_core.py
|
2018-02-28 16:27:22 +01:00
|
|
|
@@ -7,6 +7,7 @@
|
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
|
|
|
import logging
|
2018-01-26 14:46:21 +01:00
|
|
|
import os
|
|
|
|
+import socket
|
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2018-02-28 16:27:22 +01:00
|
|
|
try:
|
2018-01-26 14:46:21 +01:00
|
|
|
--
|
2018-03-19 09:54:13 +01:00
|
|
|
2.16.2
|
2018-01-26 14:46:21 +01:00
|
|
|
|
|
|
|
|