I have a Salt script that, among other things, initializes a Postgres database. After initializing the database, I want to read some properties from a file, and do an INSERT (into a configuration table) for each key/value pair.

I've seen Salt's psql_query which should solve part of the problem. My question is, how do I iterate over the pairs in my property file, then do a query for each entry?

So, the file

key1 = value1
key2 = other value

should translate into salt

  - postgres.psql_query:
    query: UPDATE configuration SET value = 'value1' WHERE key = 'key1';
  - postgres.psql_query:
    query: UPDATE configuration SET value = 'other value' WHERE key = 'key2';

If there's a better way than a property file for passing in the key/values, that would be fine too.


If you can get away with checking in those properties into a pillar, it would be very easy to iterate over every K/V pair with Jinja.

Say you have this in your pillar data:


  key1: value1
  key2: value2

and the state /srv/salt/pg-populate.sls:

{% for key, value in pillar.get('pg_properties', {}).items() %}
my_config_{{ key }}:
  - postgres.psql_query:
    query: UPDATE configuration SET value = '{{ value }}' WHERE key = '{{ key }}';
{% endfor %}

Salt will iterate over every keyX entry and run the query for all of them.

If the properties are generated dynamically, you can look into loading them as pillar data via salt's ext_pillar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.