Answers: <p>I found the solution to my problem:</p>
  
  <p>In the main.yml task :</p>
  
  <pre class="lang-yaml prettyprint-override"><code>- 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
  </code></pre>
  
  <p>In the wait_rds_are_stopped.yml task:</p>
  
  <pre class="lang-yaml prettyprint-override"><code>- 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
  </code></pre>
  
  <p>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.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-10-18T15:12:27.587Z
Answers: 2019-10-18T15:12:27.587Z
Answers: jmary
Answers: <p>If I understood you correctly, you need <code>free strategy</code>.
  It will run all tasks ASAP, not waiting for anything.
  <a href="https://docs.ansible.com/ansible/latest/plugins/strategy/free.html" rel="nofollow noreferrer">https://docs.ansible.com/ansible/latest/plugins/strategy/free.html</a></p>
  
  <p>Bear in mind, that logs will be a mess :)</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-10-17T10:22:04.650Z
Answers: 2019-10-17T10:22:04.650Z
Answers: Mr Smith
Body: <p>With Ansible, I'm stopping several RDS (it could be EC2) instances.</p>
  
  <p>Then I want continue only when all of them are in the state stopped. I use the following task:</p>
  
  <pre class="lang-yaml prettyprint-override"><code>- 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
  </code></pre>
  
  <p>Right now, my code is testing only the first element. How to make the test on all existing elements at the same time ?</p>
  
Comments: CC BY-SA 4.0
Comments: 2019-10-17T06:45:35.977Z
Comments: I'm not sure the addition of two vars works like this. Perhaps you want the `combine` filter? -- https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#combining-hashes-dictionaries
Comments: Bruce Becker
Comments: CC BY-SA 4.0
Comments: 2019-10-17T07:38:54.940Z
Comments: 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 ...
Comments: jmary
Tags: <ansible><amazon-rds>
Title: Ansible: Test if multiple RDS instances are all stopped
UserDisplayName: jmary
ThreadText: # Ansible: Test if multiple RDS instances are all stopped
  
  (Asked by: username_0 on 2019-10-16)
  
  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:
  
  ```yaml
  - 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 ?
  
  **Tags:** ansible, amazon-rds
  
  > **Comments:**
  > **username_1**: I'm not sure the addition of two vars works like this. Perhaps you want the `combine` filter? -- https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#combining-hashes-dictionaries  
  > **username_0**: 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 ...  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2019-10-18. Score: 1 | Accepted answer
  
  I found the solution to my problem:
  
  In the main.yml task :
  
  ```yaml
  - 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:
  
  ```yaml
  - 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.
  
  
  ---
  
  ### Answer by username_2 on 2019-10-17. Score: 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 :)
  
  
  ---
  
  