2017-04-12 44 views
4

目前,我當場EC2配置戲看起來是這樣的:是否有可能在Ansible中執行其他檢查?

- name: Provisioning Spot instaces 
    ec2: 
    spot_price: 0.50 
    spot_wait_timeout: 300 
    assign_public_ip: no 
    aws_access_key: "{{ aws_id }}" 
    aws_secret_key: "{{ aws_key }}" 
    region: "{{ aws_region }}" 
    image: "{{ image_instance }}" 
    instance_type: "{{ large_instance }}" 
    key_name: "{{ ssh_keyname }}" 
    count: 3 
    state: present 
    group_id: "{{ cypher_priv_sg }}" 
    vpc_subnet_id: "{{ private_subnet_id }}" 
    wait: true 
    instance_tags: 
     Name: Cypher-Worker 
    #delete_on_termination: yes 
    register: ec2 

那麼,是不是可以做到以下幾點:

雖然供應一(點)的EC2實例,我想逐步檢查(如(40%,50%,60%,70%..))如果全部失敗,則創建一個按需實例。 我該怎麼做?

我可以在這裏使用Blocks功能嗎?如果是,那麼如何?

+0

檢查什麼增量? –

+0

@MattSchuchard'spot_price'值 – Dawny33

+0

你可以使用某種'async/poll/register'和/或'begin/rescue'來做到這一點嗎?我在這裏講理論。 –

回答

0

例子:

shell: /some/command 
register: result 
when: (result is not defined) or (result.rc != 0) 
with_items: 
    - 40 
    - 50 
    - 60 
    - 70 
    - 100 # on-demand 

這將運行所有列出的項目任務,直到它成功的其中之一。

UPD:這是例如用於shell模塊,我不知道ec2模塊輸出結構,不使用它。對於shell模塊,它工作正常。爲什麼downvotes?

+0

得到這個錯誤:錯誤是:在評估條件((ec2未定義)或(ec2.rc!= 0))時出錯:'dict對象'沒有屬性'rc'' – Dawny33

+0

對於ec2模塊@ Dawny33你應該使用適當的檢查它的輸出結構,我發佈了shell模塊的例子,你應該檢查註冊變量,而不是模塊本身。 –

0

如何:

- name: Create ec2 instance 
    ec2: 
    key_name: "{{ key }}" 
    group: "{{ group }}" 
    instance_type: "{{ itype }}" 
    image: "{{ image }}" 
    region: "{{ region }}" 
    wait: yes 
    wait_timeout: 500 
    volumes: 
    - device_name: /dev/xvda 
     volume_type: "{{ voltype }}" 
     volume_size: "{{ volsize }}" 
    monitoring: no 
    vpc_subnet_id: "{{ subnetid }}" 
    register: system 

- name: Wait for ssh 
    wait_for: host={{ item.public_ip }} port=22 state=started 
    with_items: 
    - "{{ system.instances }}" 

- name: Name the new instance 
    ec2_tag: resource={{ item.id }} region=eu-central-1 state=present 
    args: 
    tags: 
    Name: "{{ name }}" 
    with_items: 
    - "{{ system.instances }}" 

你(嘗試)創建實例,並將其存儲在事實system。如果您需要檢查system是否實際上包含您需要的內容,則可以簡單地添加when: system.instances.id[0] is defined,或者進一步查看 - 通過檢查您的新實例是否可以在端口22上訪問,如上例所示。

如果實例創建失敗且無法通過ssh訪問,則可以對其他任務使用這些條件檢查,實現if-not邏輯 - 創建按需實例。

- name: Wait for ssh 
    wait_for: host={{ item.public_ip }} port=22 state=started 
    with_items: 
    - "{{ system.instances }}" 
    register: result 

下一步:

when: '"ERROR" in result' 
相關問題