title: Ansible-Failed to connect to the host via ssh
body: <p>I am trying to provison an EC2 instance and to install a LAMP server on it using Ansible from localhost. I have successfully provisioned the instance, but I was not able to install apache,php and mysql due to this error "Failed to connect to the host via ssh.". </p>
  
  <p>OS: El Capitan 10.11.6</p>
  
  <p>Ansible: 2.0.2.0</p>
  
  <p>Here is the playbook: `---</p>
  
  <pre><code>- hosts: localhost
    connection: local
    gather_facts: no
    vars_files:
      - "vars/{{ project_name }}.yml"
      - "vars/vpc_info.yml"
    tasks:
  - name: Provision
    local_action:
      module: ec2
      region: "xxxxxx"
      vpc_subnet_id: "xxxxxx"
      assign_public_ip: yes
      key_name: "xxxxxxx"
      instance_type: "t2.nano"
      image: "xxxxxxxx"
  
      wait: yes
      instance_tags:
        Name: "LAMP"
        class: "test"
        environment: "dev"
        project: "{{ project_name }}"
        az: a
      exact_count: 1
      count_tag:
        Name: "LAMP"
      monitoring: yes
    register: ec2a
  
  - hosts: lamp
    roles:
      - lamp_server
  </code></pre>
  
  <p>The content of the ansible.cfg file:</p>
  
  <pre><code>[defaults]
  private_key_file=/Users/nico/.ssh/xxxxx.pem
  </code></pre>
  
  <p>The inventory:</p>
  
  <pre><code>lamp     ansible_ssh_host=&lt;EC2 IP&gt; ansible_user=ubuntu
  </code></pre>
  
  <p>The command used for running the playbook:</p>
  
  <pre><code>ansible-playbook  -i inventory  ec2_up.yml -e project_name="lamp_server"  -vvvv 
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code> ESTABLISH SSH CONNECTION FOR USER: ubuntu
  &lt;xxxxxxxxxx&gt; SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile="/Users/nico/.ssh/xxxxxxx.pem"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 -o ControlPath=/Users/nico/.ansible/cp/ansible-ssh-%h-%p-%r xxxxxxx '/bin/sh -c '"'"'( umask 22 &amp;&amp; mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1475186461.08-93383010782630 `" &amp;&amp; echo "` echo $HOME/.ansible/tmp/ansible-tmp-1475186461.08-93383010782630 `" )'"'"''
  52.28.251.117 | UNREACHABLE! =&gt; {
      "changed": false,
      "msg": "Failed to connect to the host via ssh.",
      "unreachable": true
  }
  </code></pre>
  
  <p>I have read a lot of threads regarding this error, but nothing helped me. :( </p>