Profile photo

from miller import blake

Hello, I'm a Seattle-based site reliabililty engineering manager, I get paid to do what I love, I like Python, I'm in an abusive relationship with JavaScript, I'm a fan of good design, and I don't think things always have to be so stupid.
You can follow me @bltmiller, subscribe via RSS, and email me.

Dynamically Manage and Update a File with SaltStack

I had the problem recently of two functions infinitely changing each other every time a highstate was triggered. The requirement was that a given config file needed to contain some static content on every minion along with, potentially, some added content based on a minion’s given role (e.g. web server and database server have different needs).

The problem arose from using both file.managed and file.append together to modify the same file. In common.sls:

baseline-generic-config:
  file.managed:
    - name: ~/.bash_profile
    - source: salt://path/to/.bash_profile

and in maven.sls:

add-maven-env-variables:
  file.append:
    - name: ~/.bash_profile
    - text: "export MAVEN_HOME=$HOME/path/to/maven"

On a minion that gets targeted with maven.sls (assuming every minion gets common.sls applied), every time a highstate is triggered, the file.managed function with ID baseline-generic-config will notice that the ~/.bash_profile has changed and revert the changes. Next, the file.append function with ID add-maven-variables will notice that the MAVEN_HOME export does not exist and will append to the file. Rinse and repeat for every subsequent highstate.

A good solution for working around this is to use file.exists and then manage the file exclusively through file.append. In the updated common.sls:

touch-generic-config:
  file.exists:
    - name: ~/.bash_profile

baseline-generic-config:
  file.append:
    - name: ~/.bash_profile
    - source: salt://path/to/.bash_profile

and maven.sls goes unchanged. Now, a highstate will instead only trigger a file.append if the required contents are changed.


comments powered by Disqus

Copyright © 2020, Blake Miller. All rights reserved. | Sitemap · RSS