aws ec2 instance Permission denied (publickey) after created

531 views
Skip to first unread message

Li Zheng

unread,
Apr 13, 2017, 12:45:35 PM4/13/17
to Ansible Project
I am following ec2 module for Ansible example on link  http://docs.ansible.com/ansible/ec2_module.html


instance is created correctly. However, during Configure instance Stage I tried to install apache. It return an error
fatal: [52.90.207.199]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Warning: Permanently added.207.199' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey).\r\n", "unreachable": true}

# Launch instances, runs some tasks
# and then terminate them

- name: Create a sandbox instance
  hosts: localhost
  gather_facts: False
  vars:
    key_name: my_keypair
    instance_type: m1.small
    security_group: my_securitygroup
    image: my_ami_id
    region: us-east-1
  tasks:
    - name: Launch instance
      ec2:
         key_name: "{{ keypair }}"
         group: "{{ security_group }}"
         instance_type: "{{ instance_type }}"
         image: "{{ image }}"
         wait: true
         region: "{{ region }}"
         vpc_subnet_id: subnet-29e63245
         assign_public_ip: yes
      register: ec2

    - name: Add new instance to host group
      add_host:
        hostname: "{{ item.public_ip }}"
        groupname: launched
      with_items: "{{ ec2.instances }}"

    - name: Wait for SSH to come up
      wait_for:
        host: "{{ item.public_dns_name }}"
        port: 22
        delay: 60
        timeout: 320
        state: started
      with_items: "{{ ec2.instances }}"

- name: Configure instance(s)
  hosts: launched
  become: True
  gather_facts: True
  tasks:
    - name: install packages (apache2, git)
      apt: name={{ item }} update_cache=yes state=latest
      with_items:
        - apache2
        - git

Anderson Goulart

unread,
Apr 14, 2017, 6:10:56 AM4/14/17
to ansible...@googlegroups.com
Hi,

Ansible is trying to connect (ssh) to the instance with a different user/key that you used to launch the instance. Check for ansible_ssh_private_key_file 



// Anderson

--
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 ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f5e7c789-1e9c-4ee8-8c10-41fb53ed2886%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sudhanshu Sharma

unread,
Apr 17, 2017, 3:05:31 PM4/17/17
to Ansible Project
Try after deleteing /.ssh/Known_Hosts file.
Reply all
Reply to author
Forward
0 new messages