Repository: ansible/ansible

Issue:
Become returns timeout with sudo on over local connection if the host is unable to resolve hostname

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
Ansible core

##### ANSIBLE VERSION
```
ansible 2.2.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
```

##### CONFIGURATION
Using all the default configuration values.

##### OS / ENVIRONMENT
N/A
##### SUMMARY

In case that your ansible managed host has assigned a hostname that cannot be resolved. Become (using sudo) returns timeout:
```
"timeout waiting for privilege escalation password prompt:\nsudo: sudo: unable to resolve host tcloud014\n[sudo via ansible, key=pwtuazmdorsylnkkwwykwrahkrydnwoo] password:
```

##### STEPS TO REPRODUCE

Any playbook that uses become (using the default sudo method) executed in a host whose hostname cannot be resolved will fail. This problem is relatively common in some cloud platforms when deploying virtual machines.

For example this simple test fails:
```yaml
- hosts: localhost
  connection: local
  become: yes
  tasks:
   - debug: msg=test
```

It seems that the privilege escalation method waits for a specific prompt in the sudo command and the unexpected "sudo: unable to resolve host" message causes the error.

##### EXPECTED RESULTS
```
PLAY [localhost] ************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [localhost]

TASK: [debug msg=asd] ********************************************************* 
ok: [localhost] => {
    "msg": "asd"
}

PLAY RECAP ******************************************************************** 
localhost                  : ok=2    changed=0    unreachable=0    failed=0   
```
##### ACTUAL RESULTS

```
PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "timeout waiting for privilege escalation password prompt:\nsudo: sudo: unable to resolve host tcloud014\n[sudo via ansible, key=pwtuazmdorsylnkkwwykwrahkrydnwoo] password: "}
	to retry, use: --limit @/home/ubuntu/test.retry

PLAY RECAP *********************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1   
```



Discussion:
I'm unable to replicate the behaviour you're talking about with sudo (not doing it in a cloud environment)... Do you know how to setup a vm so that this occurs?
needs_info
Was able to reproduce on an ubuntu16 vm by setting /etc/hostname to a non-domain name and restarting the box.
 Spoke too soon.  I'm able to reproduce the output from sudo by changing /etc/hostname on an ubuntu16 vm and rebooting but I'm not able to reproduce the ansible error:
```
$ cat 20858.yml                                                         *[5362910]  (12:58:16)
- hosts: localhost
  connection: local
  become: yes
  tasks:
  - debug: msg=test
$ ansible-playbook 20858.yml -K                                         *[5362910]  (12:55:21)
SUDO password: 
 [WARNING]: Host file not found: /etc/ansible/hosts

 [WARNING]: provided hosts list is empty, only localhost is available


PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "test"
}

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0
$ ansible-playbook --version                                            *[5362910]  (12:55:30)
ansible-playbook 2.2.1.0 (detached HEAD 5362910000) last updated 2017/02/01 12:55:21 (GMT -700)
  lib/ansible/modules/core: (stable-2.2 59c9a6d3c2) last updated 2017/02/01 12:52:33 (GMT -700)
  lib/ansible/modules/extras: (stable-2.2 c501e006ea) last updated 2017/02/01 12:53:09 (GMT -700)
  config file = 
  configured module search path = Default w/o overrides
```

Tried with both password and password-less sudo. 

Can you give us any other information about how to setup the environment to reproduce this problem?
And just for completeness, here's the output from raw sudo under these circumstances:
```
$ sudo whoami                                                                               (12:59:45)
sudo: unable to resolve host zzyx
[sudo] password for badger: 
root
```
Looks like I had my sudoers incorrectly configured for the passwordless case.  I can now reproduce.  Set up the hostname as above and set up passwordless sudo for the account.

Resolving patch:
diff --git a/lib/ansible/plugins/connection/__init__.py b/lib/ansible/plugins/connection/__init__.py
--- a/lib/ansible/plugins/connection/__init__.py
+++ b/lib/ansible/plugins/connection/__init__.py
@@ -254,7 +254,7 @@ def check_password_prompt(self, b_output):
             return False
         elif isinstance(self._play_context.prompt, string_types):
             b_prompt = to_bytes(self._play_context.prompt)
-            return b_output.startswith(b_prompt)
+            return b_prompt in b_output
         else:
             return self._play_context.prompt(b_output)
 


Test patch: