title: Port 22 Connection Refused when trying to ssh between two AWS instances using Ansible
body: <pre><code> TASK [SSH connection between the ansible server and the new server] *************************************************************
  fatal: [localhost]: FAILED! =&gt; 
  {&quot;changed&quot;: true, &quot;cmd&quot;: &quot;cat ~/.ssh/id_rsa.pub | ssh -vvv -p 22 -i ~/.ssh/my.key ubuntu@xx.xx.xxx.xx \&quot;cat &gt;&gt; ~/.ssh/authorized_keys\&quot;&quot;,
  &quot;delta&quot;: &quot;0:00:03.052818&quot;, &quot;end&quot;: &quot;2021-10-04 05:57:48.631244&quot;, &quot;msg&quot;: &quot;non-zero return code&quot;, 
  &quot;rc&quot;: 255, &quot;start&quot;: &quot;2021-10-04 05:57:45.578426&quot;, &quot;stderr&quot;: &quot;OpenSSH_8.2p1 Ubuntu-4ubuntu0.3, OpenSSL 1.1.1f  
  31 Mar 2020\r\ndebug1: Reading configuration data /root/.ssh/config\r\ndebug1: 
  Reading configuration data /etc/ssh/ssh_config\r\ndebug2: resolve_canonicalize: hostname  
  xx.xx.xxx.xx   is address\r\ndebug2: 
  ssh_connect_direct\r\ndebug1: Connecting to  xx.xx.xxx.xx [ xx.xx.xxx.xx ] port 22.\r\ndebug1: 
  connect to address  xx.xx.xxx.xx   port 22: Connection refused\r\nssh: connect to host  xx.xx.xxx.xx   
  port 22: Connection refused&quot;, 
  &quot;stderr_lines&quot;: [&quot;OpenSSH_8.2p1 Ubuntu-4ubuntu0.3, OpenSSL 1.1.1f  31 Mar 2020&quot;, &quot;debug1: 
  Reading configuration data /root/.ssh/config&quot;, &quot;debug1: Reading configuration data /etc/ssh/ssh_config&quot;, 
  &quot;debug2: resolve_canonicalize: hostname  xx.xx.xxx.xx   is address&quot;,&quot;debug2: ssh_connect_direct&quot;, 
  &quot;debug1: Connecting to  
  xx.xx.xxx.xx [ xx.xx.xxx.xx  ] port 22.&quot;, &quot;debug1: connect to address  xx.xx.xxx.xx   port 22: Connection refused&quot;, &quot;ssh: connect to host  xx.xx.xxx.xx   port 22: Connection refused&quot;], &quot;stdout&quot;: &quot;&quot;, &quot;stdout_lines&quot;: []}
      
  
  Here  &quot;xx.xx.xxx.xx&quot; means my newly created server's public ip.
  </code></pre>
  <p>I'm trying to copy the public key from ansible-server to a newly created server using ansible, which is working manually, but the automation through ansible is creating this issue and blocking me to perform the other tasks which are in the playbook.</p>
  <p>Any help would be appreciated.</p>
  <p>Thankyou in advance.</p>