2016-04-06 67 views
6

我想在Ansible的幫助下創建和配置Amazon EC2計算機。 現在,我得到以下錯誤:Ansible Amazon EC2。密鑰對不存在

fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "Instance creation failed => InvalidKeyPair.NotFound: The key pair '~/.keys/EC2-Kibi-Enterprise-Deployment.pem' does not exist"} 

但.PEM項是否存在:

$ ls -lh ~/.keys/EC2-Kibi-Enterprise-Deployment.pem 
-r-------- 1 sergey sergey 1.7K Apr 6 09:56 /home/sergey/.keys/EC2-Kibi-Enterprise-Deployment.pem 

,它是在歐盟(愛爾蘭)區創建。

這裏是我的劇本:

-- 
- name: Setup servers on Amazon EC2 machines 
    hosts: localhost 
    gather_facts: no 

    tasks: 
    - include_vars: group_vars/all/ec2_vars.yml 

    ### Create Amazon EC2 instances 
    - name: Amazon EC2 | Create instances 
     ec2: 
     count: "{{ count }}" 
     key_name: "{{ key }}" 
     region: "{{ region }}" 
     zone: "{{ zone }}" 
     group: "{{ group }}" 
     instance_type: "{{ machine }}" 
     image: "{{ image }}" 
     wait: true 
     wait_timeout: 500 
     #vpc_subnet_id: "{{ subnet }}" 
     #assign_public_ip: yes 
     register: ec2 

    - name: Amazon EC2 | Wait for SSH to come up 
     wait_for: 
     host: "{{ item.public_ip }}" 
     port: 22 
     delay: 10 
     timeout: 60 
     state: started 
     with_items: "{{ ec2.instances }}" 

    - name: Amazon EC2 | Add hosts to the kibi_servers in-memory inventory group 
     add_host: hostname={{ item.public_ip }} groupname=kibi_servers 
     with_items: "{{ ec2.instances }}" 
    ### END 

### Provision roles 
- name: Amazon EC2 | Provision new instances 
    hosts: kibi_servers 
    become: yes 
    roles: 
    - common 
    - java 
    - elasticsearch 
    - logstash 
    - nginx 
    - kibi 
    - supervisor 
### END 

而且我var文件:

count: 2 
region: eu-west-1 
zone: eu-west-1a 
group: default 
image: ami-d1ec01a6 
machine: t2.medium 
subnet: subnet-3a2aa952 
key: ~/.keys/EC2-Kibi-Enterprise-Deployment.pem 

什麼是錯在這裏.pem文件?

回答

10

ec2 module的參數key正在尋找已上傳到AWS的密鑰對名稱,而不是本地密鑰。

如果你想讓Ansible上傳公鑰,你可以使用ec2_key module

所以,你的劇本是這樣的:

-- 
- name: Setup servers on Amazon EC2 machines 
    hosts: localhost 
    gather_facts: no 

    tasks: 
    - include_vars: group_vars/all/ec2_vars.yml 

    ### Create Amazon EC2 key pair 
    - name: Amazon EC2 | Create Key Pair 
     ec2_key: 
     name: "{{ key_name }}" 
     region: "{{ region }}" 
     key_material: "{{ item }}" 
     with_file: /path/to/public_key.id_rsa.pub 

    ### Create Amazon EC2 instances 
    - name: Amazon EC2 | Create instances 
     ec2: 
     count: "{{ count }}" 
     key_name: "{{ key_name }}" 
     ... 
+0

我是否需要在本地創建SSH密鑰對並在亞馬遜控制檯中導入公鑰?這是關鍵嗎? – trex

+0

您需要將公鑰(不是私有部分)上傳到AWS。您可以通過控制檯執行此操作,也可以按照示例通過Ansible執行此操作。 – ydaetskcoR

+0

似乎'ec2_keypair'模塊不存在。但有[ec2_key](http://docs.ansible.com/ansible/ec2_key_module.html)模塊。我使用它,並且在var文件中還指出了'key_name:〜/ .ssh/EC2-Kibi-Enterprise'。現在我有以下錯誤:'失敗:[localhost] =>(item = ssh-rsa AA ...「,」msg「:」必須指定region或ec2_url「}' – trex

2

該解決方案已被發現。當您爲.pem密鑰文件放置完整路徑時,EC2不喜歡。

所以,我搬到EC2-Kibi-Enterprise-Deployment.pem~/.ssh,用它添加到認證代理與ssh-add

ssh-add ~/.ssh/EC2-Kibi-Enterprise-Deployment.pem 

並糾正了重點線,我var文件到
key: EC2-Kibi-Enterprise-Deployment.pem

同樣的,如果你使用EC2 cli工具,請不要指定密鑰文件的完整路徑。
ec2-run-instances ami-d1ec01a6 -t t2.medium --region eu-west-1 --key EC2-Kibi-Enterprise-Deployment.pem

+0

我必須更改密鑰:〜/ .keys/EC2-Kibi- Enterprise-Deployment.pem鍵入:〜/ .keys/EC2-Kibi-Enterprise-Deployment –