title: ansible - how to check success of importing sql file into postgres
body: <p>I'm trying to crash course myself on ansible... and I've run into a scenario where I need to import a file into postgres.  The postgres module for ansible doesn't have all the commands that the mysql module does... so I've had to find an alternative way to run sql commands against the db.</p>
  
  <p>I'm using the shell command.  However, I don't know how to check if the shell command was successful or not.</p>
  
  <p>Here's what my playbook looks like so far: </p>
  
  <pre><code>- hosts: webservers
    tasks:
    - block:
      - debug: msg='Start sql insert play...'
      - copy: src=file.dmp dest=/tmp/file.dmp
      - debug: msg='executing sql file...'
      - shell: psql -U widgets widgets &lt; /tmp/file.dmp
      - debug: msg='all is well'
        when: result|succeeded
      rescue:
        - debug: msg='Error'
      always:
        - debug: msg='End of Play'
   # - block:
   #   - name: restart secondarywebservers
   #   - debug: msg='Attempting to restart secondary servers'
   #   - hosts: websecondaries
  </code></pre>
  
  <p>What I ultimately want to do is start the second block of code only when the first block has been successful. For now, just to learn how conditionals work, I'm trying to see if I can print a message to screen when i know for sure the sql file executed.</p>
  
  <p>It fails with the following error message: </p>
  
  <pre><code>TASK [debug] *******************************************************************
  fatal: [10.1.1.109]: FAILED! =&gt; {"failed": true, "msg": "The conditional check 'result|succeeded' failed. The error was: |failed expects a dictionary\n\nThe error appears to have been in '/etc/ansible/playbooks/dbupdate.yml': line 9, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n    - shell: psql -U openser openser &lt; /tmp/file.dmp\n    - debug: msg='all is well'\n      ^ here\n"}
  </code></pre>
  
  <p>I'm still doing some research on how conditionals work... so it could be that my syntax is just wrong.  But the bigger question is this - Since there is no native ansible / postgresql import command, I realize that there's no way for ansible to know that the commands in "file.dmp" really created my database records...
  I could add a select statement in file.dmp to try to select the record I just created... but how do i pass that back to ansible?</p>
  
  <p>Just wondering if someone has some ideas on how I could accomplish something like this.</p>
  
  <p>Thanks. </p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>Here's what the contents of the test "file.dmp" contains: </p>
  
  <pre><code>INSERT INTO mytable VALUES (DEFAULT, 64, 1, 1, 'test', 0, '^(.*)$', '\1);
  </code></pre>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I'm going to try to do something like this:</p>
  
  <p>Copy (Select * From mytable where ppid = 64) To '/tmp/test.csv' With CSV;</p>
  
  <p>after the insert statements... and then have ansible check for this file (possibly using the lineinfile command) as a way to prove to myself that the insert worked.</p>