How to make jinja read in contents of a file

6,697 views
Skip to first unread message

Nick Davis

unread,
Aug 7, 2013, 1:18:49 PM8/7/13
to salt-...@googlegroups.com
I'm trying to use jinja in a pkg.installed state to create an effect similar to how pip.installed can install packages from a requirements file.

In my pillar file, I have this:

proj_root: /project


In my PACKAGES.ubuntu file I have this:

postgresql
postgresql-server-dev-9.1
postgresql-contrib-9.1


Then in my sls file, I have this:

{% set pkgfile =  salt['pillar.get']('proj_root') + "/installation/PACKAGES.ubuntu" %}

os-pkgs:
  pkg.installed:
    - pkgs:
      {% for pkgname in pkgfile %}
      - {{ pkgname }}
      {% endfor %}


Now, my salt root and salt states are in a subdirectory of my project.

The PACKAGES.ubuntu file is in a different subdirectory.

Using the above, I keep getting this error:

MinionError: Unsupported path: /project/installation/PACKAGES.ubuntu


I also tried to use import and include on the pkgfile, but I get the same error. I know that my set pkgfile statement is correct because the error message prints out the correct path.

Has anyone successfully read the contents of a file outside of the salt root into a variable and used those contents to create parts of the state declaration? What am I doing wrong? This seems like it should be very simple.

P.S. I am open to using a renderer other than jinja if that would make it easier. But I cannot move the location of the PACKAGES file, so I need to read it where it sits.

Thanks,
Nick


David Anderson

unread,
Aug 7, 2013, 3:05:10 PM8/7/13
to salt-...@googlegroups.com
I don't think jinja can read the contents of a file from the filesystem.

Instead, you can use the PyDSL renderer (
http://docs.saltstack.com/topics/tutorials/starting_states.html#introducing-the-python-and-the-pydsl-renderers
) that reads the proj_root/intallation/PACKAGES.ubuntu file and compiles
it into the pkg.installed highstate data.

More at
https://salt.readthedocs.org/en/v0.16/ref/renderers/all/salt.renderers.pydsl.html
--
Dave
> --
> You received this message because you are subscribed to the Google
> Groups "Salt-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to salt-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Nick Davis

unread,
Aug 8, 2013, 10:40:08 AM8/8/13
to salt-...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I was wondering if that was my problem. I read through a lot of jinja
documentation and didn't see mention of that ability.

I'll give PyDSL a shot. Thanks for the idea.

Nick
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEARECAAYFAlIDrcgACgkQYoNGId/piAtpDQCdGyAHY+cwu/Ol0wpjDGn3kAUp
iAwAoJdJzVjx8l27yxKGrWI4MiOH4Lzo
=6eUP
-----END PGP SIGNATURE-----
Reply all
Reply to author
Forward
0 new messages