ansible postgres add multiple extensions to database (nested loop)

Viewed 122

For creating PostgreSQL databases I use the following schema:

---
postgres_database:
  - name: test
    user: test
    pass: secret

  - name: test2
    user: test2
    pass: secret

But now I need to add extensions per database so I thought of using something like:

---
postgres_database:
  - name: test
    user: test
    pass: secret
    extensions:
      - unaccent
      - pgcrypto
      - uuid-ossp

  - name: test2
    user: test2
    pass: secret

  - name: test3
    user: test3
    pass: secret
    extensions:
      - pgcrypto
     

The problem I have is that I can only pass an extension at a time, therefore wondering how could I loop over the itemspostgres_database.extensions within the posgres_database loop?

Currently, this works but only adds the first extension found:

- name: add extensions
  become: yes
  become_user: postgres
  postgresql_ext:
    name: "{{ item.extensions[0] }}"
    db: "{{ item.name }}"
  when: item.extensions is defined
  with_items:
    - "{{ postgres_database }}"

Any ideas about how could this be solved?

1 Answers
Related