0

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

my_config_key1:
  - postgres.psql_query:
    query: UPDATE configuration SET value = 'value1' WHERE key = 'key1';
my_config_key2:
  - 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.

Jorn
  • 441
  • 1
  • 4
  • 13

1 Answers1

1

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:

/srv/pillar/pg-properties.sls:

pg_properties:
  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

savamane
  • 91
  • 2