Search code examples
jinja2salt-project

Using dns_check in salt (wherein the documention seems wrong)


The saltstack docs note the existence of the dns_check jinja filter in 2017.7.3:

{{ 'www.google.com' | dns_check }}

which should return an ip4v address as a string.

But when I try it:

test_this_one:
  cmd.run:
    - name: |
        echo {{ 'www.google.com' | dns_check }}

I instead see

local:
    Data failed to compile:
----------
    Rendering SLS 'base:firewall' failed: Jinja error: dns_check() takes at least 2 arguments (1 given)
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/salt/utils/templates.py", line 418, in render_jinja_tmpl
    output = template.render(**decoded_context)
  File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 989, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 754, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "<template>", line 42, in top-level template code
TypeError: dns_check() takes at least 2 arguments (1 given)

Am I missing something? I'm inclined to believe I've made a mistake rather than that the docs are so publicly wrong.


Solution

  • Looking at the source code it looks like dns_check now takes a port argument - the docstring says:

    Tries to connect to the address before considering it useful. If no address can be reached, the first one resolved is used as a fallback.

    https://github.com/saltstack/salt/blob/06a00be0e1f06399805e19261e4d00f6cfd9c6a0/salt/utils/network.py#L1750

    So it's probably enough to put any port in here and it should work. (and perhaps an issue should be raised about making port optional?)