I created an ec2 instance with port 22 open and I am able to connect to this instance using the private key from my terminal, however, I am not able to connect it when I try ansible-playbook. I have been using dynamic inventory.

The command I use for SSH and which is successful too (I am running the below command where the private key is placed):

ssh -i "test-key.pem" ubuntu@ec2-x-x-x-x.eu-central-1.compute.amazonaws.com

Content for the playbook:

- name: Hello World example
  hosts: all
  become: true

    - name: Printing Hello World
          msg: "Hello World"

The command I ran:

ansible-playbook -i ec2.py --private-key /home/testuser/.ssh/test-key.pem -l instance_id playbook-hello.yml

even I tried with:

ansible-playbook -i ec2.py --private-key /home/testuser/.ssh/test-key.pem -l instance_id -e 'ansible_ssh_user=ubuntu' playbook-hello.yml


fatal: [x.x.x.x]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host x.x.x.x port 22: Operation timed out\r\n", "unreachable": true}

Other details:

I am running ansible-playbooks on my MacBook Pro. The EC2 instance is running Ubuntu OS 18.04.

When I run ./ec2.py - I can see my instance is getting printed. AWS Keys are saved in home directory .aws/credentials folder.

P.S: This question could be a duplicate of other questions available but I did not get any positive insight to match with my case.

  • 1
    Network Debug and Troubleshooting Guide might help you to move forward. Jul 21, 2019 at 8:40
  • A couple of questions: 1) Is "-l instance_id" needed when there is only one host? 2) Post the code of the playbook. 3) Who is running the playbook (whoami)? Are you able to connect the host e.g. ssh testuser@instance_id? Jul 21, 2019 at 8:45
  • Instance_id was given, somehow it was not shown here while putting the question, I am able to connect to the server using SSH and Private key (The normal way we use to connect ec2 instance). I think the 3rd thing which you mentioned is the cause here, I need to check that one and get back, but still, I am using the same user "ubuntu" to play the playbook so that should not be the issue. Because the same user is able to connect to ec2 instance with the private key. Jul 21, 2019 at 15:24
  • Put the additional info into the question instead of commenting. One more question: Why do you use dynamic inventory when you're not able to connect? Make the test case as simple as possible and make the question mcve. Jul 21, 2019 at 15:55
  • 1
    I have figured out the problem, I will post my answer soon. Jul 21, 2019 at 16:34

1 Answer 1


It was my bad to understand that the ec2.py script, which was used, only returning instances with private IPs. And because of that, it was not able to connect to the instance on port 22.

However, when I was doing ssh, I was using a Public IP or a DNS name.

So instead of using ec2.py, I executed the script using the public IP:

ansible-playbook -i a.b.c.d, playbook-hello-world.yml

a.b.c.d -- is a public IP of an instance.

If by any chance anyone gets a below error:

TASK [Gathering Facts] ***************************************************************************************************************************************
fatal: [a.b.c.d]: FAILED! => {"changed": false, "module_stderr": "Shared connection to a.b.c.d closed.\r\n", "module_stdout": "/bin/sh: 1: /usr/local/bin/python3: not found\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 127}

Please use the below command:

ansible-playbook -i a.b.c.d, -e 'ansible_python_interpreter=/usr/bin/python3' playbook-hello-world.yml

Just make sure, pythos3 is installed on the mentioned path of the instance, where you are trying to execute the playbook.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.