2016-05-12 66 views
1

我試圖做一個開始與ansible,特別是使用ansible playbook來部署ec2實例,但我不斷收到錯誤。錯誤開始ec2實例與ansible

我跟隨代碼中發現在這個線程:Best way to launch aws ec2 instances with ansible

我在我自己的細節取代給我下面的

hosts文件:

[local] 
localhost 

[webserver] 

create_instance.yml

--- 
    - name: Provision an EC2 Instance 
    hosts: local 
    connection: local 
    gather_facts: False 
    tags: provisioning 
    # Necessary Variables for creating/provisioning the EC2 Instance 
    vars: 
     instance_type: t2.micro 
     security_group: webserver # Change the security group name here 
     image: ami-f95ef58a # Change the AMI, from which you want to launch  the server 
     region: eu-west-1 # Change the Region 
     keypair: MyKeyPair # Change the keypair name 
     count: 1 

     # Task that will be used to Launch/Create an EC2 Instance 
     tasks: 

    - name: Create a security group 
     local_action: 
     module: ec2_group 
     name: "{{ security_group }}" 
     description: Security Group for webserver Servers 
     region: "{{ region }}" 
     rules: 
     - proto: tcp 
      type: ssh 
      from_port: 22 
      to_port: 22 
      cidr_ip: 0.0.0.0/0 
     - proto: tcp 
      from_port: 80 
      to_port: 80 
      cidr_ip: 0.0.0.0/0 
     rules_egress: 
     - proto: all 
      type: all 
      cidr_ip: 0.0.0.0/0 


    - name: Launch the new EC2 Instance 
    local_action: ec2 
        group={{ security_group }} 
        instance_type={{ instance_type}} 
        image={{ image }} 
        wait=true 
        region={{ region }} 
        keypair={{ keypair }} 
        count={{count}} 
    register: ec2 

    - name: Add the newly created EC2 instance(s) to the local host group (located inside the directory) 
    local_action: lineinfile 
        dest="./hosts" 
        regexp={{ item.public_ip }} 
        insertafter="[webserver]" line={{ item.public_ip }} 
    with_items: ec2.instances 


    - name: Wait for SSH to come up 
    local_action: wait_for 
        host={{ item.public_ip }} 
        port=22 
        state=started 
    with_items: ec2.instances 

    - name: Add tag to Instance(s) 
    local_action: ec2_tag resource={{ item.id }} region={{ region }} state=present 
    with_items: ec2.instances 
    args: 
     tags: 
     Name: webserver  

然後我創建環境變量統計局對我的AWS項,如下所示:

export AWS_ACCESS_KEY=my aws key 
export AWS_SECRET_KEY=my aws secret key 

當我與 須藤ansible-劇本-i主機上運行我的代碼create_instance.yml 我得到以下錯誤:

PLAY [localhost]  ************************************************************** 

TASK: [make one instance] ***************************************************** 
failed: [localhost] => {"failed": true} 
msg: No handler was ready to authenticate. 1 handlers were checked.  ['HmacAuthV4Handler'] Check your credentials 

FATAL: all hosts have already failed -- aborting 

PLAY RECAP ******************************************************************** 
      to retry, use: --limit @/home/ubuntu/create_instance.retry 

localhost     : ok=0 changed=0 unreachable=0 failed=1 

人建議我可能會出錯的地方?

+0

請勿使用sudo。根用戶(可能)沒有你的環境變量加載 – ydaetskcoR

+0

感謝您的建議,但仍然沒有運氣。爲了確認我正確設置密鑰對,是否應將我的yml文件中的「密鑰對」變量設置爲已上傳到AWS的密鑰對的名稱?我的意思是我用來通過AWS控制檯創建新的ec2實例的密鑰對的名稱?這是我目前使用的,但只是想檢查它是正確的。 – Rjodo

+0

它沒有那麼遠。這是失敗的,因爲你沒有正確設置aws連接變量。如果您使用sudo,那麼它不會加載您在當前shell中導出的變量。另一種方法是在劇本或庫存中指定「aws_access_key」和「aws_secret_key」變量。 – ydaetskcoR

回答

0

如果您的主持人無法與您的AWS賬戶建立連接,則會出現此錯誤。爲此,您需要確保訪問密鑰已正確設置並具有足夠的權限來創建實例。

Ansible在python上工作,並選擇python目錄。所以確保你使用pip安裝了awscli而不是apt-get install awscli。使用sudo pip install awscli

在文件〜/ .aws/credentials中指定您的訪問密鑰。

另外請確保您已安裝更新版本的boto和python。 請參閱此http://www.dowdandassociates.com/blog/content/howto-install-aws-cli-security-credentials/。這裏很好地提到了配置密鑰的所有方法。

+0

謝謝。我遵循你的步驟,但我仍然有問題。我收到的錯誤如下....身份驗證或權限失敗。在某些情況下,您可能已經能夠對遠程目錄進行身份驗證並且沒有權限。考慮將ansible.cfg中的遠程臨時路徑更改爲以「/ tmp」爲根的路徑。失敗的命令是:mkdir -p $ HOME/.ansible/tmp/ansible-tmp-1463751636.36-33202375925080 && chmod a + rx $ HOME/.ansible/tmp/ansible-tmp-1463751636.36-33202375925080 && echo $ HOME/.ansible/tmp/ansible-tmp-1463751636.36-33202375925080,結果退出1 – Rjodo

+0

噢,這意味着你沒有權限在你的盒子上創建目錄。你能確定在$ HOME上創建的/.ansible/tmp/目錄的寫權限和所有權嗎?你正在用同一個用戶運行這個完美的劇本嗎? –