Search code examples
jinja2configsalt-project

Why is salt['slsutil.banner'] returning None whatever arguments I pass?


I'm using saltstack to manage my personal config. In particular I'm managing my ~/.profile programatically. I have various things throughout the setup which append text (mostly env var exports) to it, and they all work exactly as expected. I want to to use slsutil.banner to prepend a banner saying that the file is managed programatically by salt, and not to touch it by hand. So I have in my profile/init.sls:

profile-managed-banner:    
  file.prepend:    
    - name: {{ pillar['profile_file'] }}    
    - text: {{ salt['slsutil.banner']() }}

This should write the default banner to the beginning of the file.

When I then run run salt-call (the setup is masterless. Running salt-call as sudo if that's at all relevant) I get:

      ID: profile-managed-banner
Function: file.prepend
    Name: /home/modallyFragile/.saltProfile
  Result: True
 Comment: File /home/modallyFragile/.saltProfile is in correct state
 Started: 15:59:07.760790
Duration: 1.757 ms
 Changes:   

So clearly salt can find and use all the functions (or at least it thinks it can) and the the file is having something prepended to it. If I check the file though, I get this:

None
[ ... further config here]

If I substitute the templating for a string (so - text: some string here) it works as expected (prepends 'some string' to the file). So the probelm is with the templating slsutil.banner then. I've tried passing (various combinations of) arguments explicitly and nothing seems to help.

Why might this be happening and what can I do about it? Failing anything more substantive, what could I do to further diagnose the problem (I'm pretty new to saltstack, is there a particular log I should be checking with all the relevant info, etc. etc.)? I can't find any issues or problems by searching (github or more generally), so I'm drawing a blank. Literally any suggestions would be really helpful. Thanks!


Solution

  • Adding this as an answer. As per the official documentation:

    Create a standardized comment block to include in a templated file.

    And this does work inside a templated file.

    Just for example files/user-profile.j2:

    {{ salt.slsutil.banner() }}
    
    # the default umask is set in /etc/profile; for setting the umask
    # for ssh logins, install and configure the libpam-umask package.
    umask 022
    
    PATH=$PATH:/my/local/bin
    

    Used in a setup.sls:

    create-user-profile:
      file.managed:
      - name: /home/user1/.profile
      - source: salt://files/user-profile.j2
      - template: jinja
    

    Will create a file /home/user1/.profile with a default banner at the top and the remaining content as it is.

    Then it starts making sense. If you want a banner on file that "it is managed by Salt", then the file should be managed by Salt (as template). Prepending the banner in a remote file, leaving the remaining file contents unmanaged contradicts the purpose of the banner.

    So if you want a banner in the file, you can manage the ~/.profile file as a template, and write it to minions as above.

    Update:

    It might be worth reporting this as an issue if we want a custom banner prepended to a file without managing it.