Created
October 8, 2015 11:18
-
-
Save Xiol/f8a27b24af936293c862 to your computer and use it in GitHub Desktop.
Salt grain for discovering primary IP address on a Linux machine.
This file contains 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
#!/usr/bin/env python | |
import subprocess | |
def linux_get_primary_ip4(): | |
grains = {} | |
try: | |
defroute_device = subprocess.check_output("ip route | grep default | grep -Eo 'dev\s+\w+' | awk '{print $2}'", shell=True).strip() | |
devip = subprocess.check_output("ip a s {0} | grep -E 'inet\s' | cut -d'/' -f1 | awk '{{print $2}}'".format(defroute_device), shell=True).strip() | |
except subprocess.CalledProcessError as e: | |
return | |
if devip is not None or not devip == "": | |
grains['primary_ip4'] = devip | |
return grains |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment