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] state `hostsfile` broken if no pillar `hostsfile:hosts` provided #29

Closed daks closed 4 years ago

daks commented 4 years ago

Your setup

Formula commit hash / release tag

3be120235d5bab28eb8f4f7ea5f8d6a250ba14d4 v0.8.0

Versions reports (master & minion)

2019.2.0

Pillar / config used

hostsfile:
  alias:
      main_ip
  hosts:
      None
  only:
      ----------
      127.0.0.1:
          - foo.tld
          - foo
            - localhost

main_ip is a mine_function we use and return an IP depending of the main interface serving a service.

Bug details

Describe the bug

When hosts is not completed the state is broken with a message:

    Rendering SLS 'hostsfile' failed: Jinja error: 'NoneType' object is not iterable
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/salt/utils/templates.py", line 392, in render_jinja_tmpl
    output = template.render(**decoded_context)
  File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python3/dist-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 22, in top-level template code
TypeError: 'NoneType' object is not iterable

; line 22

---

[...]
{%- set pillar_only = salt['pillar.get']('hostsfile:only', {}) %}
{%- set mine_hosts = salt['mine.get'](minions, minealias, tgt_type=minions_type) %}
{%- if mine_hosts is defined %}
{%-   do hosts.update(mine_hosts) %}
{%- endif %}
{%- do hosts.update(pillar_hosts) %}    <======================

I think this formula could be used to generate a /etc/hosts only using a mine query but no static host.

Steps to reproduce the bug

Expected behaviour

I expect the formula to be able to:

Attempts to fix the bug

Working on it, hoping to find a bugfix soon.

Additional context

daks commented 4 years ago

Let's close this bug report. The problem seems to not come from the formula itself but our use of pillarstach which generates hosts: None instead of a non-existent pillar.