# Execut script with Ansible

**URL:** <https://forum.ansible.com/t/execut-script-with-ansible/32074>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [January 28, 2020, 11:32am UTC](https://forum.ansible.com/t/execut-script-with-ansible/32074 "2020-01-28T11:32:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![P\_Varsha](https://avatars.discourse-cdn.com/v4/letter/p/e274bd/32.png) [@P\_Varsha](https://forum.ansible.com/u/P_Varsha)\
**Post date:** [January 28, 2020, 11:32am UTC](https://forum.ansible.com/t/execut-script-with-ansible/32074/1 "2020-01-28T11:32:35Z")

</div>

can you please share what actually error log you are getting.

---

<div class="post-metadata">

**Author:** ![Karther](https://avatars.discourse-cdn.com/v4/letter/k/3ec8ea/32.png) [@Karther](https://forum.ansible.com/u/Karther)\
**Post date:** [January 28, 2020, 4:13pm UTC](https://forum.ansible.com/t/execut-script-with-ansible/32074/2 "2020-01-28T16:13:03Z")

</div>

Hello Varsha,

For exemple if in my script sql “real.sql” there is commande line toto  
I have this error :

Traceback (most recent call last):  
File “./script\_sql\_4.py”, line 19, in   
cur.execute(f.read().decode(‘utf-8’))  
File “/usr/lib/pymodules/python2.7/pymssql.py”, line 196, in execute  
raise OperationalError, e[0]  
pymssql.OperationalError: SQL Server message 2812, severity 16, state 62, line 1:  
Could not find stored procedure ‘toto’.  
DB-Lib error message 2812, severity 16:  
General SQL Server error: Check messages from the SQL Server

I want get statut of my command “**cur.execute(f.read().decode(‘utf-8’))**” in python after this command is execute  
for after get log message error with this value.

Someone have any idea please ?

Thanks very much Community Ansible !! 🙂

Best Regards,

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 28, 2020, 4:54pm UTC](https://forum.ansible.com/t/execut-script-with-ansible/32074/3 "2020-01-28T16:54:26Z")

</div>

> Hello Varsha,
> 
> For exemple if in my script sql "real.sql" there is commande line toto  
> I have this error :
> 
> Traceback (most recent call last):  
> File "./script\_sql\_4.py", line 19, in \<module\>  
> cur.execute(f.read().decode('utf-8'))  
> File "/usr/lib/pymodules/python2.7/pymssql.py", line 196, in execute  
> raise OperationalError, e[0]  
> pymssql.OperationalError: SQL Server message 2812, severity 16, state 62, line 1:  
> Could not find stored procedure 'toto'.  
> DB-Lib error message 2812, severity 16:  
> General SQL Server error: Check messages from the SQL Server
> 
> I want get statut of my command "\*cur.execute(f.read().decode('utf-8'))\*" in python after this command is execute  
> for after get log message error with this value.

So you want the error message from your script in your Ansible playbook? How does your task looks like right now?

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Karther](https://avatars.discourse-cdn.com/v4/letter/k/3ec8ea/32.png) [@Karther](https://forum.ansible.com/u/Karther)\
**Post date:** [January 29, 2020, 8:22am UTC](https://forum.ansible.com/t/execut-script-with-ansible/32074/4 "2020-01-29T08:22:59Z")

</div>

Hello,

Yes my task is this :

- name: “Execut script python”  
shell: ./myscript.py  
delegate\_to: localhost

I want to get statut of my command line in my script for get message error.

Thanks very much Community Ansible 🙂

Best Regards,

Karther

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 29, 2020, 9:06am UTC](https://forum.ansible.com/t/execut-script-with-ansible/32074/5 "2020-01-29T09:06:05Z")

</div>

> Hello,
> 
> Yes my task is this :
> 
> - name: "Execut script python"  
> shell: ./myscript.py  
> delegate\_to: localhost
> 
> I want to get statut of my command line in my script for get message error.

Please try this (untested):

- name: "Execut script python"  
&nbsp;&nbsp;shell: ./myscript.py  
&nbsp;&nbsp;register: myscript  
&nbsp;&nbsp;delegate\_to: localhost  
&nbsp;&nbsp;failed\_when: false

- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ myscript[stderr\_lines] }}"

The failed\_when condition is necessary to reach the debug task.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Karther](https://avatars.discourse-cdn.com/v4/letter/k/3ec8ea/32.png) [@Karther](https://forum.ansible.com/u/Karther)\
**Post date:** [January 29, 2020, 9:49am UTC](https://forum.ansible.com/t/execut-script-with-ansible/32074/6 "2020-01-29T09:49:09Z")

</div>

Hello,

Yes this register variable error works but i want to get status of execution of my python programme **cur.execute** :  
I want to know from this programme python if **cur.execute** is success execute or not …

**#!/usr/bin/python**

**import pymssql**

**conn = pymssql.connect(host=“ihospisql02”, user=“sa\_ofs”, password=“#Adm!n0fS1”, database=“my\_database”)**  
**cur = conn.cursor()**

**with open(‘real.sql’) as f:**  
**cur.execute(f.read().decode(‘utf-8’))**  
**conn.commit()**

**cur.close()**

Do you understand what i want please Community Ansible ?

Thanks very much

Best Regards,

Karther

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 29, 2020, 9:56am UTC](https://forum.ansible.com/t/execut-script-with-ansible/32074/7 "2020-01-29T09:56:03Z")

</div>

> Hello,
> 
> Yes this register variable error works but i want to get status of execution of my python programme \*cur.execute\* :  
> I want to know from this programme python if \*cur.execute\* is success execute or not ...

That's up to your script, really. In case \*cur.execute\* fails it should print the error to STDERR and exit  
with exit code != 0.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
