Issue
I have a playbook that creates an SSH key in a remote serverA that then copies it over another remote serverB.
I'm looking for a way to test the SSH connection from serverA to serverB, and then maybe run some command in serverB (for example uname -a
) to output it as a debug message that confirms the connection is working.
I've been looking around on the Internet and here as well, but I haven't found anything yet...
Any clue?
Solution
A quick approach would be to :
On Ansible's control node, use openssh_keypair to create an SSH keypair. Please pay attention to the path, to make sure the existent keypair is not overwritten.
Copy the keypair from Ansible's control node to serverA (make sure your set the right permssions for the files), use the copy module.
Copy the public-key (newly generated keypair) from Ansible's control node to serverB (make sure your set the right permssions for the file), and delete the source keypair.
Now, SSH keypairs setup is ready between serverA & serverB.
Run command module, on the serverA and register it's result e.g:
- name: Create variable from command
command: ssh -o StrictHostKeyChecking=no user@serverB 'some_command'
register: command_output
Print out the out the output of the registered result:
- debug: msg="{{ command_output.stdout }}"
Answered By - senjoux