# Sudo support…  works!(?) in both ansible and ansible-playbook, testing needed

**URL:** <https://forum.ansible.com/t/sudo-support-works-in-both-ansible-and-ansible-playbook-testing-needed/11381>\
**Category:** Ansible Project\
**Created:** [March 30, 2012, 3:05am UTC](https://forum.ansible.com/t/sudo-support-works-in-both-ansible-and-ansible-playbook-testing-needed/11381 "2012-03-30T03:05:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [March 30, 2012, 3:05am UTC](https://forum.ansible.com/t/sudo-support-works-in-both-ansible-and-ansible-playbook-testing-needed/11381/1 "2012-03-30T03:05:26Z")

</div>

Ok, got this working in the latest git:

Usage (as your user account)

$ ansible all -m service -a “name=httpd state=restarted” -u username --sudo # or -s

From a playbook, just add a sudo: True bit and change the user line:

user: username  
sudo: True

The implementation is… creative.

Rather than deal with paramiko input and output streams over a pty/shell, which is … well, words I cannot use on a public mailing list how  
complicated … I am redirecting all output to a file, and then moving the file once the command execution is complete, and then using SFTP to wait until  
the “complete” file actually exists rather than rely on paramiko’s broken “exit code ready” logic and so forth. It’s dark magic, but it seems to work  
fine. Hopefully that’s true of everyone’s distributions. I’ll say it’s not super efficient now because it hammers the system saying "is it done yet, is it  
done yet, every 1 second. We could probably do some exponential back off if needed… as well as come up with a sane maximum timeout value  
after which we could rule the command dead… but I didn’t want to make yet another timeout parameter for that, and using the SSH timeout variable  
wouldn’t be right, and it’s not an async thing either.

Anyway, should work fine…

Please help test and let me know.

Thanks!

–Michael

---

<div class="post-metadata">

**Author:** ![Matthew\_Williams](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Matthew\_Williams](https://forum.ansible.com/u/Matthew_Williams)\
**Post date:** [March 30, 2012, 4:14am UTC](https://forum.ansible.com/t/sudo-support-works-in-both-ansible-and-ansible-playbook-testing-needed/11381/2 "2012-03-30T04:14:14Z")

</div>

My playbook is hanging at the setup phase. Here’s a snippet from my TB after Ctrl-C:

Process Process-4:  
Traceback (most recent call last):  
File “/usr/lib/python2.7/multiprocessing/process.py”, line 258, in \_bootstrap  
self.run()  
File “/usr/lib/python2.7/multiprocessing/process.py”, line 114, in run  
self.\_target(\*self.\_args, \*\*self.\_kwargs)  
File “/home/matthew/overlord/lib/ve/local/lib/python2.7/site-packages/ansible/runner.py”, line 55, in \_executor\_hook  
while not job\_queue.empty():  
File “”, line 2, in empty  
File “/usr/lib/python2.7/multiprocessing/managers.py”, line 755, in \_callmethod  
self.\_connect()  
File “/usr/lib/python2.7/multiprocessing/managers.py”, line 742, in \_connect  
conn = self.\_Client(self.\_token.address, authkey=self.\_authkey)  
File “/usr/lib/python2.7/multiprocessing/connection.py”, line 169, in Client  
c = SocketClient(address)  
File “/usr/lib/python2.7/multiprocessing/connection.py”, line 289, in SocketClient  
s.connect(address)  
File “/usr/lib/python2.7/socket.py”, line 224, in meth  
return getattr(self.\_sock,name)(\*args)  
error: [Errno 2] No such file or directory

---

<div class="post-metadata">

**Author:** ![Matthew\_Williams](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Matthew\_Williams](https://forum.ansible.com/u/Matthew_Williams)\
**Post date:** [March 30, 2012, 4:15am UTC](https://forum.ansible.com/t/sudo-support-works-in-both-ansible-and-ansible-playbook-testing-needed/11381/3 "2012-03-30T04:15:02Z")

</div>

(This is on Ubuntu Oneiric (11.10))

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [March 30, 2012, 11:14am UTC](https://forum.ansible.com/t/sudo-support-works-in-both-ansible-and-ansible-playbook-testing-needed/11381/4 "2012-03-30T11:14:26Z")

</div>

Unfortunately control-C messages are just that, control-C messages.  
They don't tell you anything and if I could hide them, I would,  
because they  
aren't really errors. You'd get them anyway from any control-C.

Run with "-T 5" for a really short SSH timeout.... it may be you have  
some legitimate host connections that need to timeout, such as host  
names that are offline  
or mis-entered.

Currently the code will also wait for the command to complete  
indefinitely, so you MIGHT be seeing that, i.e. command never wrote  
a result file because the module died. Adding some debug in  
connection.py to print the result\_file path and looking for the  
result\_file+"\_pre" file and seeing if anything is in it may be  
worthwhile in that case -- my setup module works fine over sudo.
