2016-02-01 19 views
0

我的playbook有問題,應該通過內置模塊創建新的EC2實例並連接到它們以設置一些默認的東西。在相同腳本中創建槽ec2模塊後無法訪問機器

我去了很多教程/帖子,但沒有人提到相同的問題,因此我在那裏問。

就創建而言,一切都很順利,但是當我創建實例併成功等待SSH出現時。我說錯誤,說機器無法訪問。

UNREACHABLE! => {"changed": false, "msg": "ERROR! SSH Error: data could not be sent to the remote host. Make sure this host can be reached over ssh", "unreachable": true} 

我試圖手動連接(從終端到同一主機),我很成功(而劇本等待連接)。我也嘗試在ansible.cfg中增加超時。我證實了給定的主機名是有效的(也是),並且還嘗試了公有的ip而不是公有的DNS,但沒有任何幫助。

基本上我的劇本看起來像

--- 
    - name: create ec2 instances 
    hosts: local 
    connection: local 
    gather_facts: False 
    vars: 
     machines: 
     - { type: "t2.micro", instance_tags: { Name: "machine1", group: "some_group" }, security_group: ["SSH"] } 
    tasks: 
     - name: lunch new ec2 instances 
     local_action: ec2 
         group={{ item.security_group }} 
         instance_type={{ item.type}} 
         image=... 
         wait=true 
         region=... 
         keypair=... 
         count=1 
         instance_tags=... 
     with_items: machines 
     register: ec2 

     - name: wait for SSH to come up 
     local_action: wait_for host={{ item.instances.0.public_dns_name }} port=22 delay=60 timeout=320 state=started 
     with_items: ec2.results 

     - name: add host into launched group 
     add_host: name={{ item.instances.0.public_ip }} group=launched 
     with_items: ec2.results 

    - name: with the newly provisioned EC2 node configure basic stuff 
    hosts: launched 
    sudo: yes 
    remote_user: ubuntu 
    gather_facts: True 
    roles: 
     - common 

注:在很多教程從創建不同的方式訪問EC2實例的結果,但是那可能是不同的問題。

感謝

解決:

我不知道怎麼回事,但它突然開始工作。沒有線索。在情況下,我會發現一些新的信息,會更新這個問題

+0

你究竟在哪裏得到這個錯誤?哪個玩法? – helloV

+0

它在共同角色(set_hostname)中的第一個任務。基本上,當我第一次嘗試訪問新機器時。分離成兩個劇本(必須分開管理)的作品,但我不喜歡它 –

回答

1

幾個點,可以幫助:

  1. 我猜這是一個版本的區別,但我從來沒有見過一個「結果」鍵入已註冊的'ec2'變量。在任何情況下,我通常都會使用'tagged_instances' - 這可以確保即使播放沒有創建實例(即因爲匹配的實例已經存在於前一個運行中),該變量仍會返回實例數據可以用來將新的主機添加到庫存。

  2. 嘗試在'wait_for'播放中添加'search_regex:'OpenSSH'',以確保它在SSH守護程序完全啓動之前不會嘗試運行。

修改後的劇本是這樣的:

- name: wait for SSH to come up 
    local_action: wait_for host={{ item.public_dns_name }} port=22 delay=60 timeout=320 state=started search_regex="OpenSSH" 
    with_items: ec2.tagged_instances 
- name: add host into launched group 
    add_host: name={{ item.public_ip }} group=launched 
    with_items: ec2.tagged_instances 

也,當然,要確保Ansible知道使用指定的鍵SSH'ing到遠程主機或者通過當在庫存條目中添加'ansible_ssh_private_key_file'或在命令行中指定'--private-key = ...'。

+0

嗨,謝謝你的建議。 廣告1)我知道,它的奇怪,我不得不使用調試很多次才能得到它。我不喜歡它,但無論如何 廣告2)謝謝,將檢查修改 –