state for all hosts expect one, and get IP

14 views
Skip to first unread message

stefa...@googlemail.com

unread,
May 18, 2022, 9:57:10 AM5/18/22
to Salt-users

Hello,

maybe a stupid question but, we've a state that install sytemd-timesynced, and we also have a timerserver which is running ntpd, so how can I archive that all hosts get the state expect of the ntp-server, at the moment I would create an if condition in the state, does exist a better (salt)way?
Or maybe via pillars? Something like if pillar "ntp-server" is set then don't install?! but how should look this in a state?

Second question is, we are running zabbix and create hosts via salt for this we need the IP address (we get his by: ip: {{ grains['ipv4'][1] }}), but if a host has multiple IP addresses how can I get the correct one? Sometimes we had: ens192, enp2s0f0, docker0, lo, etc, in the old way we should ask eth0, but nowadays we've VMs and hardwareservers and the interface name is very different. Any ideas how to fix that?

thanks in advance!
best regards
Stefan

Dirk Heinrichs

unread,
May 18, 2022, 12:01:14 PM5/18/22
to salt-...@googlegroups.com
Am 18.05.22 um 15:57 schrieb 'stefa...@googlemail.com' via Salt-users:

maybe a stupid question but, we've a state that install sytemd-timesynced, and we also have a timerserver which is running ntpd, so how can I archive that all hosts get the state expect of the ntp-server, at the moment I would create an if condition in the state, does exist a better (salt)way?
Or maybe via pillars? Something like if pillar "ntp-server" is set then don't install?! but how should look this in a state?

Not in the state, but in your top.sls. You can either use hostname based targeting or maybe by a (static) custom grain ("role", or something like that) you create on your NTP server to identify it as such. You can the write something like this into your top.sls:

{{ saltenv }}:
 'G@kernel:Linux and not G@role:NTPServer': # or 'G@fqdn:ntp.yourdomain.org'
   - match: compound
   - <state to install timesyncd>
 'G@kernel:Linux and G@role:NTPServer': # or 'G@fqdn:ntp.yourdomain.org'
   - match: compound
   - <state to install NTP>
  '*':
    - other
    - states

Second question is, we are running zabbix and create hosts via salt for this we need the IP address (we get his by: ip: {{ grains['ipv4'][1] }}), but if a host has multiple IP addresses how can I get the correct one? Sometimes we had: ens192, enp2s0f0, docker0, lo, etc, in the old way we should ask eth0, but nowadays we've VMs and hardwareservers and the interface name is very different. Any ideas how to fix that?

I use a custom grain ("primary_interface") for this. Put this into the "_grains" directory in your Salt master's fileroot (or git repository if your Salt states are managed this way):

#!/usr/bin/env python

from salt.utils.platform import is_linux

def __virtual__():
  '''
  Only valid on Linux minions
  '''
  return is_linux()

def primary_interface():
  '''
  Determine the primary network interface on Linux systems
  '''
  result = {}
  with open('/proc/net/route', 'r') as route:
    for line in route:
      elements = line.split("\t")
      if elements[1] == '00000000' and elements[7] == '00000000':
        result['primary_interface'] = elements[0]
  return result


HTH...

    Dirk
-- 
Dirk Heinrichs <dirk.he...@altum.de>
Matrix-Adresse: @heini:chat.altum.de
GPG Public Key: 80F1540E03A3968F3D79C382853C32C427B48049
Privacy Handbuch: https://www.privacy-handbuch.de
OpenPGP_signature
Reply all
Reply to author
Forward
0 new messages