0

Hi ServerFault Community,

I'm trying to learn SaltStack and now I'm facing a problem. I have a property file (propertyfile.properties) with values being populated by salt pillar. Now this property file exists in multiple directories. The issue that I have is that I want the values of the property file be populated/rendered by salt pillar.get function base on what directory currently it is into. To give you more idea, let's consider this example:

propertyfile.property (in directory 1)
name={{ salt['pillar.get']['dir1.name'] }}


propertyfile.property (in directory 2)
name={{ salt['pillar.get']['dir2.name'] }}

#pillar
dir1.name=dir1
dir2.name=dir2

note that the property file is only one and is generated by salt to multiple directories via loop like this:

{% for dir in 'dir1', 'dir2' %}
propertyfile_properties_{{ dir }}:
    file.managed:
        - name: /home/devuser/{{ dir }}/propertyfile.properties
        - source: {{ propertyfile_source }}
        - source_hash: {{ propertyfile_source }}.MD5
        - template: jinja
        - show_diff: True
        - makedirs: True
{% endfor %}

Any ideas? Your help is very much appreciated. Thanks

1 Answer 1

1

It sounds like you need like /home/devuser/{{ dir }}/ in the propertyfile_properties_{{ dir }} file? You can always add context to a state to pass variables into jinja

So it might look something like

{% for dir in 'dir1', 'dir2' %}
propertyfile_properties_{{ dir }}:
    file.managed:
        - name: /home/devuser/{{ dir }}/propertyfile.properties
        - source: {{ propertyfile_source }}
        - source_hash: {{ propertyfile_source }}.MD5
        - template: jinja
        - show_diff: True
        - makedirs: True
        - context
          file_dir: /home/devuser/{{ dir }}/
{% endfor %}

So in the template you should be able to use {{ file_dir }}

3
  • Hi @Mike, thanks. I want the dir.name to be dynamic base on the current directory where the property file is being rendered. Is your solution able to do that? I'm thinking of maybe including some sort of conditionals inside the file like: {% set dirname = salt['cmd.run']<code to get current directory> %} name={{ salt['pillar.get']['+ {{ dirname }} +'.name'] }}
    – Master_A2Z
    Dec 21, 2015 at 6:39
  • if the file is being placed in /home/devuser/{{ dir }}/propertyfile.properties then using my answer is valid.
    – Mike
    Dec 21, 2015 at 6:52
  • Hi @Mike, i'm successful in getting the current dir via the context. However, this does not work: dir={{ file_dir }} ## this result to dir1 name={{ salt['pillar.get'](dir +'.name','default_name') }} it always get the default_name value whereas my dir1.name has a value of 'new_name'. Any ideas?
    – Master_A2Z
    Dec 23, 2015 at 7:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .