This ansible playbook is not exactly bullet-proof and production ready but it shows nicely how powerful ansible is.

---

# this playbook does reconfiguration of postgresql
# first it finds location of postgresql.conf and pg_hba.conf files
# this operation works even when you get "peer authentication" error when trying to access psql
# second it makes changes in both files
# and restarts postgresql service at the end
 
- name: check pg files
  hosts: local-virtual
  tasks:
  - name: can we access psql
    command: "which psql"
    ignore_errors: true
    register: find_psql_output

  - name: debug find psql output
    debug: var=find_psql_output.stdout

  - name: find main config file
    command: bash -c "sudo -u postgres psql -U postgres -d postgres -t -c 'show config_file' | sed 's/ //g' "
    sudo: yes
    ignore_errors: true
    register: pg_main_conf

  - name: was main config file found
    debug: var=pg_main_conf.stdout

  - name: find hba file
    command: bash -c "sudo -u postgres psql -U postgres -d postgres -t -c 'show hba_file' | sed 's/ //g' "
    sudo: yes
    ignore_errors: true
    register: pg_hba_conf

  - name: was main config file found
    debug: var=pg_hba_conf.stdout

  - name: postgresql should listen on all ports
    lineinfile: dest="{{ pg_main_conf.stdout }}"
                regexp="^listen_addresses"
                line="listen_addresses = '*'" state=present
    sudo: yes

  - name: postgresql should allow access to host
    copy:
      dest: "{{ pg_hba_conf.stdout }}"
      content: |
        local   all   postgres   trust 
        local   all   all        trust
        host    all   all        0.0.0.0/0   md5
    sudo: yes 

  - name: restart postgresql
    service: name=postgresql state=restarted
    sudo: yes