Search code examples
salt-project

SaltStack - Managed file gets written on every run, how to only write the file if there are updates?


I'm having some trouble where my managed files are being written on every state run, even if there was nothing that updated in the file. Oddly enough, I can't seem to find anything about stopping this from happening via google or in the salt docs.

My states is split up into init.sls to deploy the package, and config.sls to configure the package, with default and environment specific config values split out into a pillar. Examples are below.

Config State Example:
{% from "amq/map.jinja" import amq with context %}
camelxml_conf:
  file.managed:
    - name: {{ amq.camelxml }}
    - source: salt://amq/conf/camel.xml.tmpl
    - template: jinja
    - user: omapp
    - group: omapp
    - mkdirs: True
    - recurse:
      - user
      - group

Pillar Example:
default_routes:
  Route1:
    from_uri: 'activemq:inputqueue1'
    process_ref: 'myprocessor1'
    to_uri: 'activemq:outputqueue1'
  Route2:
    from_uri: 'activemq:inputqueue2'
    process_ref: 'myprocessor2'
    to_uri: 'activemq:outputqueue2'

{% if 'qa' in grains['env'] %}
env_routes:
  route1:
    from_uri: 'activemq:inputqueue3'
    process_ref: 'myprocessor3'
    to_uri: 'activemq:outputqueue3'
{% endif %}

Camel.xml Template Example:
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
      <!-- routes common across all environments -->
      {% for route, args in pillar.get('default_routes', {}).items() %}
        <route>
          <from uri="{{ args.from_uri }}"/>
          <process ref="{{ args.process_ref }}"/>
          <to uri="{{ args.to_uri }}"/>
        </route>
      {% endfor %}

      <!-- routes unique to this environment -->
      {% for env_route, args in pillar.get('env_routes', {}).items() %}
        <route>
          <from uri="{{ args.from_uri }}"/>
          <process ref="{{ args.process_ref }}"/>
          <to uri="{{ args.to_uri }}"/>
        </route>
      {% endfor %}

Am I doing anything completely wrong here? Should I not be setting values dynamically in my pillar? I'm thinking this may be why a new file is written on each state run, but am not sure.


Solution

  • If you run your state with test=True appended to the command it will show you a diff of the changes it's going to make. That might help you track down why it thinks a change is necessary