How to create salt from pillar using saltstack?

Lets take an example of what I have today: https://gist.github.com/Natim/6548009

I use the pillar to create a database and a user. It works fine on server with one of the roles but if I need both roles on the same server only the last pillar is taken into account.

How can I create my pillar and salt so that a salt is create for each role?

I could do something like that:

{% for db in pillar['dbs'] %}
postgresql_db_{{ db['postgresql_db_name'] }}:
    postgres_database.present:
        - name: {{ db['postgresql_db_name'] }}
        - owner: {{ db['postgresql_db_user'] }}
        - encoding: UTF8
        - lc_ctype: en_US.UTF8
        - lc_collate: en_US.UTF8
        - template: template0
        - runas: postgres
        - require:
            - service: postgresql
            - postgres_user: postgresql_user_{{ db['postgresql_db_user'] }}

postgresql_user_{{ db['postgresql_db_user'] }}:
    postgres_user.present:
        - name: {{ db['postgresql_db_user'] }}
        - password: {{ db['postgresql_db_password'] }}
        - require:
            - service: postgresql
{% endfor %}

But then how to populate the pillar['dbs'] with two files?

Thanks


Solution 1:

I finally managed to do what I wanted.

Here is my solution:

pillar

postgresql-database-novaauth:
    user: novaauth
    password: novaauth
    name: novaauth

postgresql-database-peopleask:
    user: peopleask
    password: peopleask
    name: peopleask

They can be in different files.

salt

Then create your salt like this:

postgresql:
    pkg:
        - name: postgresql-9.3
        - installed
    service.running:
        - enable: True

{% for key in pillar.keys() if key.startswith('postgresql-database-') %}

postgresql_db_{{ pillar[key]['name'] }}:
    postgres_database.present:
        - name: {{ pillar[key]['name'] }}
        - owner: {{ pillar[key]['user'] }}
        - encoding: UTF8
        - lc_ctype: en_US.UTF8
        - lc_collate: en_US.UTF8
        - template: template0
        - runas: postgres
        - require:
            - service: postgresql
            - postgres_user: postgresql_user_{{ pillar[key]['user'] }}

postgresql_user_{{ pillar[key]['user'] }}:
    postgres_user.present:
        - name: {{ pillar[key]['user'] }}
        - password: {{ pillar[key]['password'] }}
        - require:
            - service: postgresql

{% endfor %}

postgresql.conf:
    file.append:
        - name: /etc/postgresql/9.3/main/postgresql.conf
        - text: "listen_addresses = '*'"
        - require:
            - pkg: postgresql
        - watch_in:
            - service: postgresql

The important thing is this :

{% for key in pillar.keys() if key.startswith('postgresql-database-') %}

This let you define many pillars that will be taken into account without overriding other.

Interesting article: http://dev.mlsdigital.net/posts/SaltStackBeyondJinjaStates/

Many thanks to brutasse for the help.