2017-04-12 42 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' 
相关问题