Version of package installed with apt?

Is there an elegant way to find out what the version of an installed package (say with the apt module) is?

I now have this code to get PostgreSQL’s configdir on Debian:

  • name: required packages for PostgreSQL
    apt: name={{ item }} state=present
    with_items:

  • postgresql

  • postgresql-client

  • name: version of installed PostgeSQL server package
    shell: dpkg-query -W --showformat=‘${Version}’ postgresql | cut --delimiter=+ --fields=1
    register: pv
    changed_when: False

  • name: postgresql version
    set_fact:
    postgresql_version: “{{ pv.stdout }}”

  • name: postgresql configdir
    set_fact:
    postgresql_configdir: “/etc/postgresql/{{ postgresql_version }}/main”

But I find the dpkg-query|cut a bit ugly. Is there a better way?