ansible wait_for ssh for a specific user

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.

Hi,

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

thanks,
Guilherme.

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 ...

Reza,

Could you run with -vvv at the end?

Thanks,
Guilherme.

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’

Reza,

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

Guilherme.

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