Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i add the contents of a pillar variable to a file with salt?

In a salt state file, how do I add the contents of a pillar to a file on the remote salt-minion?

For instance, if I have pillar data like:

ssl:
    some-domain.com:
        key:
            -----BEGIN RSA PRIVATE KEY-----
            MIICX... snip ...

And on the remote salt-minion, I want to have a file like, /etc/nginx/ssl/som-domain.com.key that contains the contents of that private key, how would I do it? I'm also open to an answer like, "You're doing it all wrong, dummy." As long as you give me some insight and point me in the right direction it will help tremendously.

I know about salt's file.managed, and file.* friends from some testing, but I'm not sure if there's a best / preferred way to add arbitrary pillar data to a remote file as I'm trying to do.

I'm still learning / hacking around with salt, so I'm also still using Yaml + Jinja for templating, if it matters to you in formulating an appropriate answer.

Thanks!

Edit: By the way current, hacky solution is:

Create a file in my file_root /srv/salt/, like /srv/salt/ssl/some-domain.com.key that contains something like {{ salt[pillar.get]('ssl:some-domain.com:key') }}

But that just seems so hacky. Looking for a better solution here.

like image 493
Kevin Avatar asked Jul 27 '15 01:07

Kevin


1 Answers

This is available from the file.managed state, though admittedly it's easy to miss in the documentation:

# /srv/salt/something.sls
some-domain-key:
  file.managed:
    - name: /etc/nginx/ssl/some-domain.com.key
    - mode: 600
    - contents_pillar: ssl:some-domain.com:key

If you're managing nginx as your example suggests, you may also be interested in the nginx.ng formula. It can do this for you.

like image 112
Andrew Avatar answered Oct 12 '22 23:10

Andrew