# ansible wait\_for ssh for a specific user

**URL:** <https://forum.ansible.com/t/ansible-wait-for-ssh-for-a-specific-user/23234>\
**Category:** Ansible Project\
**Created:** [December 12, 2016, 10:39am UTC](https://forum.ansible.com/t/ansible-wait-for-ssh-for-a-specific-user/23234 "2016-12-12T10:39:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zerros](https://avatars.discourse-cdn.com/v4/letter/z/9de0a6/32.png) [@zerros](https://forum.ansible.com/u/zerros)\
**Post date:** [December 12, 2016, 10:39am UTC](https://forum.ansible.com/t/ansible-wait-for-ssh-for-a-specific-user/23234/1 "2016-12-12T10:39:25Z")

</div>

Hi, forgive me for my bad english.

I’m trying to find a way to test an ssh connection to a specific user on a new server.

When I create a new virtual server, an ansible playbook is executed to finish the soft deployments:  
1 Check if ssh is available (port 22)  
2 Chekc if the ansible user is available  
3 Get the uname  
4 Install packages

I have no problem for 1, 3 and 4:

- name: “waiting for host to start”  
local\_action: wait\_for  
host={{ inventory\_hostname }}  
state=started  
port=22  
delay=10  
become: false

- name: “waiting to connect to xxxx user”  
local\_action: command ssh -i ~/.ssh/mykey -o ConnectTimeout=5 -o ConnectionAttempts=1 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o GSSAPIAuthentication=no -o GSSAPIKeyExchange=no -l xxxxx {{inventory\_hostname}} exit  
register: result  
until: result.rc == 0  
retries: 20  
delay: 5

- name: “waiting the uname”  
command: uname -a  
register: result

- name: “uname”  
debug:  
var: result.stdout

but the second step is never OK. When the playbook execute this step, ansible execute the command, but never exit from this command. I have to kill the process of this command to force ansible to re-run a new test.  
Finally when the server and user is available, after a new kill, ansible can continue with step 3.

How can I resolve this problem. I can add a sleep before the step 2, but it is not a good solution. Thanks in advance for your helps.

---

<div class="post-metadata">

**Author:** ![Guilherme\_Ueno](https://avatars.discourse-cdn.com/v4/letter/g/58f4c7/32.png) [@Guilherme\_Ueno](https://forum.ansible.com/u/Guilherme_Ueno)\
**Post date:** [December 12, 2016, 12:58pm UTC](https://forum.ansible.com/t/ansible-wait-for-ssh-for-a-specific-user/23234/2 "2016-12-12T12:58:30Z")

</div>

Hi,

Did you already try to use “shell” instead “command”? When you try to execute that command from another machine it works?

thanks,  
Guilherme.

---

<div class="post-metadata">

**Author:** ![Reza\_ISSANY](https://avatars.discourse-cdn.com/v4/letter/r/edb3f5/32.png) [@Reza\_ISSANY](https://forum.ansible.com/u/Reza_ISSANY)\
**Post date:** [December 12, 2016, 3:25pm UTC](https://forum.ansible.com/t/ansible-wait-for-ssh-for-a-specific-user/23234/3 "2016-12-12T15:25:41Z")

</div>

yess I've already tried shell butsame issue. If I try to connect  
manually, it works. The problem is that ansible doesn't release the  
first connection attemps while the server isn't ready. I don't know  
why ...

---

<div class="post-metadata">

**Author:** ![Guilherme\_Ueno](https://avatars.discourse-cdn.com/v4/letter/g/58f4c7/32.png) [@Guilherme\_Ueno](https://forum.ansible.com/u/Guilherme_Ueno)\
**Post date:** [December 12, 2016, 4:18pm UTC](https://forum.ansible.com/t/ansible-wait-for-ssh-for-a-specific-user/23234/4 "2016-12-12T16:18:43Z")

</div>

Reza,

Could you run with -vvv at the end?

Thanks,  
Guilherme.

---

<div class="post-metadata">

**Author:** ![Reza\_ISSANY](https://avatars.discourse-cdn.com/v4/letter/r/edb3f5/32.png) [@Reza\_ISSANY](https://forum.ansible.com/u/Reza_ISSANY)\
**Post date:** [December 12, 2016, 6:28pm UTC](https://forum.ansible.com/t/ansible-wait-for-ssh-for-a-specific-user/23234/5 "2016-12-12T18:28:37Z")

</div>

I can’t reproduce the same run. Actually, ansible ask me to provide a password to connect. I don’t know why.  
Ansible has to wait until he can connect using the command I provide no ?

2016-12-12 19:23:31,939 p=12608 u=deploy | Using ansible.cfg as config file  
2016-12-12 19:23:32,048 p=12608 u=deploy | PLAYBOOK: main.yml \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
2016-12-12 19:23:32,048 p=12608 u=deploy | 1 plays in main.yml  
2016-12-12 19:23:32,051 p=12608 u=deploy | PLAY [all] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
2016-12-12 19:23:32,095 p=12608 u=deploy | TASK [wait-for-available : waiting for host to start] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
2016-12-12 19:23:32,095 p=12608 u=deploy | task path: /ansible/roles/wait-for-available/tasks/main.yml:1  
2016-12-12 19:23:32,265 p=12608 u=deploy | Using module file /ansible/modules/utilities/logic/wait\_for.py  
2016-12-12 19:23:57,413 p=12608 u=deploy | ok: [192.168.0.57 → localhost] =\> {  
“changed”: false,  
“elapsed”: 25,  
“invocation”: {  
“module\_args”: {  
“connect\_timeout”: 5,  
“delay”: 10,  
“exclude\_hosts”: null,  
“host”: “192.168.0.57”,  
“path”: null,  
“port”: 22,  
“search\_regex”: null,  
“sleep”: 1,  
“state”: “started”,  
“timeout”: 300  
},  
“module\_name”: “wait\_for”  
},  
“path”: null,  
“port”: 22,  
“search\_regex”: null,  
“state”: “started”  
}  
2016-12-12 19:23:57,415 p=12608 u=deploy | TASK [wait-for-available : waiting to connect to ansible user] \*\*\*\*\*\*\*\*\*\*\*\*\*  
2016-12-12 19:23:57,415 p=12608 u=deploy | task path: /ansible/roles/wait-for-available/tasks/main.yml:10  
2016-12-12 19:23:57,567 p=12608 u=deploy | Using module file /ansible/modules/commands/command.py  
2016-12-12 19:25:03,496 p=12608 u=deploy | [ERROR]: User interrupted execution

- name: “waiting for host to start”  
local\_action: wait\_for  
host={{ inventory\_hostname }}  
state=started  
port=22  
delay=10  
become: false  
when: ostype == ‘Linux’

- name: “waiting to connect to ansible user”  
local\_action: command ssh -vvv -i ~/.ssh/deploy -o ConnectTimeout=5 -o ConnectionAttempts=1 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o GSSAPIAuthentication=no -o GSSAPIKeyExchange=no -l ansible {{inventory\_hostname}} exit  
register: result  
until: result.rc == 0  
retries: 20  
delay: 5  
when: ostype == ‘Linux’

- name: “waiting the uname”  
command: uname -a  
register: result  
when: ostype == ‘Linux’

- name: “uname”  
debug:  
var: result.stdout  
when: ostype == ‘Linux’

---

<div class="post-metadata">

**Author:** ![Guilherme\_Ueno](https://avatars.discourse-cdn.com/v4/letter/g/58f4c7/32.png) [@Guilherme\_Ueno](https://forum.ansible.com/u/Guilherme_Ueno)\
**Post date:** [December 12, 2016, 7:12pm UTC](https://forum.ansible.com/t/ansible-wait-for-ssh-for-a-specific-user/23234/6 "2016-12-12T19:12:32Z")

</div>

Reza,

You have to put the parameter at the end of command, like: ansible-playbook -i inventory… -vvv

Guilherme.

---

<div class="post-metadata">

**Author:** ![Reza\_ISSANY](https://avatars.discourse-cdn.com/v4/letter/r/edb3f5/32.png) [@Reza\_ISSANY](https://forum.ansible.com/u/Reza_ISSANY)\
**Post date:** [December 12, 2016, 7:19pm UTC](https://forum.ansible.com/t/ansible-wait-for-ssh-for-a-specific-user/23234/7 "2016-12-12T19:19:28Z")

</div>

i have add the vvv to ansible and to ssh command. The log provided comes from this run. I don’t have more logs. I will try tomorrow to make a new run just with the wait task.

reza
