2

With Ansible, I'm stopping several RDS (it could be EC2) instances.

Then I want continue only when all of them are in the state stopped. I use the following task:

- name: Wait for RDS modify to finish
  rds_instance_facts:
    aws_access_key: "{{ assumed_role.sts_creds.access_key }}"
    aws_secret_key: "{{ assumed_role.sts_creds.secret_key }}"
    security_token: "{{ assumed_role.sts_creds.session_token }}"
    db_instance_identifier: "{{ item }}"
    region: "{{ region }}"
  loop: "{{ vars[ 'list_rds_resources_ids1_' ] + vars[ 'list_rds_resources_ids2_' ] }}"
  register: aws_rds_facts
  until: aws_rds_facts[0].db_instance_status == "stopped"
  retries: 90
  delay: 10

Right now, my code is testing only the first element. How to make the test on all existing elements at the same time ?

jmary
  • 171
  • 5

2 Answers2

2

If I understood you correctly, you need free strategy. It will run all tasks ASAP, not waiting for anything. https://docs.ansible.com/ansible/latest/plugins/strategy/free.html

Bear in mind, that logs will be a mess :)

Mr Smith
  • 51
  • 4
1

I found the solution to my problem:

In the main.yml task :

- include: wait_rds_are_stopped.yml
  when: chosen_action == "stop"
  loop: "{{ vars[ 'list_rds_resources_ids1_' + environment ] + vars[ 'list_rds_resources_ids2_' + environment ] }}"
  loop_control:
    loop_var: outer_item

In the wait_rds_are_stopped.yml task:

- name: Wait for RDS stop to finish
  rds_instance_facts:
    aws_access_key: "{{ assumed_role.sts_creds.access_key }}"
    aws_secret_key: "{{ assumed_role.sts_creds.secret_key }}"
    security_token: "{{ assumed_role.sts_creds.session_token }}"
    db_instance_identifier: "{{ outer_item }}"
    region: "{{ region }}"
  register: aws_rds_facts
  until: aws_rds_facts.instances[0].db_instance_status == "stopped"
  retries: 90
  delay: 10

Thus, the loop control is outside. The stop of the 3 databases has been launched before, without waiting, so we save time. And the external loop will finish only when all the databases are stopped.

jmary
  • 171
  • 5