Is it possible to do if-else checks in Ansible?

Currently, my EC2 game preparation process is as follows:

- 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

So, is it possible to do the following:

During the initialization of the EC2 (spot) instance, I want to check gradually (for example, 40%, 50%, 60%, 70% ..)) And if all else fails, create the instance on request. How to do it?

Can I use the Blocks function here? If so, how?

+4
source share
2 answers

Example:

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

This will lead to the completion of the task with all of the listed items until it succeeds with one of them.

UPD: shell, ec2, . shell . downvotes?

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, .

, if-not - ssh, .

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

:

   when: '"ERROR" in result'
0

Source: https://habr.com/ru/post/1674646/


All Articles