Salt changing /etc/hosts, but still caching old one?

918 views Asked by At

Is salt caching /etc/hosts? I'm in a situation where I change /etc/hosts such that the FQDN points to the external IP address instead of 127.0.0.1

The problem is that in the first run, the fqdn_ipv4 stays 127.0.0.1 and I need to rerun salt '*' state.highstate to get the right values. This leads to problems like this, which cost me a lot of time.

Is salt rendering everything before execution (or caches DNS)? How do I address this problem?

The state file looks like this:

127.0.0.1:
  host.absent:
    - name:     {{ nodename }}
    - ip:          127.0.0.1

127.0.1.1:
  host.absent:
    - name:     {{ nodename }}
    - ip:          127.0.1.1

{% for minion, items in salt['mine.get']('environment:' + environment, 'grains.item', expr_form='grain')|dictsort %}

{{ minion }}:
  host.present:
    - ip:       {{ items['ip_addr'] }}
    - names:
      - {{ minion }}
      - {{ minion.split('.')[0] }}

{% endfor %}

And the code that uses the IP looks like this:

{% set ipv4     = salt['config.get']('fqdn_ip4') -%}

# IP Address that Agent should listen on
listening_ip={{ ipv4[0] }}
1

There are 1 answers

0
ahus1 On BEST ANSWER

Salt is caching the values of grains. Therfore the salt['config.get']('fqdn_ip4') will retrieve the value from the beginning of the script.

Use the following in your state file to refresh the grain information:

refreshgrains:
  module.run:
    - name: saltutil.sync_grains

Salt will render the state before executing it, so you might not be able to use any new grain information inside the state file itself.

But you will be able to use the new grain values in Jinja templates for files. I assume the second code snippet is from a template that is used by Salt's file.managed, so you should be safe here.