I am newbie to ansible. I am trying to execute two different task. 
1. checking passwdless login. executing hostname and id command in the 
remote host and printing its result when succcess.
2. executing ssh-copy-id and copying pub key to the remote host

I want to execute the second task, only if the first task failed. 

Please let me know the mistake I am doing here.

On Wednesday, 11 December 2019 14:51:53 UTC+5:30, Ganeshwari Sk wrote:
>
> in Ansible playbook, I want to do two task, one to check passwdless ssh 
> login is enabled or not, and in another task if passwdless login not abled 
> for the remote host, then execute the ssh-copy-id command.
> I have written the following playbook, but it is not working as desired, 
> please help me to resolve the issue.
>
> ---
> - name: Check passwdless login
>   #hosts: stage-servers
>   hosts: Testing-Server
>   gather_facts: true
>    
>   tasks:
>     - name: check ssh to remote hosts works
>       block:
>         - name: check ssh connection
>           shell: "hostname; id"
>           register: ssh_connection_test
>           failed_when: ssh_connection_test.rc != 0
>
>         - debug:
>             var: ssh_connection_test.stdout_lines
>
>       rescue:
>         - debug:
>            msg: "failed for host 
> {{hostvars[inventory_hostname]['ansible_default_ipv4']['address']}}"
>         - name: Task failed, no passdwless ssh working
>           shell: "ssh-copy-id -o StrictHostKeyChecking=no -i 
> ~/.ssh/id_rsa.pub \"root@{{ 
> hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }} \""
>           register: ssh_copy_id_execution
>           failed_when: ssh_copy_id_execution.rc != 0
>
>         - debug :
>              var: ssh_copy_id_execution.stdout_lines
>
>       always:
>         - debug:
>              msg: "This always executed.."
>
>
> *I am getting this output:*
> LAY [Check passwdless login] 
> ******************************************************************************************************************************************************************************
>
> TASK [Gathering Facts] 
> *************************************************************************************************************************************************************************************
> fatal: [x.x.x.x]: UNREACHABLE! => {"changed": false, "msg": "Failed to 
> connect to the host via ssh: Address x.x.x.x maps to test.testlab.com, 
> but this does not map back to the address - POSSIBLE BREAK-IN 
> ATTEMPT!\r\nPermission denied 
> (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n", "unreachable": 
> true}
> to retry, use: --limit @/etc/ansible/passwordless.retry
>
> PLAY RECAP 
> *************************************************************************************************************************************************************************************************
> x.x.x.x                 : ok=0    changed=0    unreachable=1    failed=0   
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/b81ceb8a-f88a-49af-bfd1-fe553ba27541%40googlegroups.com.

Reply via email to