error query sqlcmd

121 views
Skip to first unread message

Christovam Paynes Silva

unread,
Apr 17, 2018, 7:49:06 PM4/17/18
to Ansible Project

Hi, okay?

I'm new to ansible, so understand my rookie mistakes, rsrsrs. I'm having an error interpreting my query. From what I realized the problem is in the comma, when you get into it the Ansible gives the error below.
After adjusting this, I want to work with Jinja2 in the query.

What do I need to do to execute this query correctly?



ansible-playbook test.yml

---
- name: Dump Users
  hosts: ansible
  tasks:
    - set_fact:
        sqlscript1: "{{ lookup('template', 'sql.j2') }}"
    - name: Dumps Users
      command: sqlcmd -S 192.168.10.2 -d DATABASE1 -U sa -P "password" -Q "{{ sqlscript1 }}"
      register: result_login
      
    - debug: 
          msg: "{{ result_login.stdout }}"


sql.j2:

SELECT DISTINCT PPOA.CODUSU
FROM SMATRICPL,
     SAL,
     PPOA
WHERE SMATRICPL.CODCOL = 1
  AND SMATRICPL.CODSTATUS = 5 
        (SELECT DISTINCT PPOA.CODUSU
    FROM PPOA,
   PFUNC
WHERE PFUNC.CODPOA = PPOA.COD AND 
   PFUNC.CODR IN ('P','M','D') AND ... etc





Result:

PLAY [Dump Users] ********************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************
ok: [192.168.10.15]

TASK [set_fact] *******************************************************************************************************************************************
ok: [192.168.10.15]

TASK [Dump users] ******************************************************************************************************************************
changed: [192.168.10.15]

TASK [debug] **********************************************************************************************************************************************
ok: [192.168.10.15] => {
    "msg": "Msg 102, Level 15, State 1, Server SRVBD2, Line 26\nIncorrect syntax near 'SMATRICP'."
}

PLAY RECAP ************************************************************************************************************************************************
192.168.10.15             : ok=4    changed=1    unreachable=0    failed=0   



Thanks




Message has been deleted

Christovam Paynes Silva

unread,
Apr 17, 2018, 8:07:34 PM4/17/18
to Ansible Project
If you remove all the indentation (spaces, new lines), it works.
If you remove a portion of the indentation, errors occur on other lines, such as
"Msg": "Msg 102, Level 15, State 1, Server SRVBD2, Line 22 \ nIncorrect syntax near 'CODPOA'."

Is there a way to leave the query indented?
Reply all
Reply to author
Forward
0 new messages