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 ?

  • I'm not sure the addition of two vars works like this. Perhaps you want the combine filter? -- docs.ansible.com/ansible/latest/user_guide/… – Bruce Becker Oct 17 at 6:45
  • This is not the problem .... The addition of two vars works fine ... This way you can for example loop on "{{ vars[ 'list_rds_resources_ids1_' + prod ] + vars[ 'list_rds_resources_ids2_' + preprod ] }}" which is equivalent to "{{ list_rds_resources_ids1_prod + list_rds_resources_ids2_preprod "}}. Anyway, this is not the point of the question ... The question is about what to put in the until ... – jmary Oct 17 at 7:38
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 :)

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.