jinja: static per server uuid

747 views
Skip to first unread message

pille

unread,
Jul 5, 2012, 6:39:30 AM7/5/12
to salt-...@googlegroups.com
hi,

i need to generate a config file that includes a UUID (RFC 4122).
i'd like to do this using jinja-templating, but don't know a solid way
to do it and like to hear your comments.

the UUID is static and thus its generation should be idempotent.
i don't like to use pillar for that, because actually the ID doesn't
matter, so i don't want to configure it.

is there a way to derive one from the server-id-grain and an
service-identifying string like:
uuid(GRAINS['server-id'] + 'service1') =
68cbaed6-c68d-11e1-8095-7c726da07202
uuid(GRAINS['server-id'] + 'service2') =
81436bf2-c68d-11e1-a079-7c726da07202

pille

Thomas S Hatch

unread,
Jul 5, 2012, 11:29:27 AM7/5/12
to salt-...@googlegroups.com
Let me make sure I follow you, you want to generate a consistent UUID from the server_id grain and place it in the template. I don't know of a way to import python modules directly into jinja, you could write the template in pure python, or make a uuid salt module that could call the function.

pille+salt+...@struction.de

unread,
Jul 5, 2012, 12:09:38 PM7/5/12
to salt-...@googlegroups.com
> Let me make sure I follow you, you want to generate a consistent UUID
> from the server_id grain and place it in the template.

yes, it should be generated from server_id and an arbitrary string
(service-specific, to have more than one of such UUIDs).

> I don't know of a
> way to import python modules directly into jinja, you could write the
> template in pure python, or make a uuid salt module that could call the
> function.

wouldn't it work to assign it a variable in the sls and pass that to jinja?

pille

Thomas S Hatch

unread,
Jul 5, 2012, 4:31:38 PM7/5/12
to salt-...@googlegroups.com
if you are willing to paste the uuids in the sls that would of course work, then it is passed as a variable into the template. I thought you wanted to generate it on the fly

pille+salt+...@struction.de

unread,
Jul 6, 2012, 4:04:37 PM7/6/12
to salt-...@googlegroups.com
On 07/05/2012 10:31 PM, Thomas S Hatch wrote:
> if you are willing to paste the uuids in the sls that would of course
> work, then it is passed as a variable into the template. I thought you
> wanted to generate it on the fly

no, i thought about generating it in the sls and assigning it to a
variable that is passed to the template.

or is sls-magic limited to jinja, too?

pille

Thomas S Hatch

unread,
Jul 7, 2012, 10:56:45 PM7/7/12
to salt-...@googlegroups.com
Well, this is getting hairy, but it should work:

/etc/uuidfile:
  file.managed:
    - source: salt://somefile
    - template: jinja
    - uuid: {{salt['cmd.run']('python2 -c 'import uuid; print uuid.uuid5(uuid.NAMESPACE_DNS, ' + grains['server-id'] + 'service')}}

Or at least get you closer, since I have not tested this

pille+salt+...@struction.de

unread,
Jul 17, 2012, 8:34:05 AM7/17/12
to salt-...@googlegroups.com
On 07/08/2012 04:56 AM, Thomas S Hatch wrote:
> Well, this is getting hairy, but it should work:
>
> /etc/uuidfile:
> file.managed:
> - source: salt://somefile
> - template: jinja
> - uuid: {{salt['cmd.run']('python2 -c 'import uuid; print
> uuid.uuid5(uuid.NAMESPACE_DNS, ' + grains['server-id'] + 'service')}}

all in all it does not run.
i've got the part generating the uuid running in a shell,
but probably due to quoting i cannot make it work in salt.

when i place it in a static script with hardcoded strings that is called
in jinja, it works.

i've failed using ARGV or ENV to make it dynamic.

pille

pille

unread,
Aug 10, 2012, 2:24:42 PM8/10/12
to salt-...@googlegroups.com
On Sat, 7 Jul 2012 20:56:45 -0600, Thomas S Hatch wrote:
> /etc/uuidfile:
> file.managed:
> - source: salt://somefile
> - template: jinja
> - uuid: {{salt['cmd.run']('python2 -c 'import uuid; print
> uuid.uuid5(uuid.NAMESPACE_DNS, ' + grains['server-id'] + 'service')}}
>
> Or at least get you closer, since I have not tested this

thanks again for the snippet. i forgot to post my solution after
fiddling around with the quoting and escaping escape-hell and want to
supply you with that, finally.

the following is my working real-world example. 'libvirt-network-config'
is the service tag.
@thomas: i guess it's not possible to define another variable inside
that resource-definition, prior to uuid and reference that inside the
uuid-definition?

/etc/libvirt/qemu/networks/virt.xml:
file.managed:
- source:
salt://ubuntu-virt-server/files/etc/libvirt/qemu/networks/virt.xml
- template: jinja
- uuid: {{ salt['cmd.run']('python2 -c \'import uuid; print
uuid.uuid5(uuid.NAMESPACE_DNS, ' + ' "' + grains['id'] +
'libvirt-network-config' + '")\'') }}
- makedirs: True

salt://ubuntu-virt-server/files/etc/libvirt/qemu/networks/virt.xml looks
like this:
<network>
<name>virt</name>
<uuid>{{ uuid }}</uuid>
<forward dev='eth0' mode='nat'/>
<bridge name='virt' stp='on' delay='0' />
<domain name='virt.mediapeers.com'/>
<ip address='172.16.0.1' netmask='255.255.255.0'>
</ip>
</network>

cheers
pille

Thomas S Hatch

unread,
Aug 10, 2012, 2:47:17 PM8/10/12
to salt-...@googlegroups.com
On Fri, Aug 10, 2012 at 12:24 PM, pille <pille+salt+...@struction.de> wrote:
On Sat, 7 Jul 2012 20:56:45 -0600, Thomas S Hatch wrote:
/etc/uuidfile:
  file.managed:
    - source: salt://somefile
    - template: jinja
    - uuid: {{salt['cmd.run']('python2 -c 'import uuid; print
uuid.uuid5(uuid.NAMESPACE_DNS, ' + grains['server-id'] + 'service')}}

Or at least get you closer, since I have not tested this

thanks again for the snippet. i forgot to post my solution after fiddling around with the quoting and escaping escape-hell and want to supply you with that, finally.

the following is my working real-world example. 'libvirt-network-config' is the service tag.
@thomas: i guess it's not possible to define another variable inside that resource-definition, prior to uuid and reference that inside the uuid-definition?

I don't follow...
Reply all
Reply to author
Forward
0 new messages