title: Run a PostgreSQL script using Ansible
body: <p>I am looking for a way to run a Postgres script using Ansible. While I found a reasonably good example <a href="https://stackoverflow.com/questions/21160776/how-to-execute-a-shell-script-on-a-server-using-ansible">Here</a>, I need to:</p>
  
  <ul>
  <li>Run the script as user postgres</li>
  <li>I don't necessarily need to keep a copy of the script on the server so if I need to have a copy, it will only be for temp use.</li>
  </ul>
  
  <p>Can anyone tell me if this is possible and if so an example of running it. Here is what I tried so far using Ansible and it just hung at these points:</p>
  
  <pre><code> - name: Testing DB to make sure it is available
     command: psql -U bob image
     register: b
   - debug: b
  
   - name: Verifying Tables exist in Image
     shell: \d image
     register: c
   - debug: c
  
   - name: Exiting Image DB
     shell: \q
     register: d
   - debug: d
  
   - name: Going to Agent DB
     command: psql -U bob agent
     register: e
   - debug: e
  </code></pre>
  
  <p>This always hangs at the first part of it when logging into the image DB.</p>