Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

325
Views
ansible postgres add multiple extensions to database (nested loop)

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

In a nutshell:

- name: add extensions
  become: yes
  become_user: postgres
  postgresql_ext:
    name: "{{ item.1 }}"
    db: "{{ item.0.name }}"
  loop: "{{ postgres_database | subelements('extensions', skip_missing=True) }}"

Refs:

  • https://docs.ansible.com/ansible/4/user_guide/playbooks_loops.html#with-subelements
  • https://docs.ansible.com/ansible/latest/collections/ansible/builtin/subelements_lookup.html
  • https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#combining-objects-and-subelements
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!