saltstack-formulas / hostsfile-formula

Use minion IDs to keep /etc/hosts and hostnames consistent for clusters without DNS
http://docs.saltstack.com/en/latest/topics/development/conventions/formulas.html
Other
33 stars 89 forks source link

[BUG] Formula adds garbage to /etc/hosts if hostsfile:alias returns none #33

Closed andrew-vant closed 3 years ago

andrew-vant commented 3 years ago

Your setup

Formula commit hash / release tag

02c5de72b32e5b14ced692ead558f74964c09841

Versions reports (master & minion)

Master is 3001.1, minion is 3000.3:

Minion

Salt Version:
           Salt: 3000.3

Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: 2.6.1
      docker-py: Not Installed
          gitdb: Not Installed
      gitpython: Not Installed
         Jinja2: 2.10
        libgit2: Not Installed
       M2Crypto: Not Installed
           Mako: 1.0.7
   msgpack-pure: Not Installed
 msgpack-python: 0.5.6
   mysql-python: Not Installed
      pycparser: Not Installed
       pycrypto: 2.6.1
   pycryptodome: Not Installed
         pygit2: Not Installed
         Python: 2.7.17 (default, Jul 20 2020, 15:37:01)
   python-gnupg: 0.4.1
         PyYAML: 3.12
          PyZMQ: 16.0.2
          smmap: Not Installed
        timelib: Not Installed
        Tornado: 4.5.3
            ZMQ: 4.2.5

System Versions:
           dist: Ubuntu 18.04 bionic
         locale: UTF-8
        machine: x86_64
        release: 4.15.0-118-generic
         system: Linux
        version: Ubuntu 18.04 bionic

Master

Salt Version:
           Salt: 3001.1

Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: 2.6.1
      docker-py: Not Installed
          gitdb: 2.0.3
      gitpython: 2.1.8
         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: 2.6.1
   pycryptodome: 3.4.7
         pygit2: Not Installed
         Python: 3.6.9 (default, Jul 17 2020, 12:50:27)
   python-gnupg: 0.4.1
         PyYAML: 3.12
          PyZMQ: 17.1.2
          smmap: 2.0.3
        timelib: Not Installed
        Tornado: 4.5.3
            ZMQ: 4.2.5

System Versions:
           dist: ubuntu 18.04 Bionic Beaver
         locale: UTF-8
        machine: x86_64
        release: 4.15.0-99-generic
         system: Linux
        version: Ubuntu 18.04 Bionic Beaver

Pillar / config used

Pillar config is minimal:

hostsfile:
  alias: ip
  minions: '*'

And the alias:

mine_functions:
  ip:
    - mine_function:  grains.get
    - path:to:grain

Bug details

Describe the bug

If the mine function specified under hostsfile:alias returns nothing for a given minion, the state 1. always returns dirty, and 2. appends multiple copies of some other host in the hosts file -- I think always the last one present, but I'm not sure. So you get something like this, assuming there are four hosts and host4 is the one with no valid return:

192.168.0.1 host1
192.168.0.2 host2
192.168.0.3 host3 host3 host3 host3 host3 host3 host3 (more)
# host4 never appears

Steps to reproduce the bug

Configuring the pillar as above and rigging the alias to return None for minion should reproduce the problem.

Expected behaviour

Probably offending hosts should be ignored, or the state should return an error, or something. In any case some check should be done that what we're trying to add is really an IP address.

Attempts to fix the bug

I haven't poked around it yet.

Additional context

I am not sure if the underlying bug is in the formula or in salt itself.

edwin--zhao commented 3 years ago

I met this problem when mine_functions return ip is None ,
add {%- if addrlist %}

noelmcloughlin commented 3 years ago

38