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

假装没事ソ 提交于 2019-12-25 02:48:33

问题


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:


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.



来源:https://stackoverflow.com/questions/31007254/salt-changing-etc-hosts-but-still-caching-old-one

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!