{"id":33446,"date":"2024-07-09T10:22:30","date_gmt":"2024-07-09T08:22:30","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/?p=33446"},"modified":"2024-07-09T10:22:33","modified_gmt":"2024-07-09T08:22:33","slug":"ansible-loops-a-guide-from-basic-to-advanced-examples","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/ansible-loops-a-guide-from-basic-to-advanced-examples\/","title":{"rendered":"Ansible loops: A guide from basic to advanced examples"},"content":{"rendered":"\n<p>If you are writing roles with Ansible, you must already have thought about implementing a loop, a loop of loops with Ansible, and wonder how. The ability to execute tasks in loops is primordial. This guide will provide multiple loop examples in Ansible, starting with a basic loop and progressing to more advanced scenarios.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-basic-loop\">Basic loop<\/h2>\n\n\n\n<p>Let&#8217;s start with the most basic loop as an introduction. <\/p>\n\n\n\n<p>In the following playbook, called playbook.yml, a list of numbers is created, from 1 to 5. Then a loop on the debug task displays each number.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n# playbook.yml\n- name: Loop examples\n  hosts: localhost\n  connection: local\n  gather_facts: False\n  tasks:\n  - set_fact:\n      numbers: &#x5B;1,2,3,4,5]\n  \n  - name: Most basic loop\n    debug:\n      msg: &#039;{{ item }}&#039;\n    loop: &#039;{{\u00a0numbers }}&#039;\n<\/pre><\/div>\n\n\n<p>You can test it by running the command &#8220;ansible-playbook playbook.yml&#8221;.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n$ ansible-playbook playbook.yml \n\nPLAY &#x5B;Use vars from dbservers] ****************************************************************************************************************************************************************\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;Most basic loop] ************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; (item=1) =&gt; {\n    &quot;msg&quot;: 1\n}\nok: &#x5B;localhost] =&gt; (item=2) =&gt; {\n    &quot;msg&quot;: 2\n}\nok: &#x5B;localhost] =&gt; (item=3) =&gt; {\n    &quot;msg&quot;: 3\n}\nok: &#x5B;localhost] =&gt; (item=4) =&gt; {\n    &quot;msg&quot;: 4\n}\nok: &#x5B;localhost] =&gt; (item=5) =&gt; {\n    &quot;msg&quot;: 5\n}\n\nPLAY RECAP ************************************************************************************************************************************************************************************\nlocalhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-loop-of-loops\">Loop of loops<\/h2>\n\n\n\n<p>In most use cases, you want to loop multiple tasks sequentially. The first thought is to use a block statement, but a block doesn&#8217;t accept a loop. The solution is to use &#8220;ansible.builtin.include_tasks&#8221; and loop on the task file.<\/p>\n\n\n\n<p>The usage of loop_control is recommended to rename the loop_var name and not use &#8220;item&#8221;. See below the example with the &#8220;playbook.yml&#8221; and &#8220;loop.yml&#8221; files.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n# playbook.yml\n- name: Loop examples\n  hosts: localhost\n  connection: local\n  gather_facts: False\n  tasks:\n  - set_fact:\n      numbers: &#x5B;1,2,3,4,5]\n\n  - name: loop multiple tasks with include_task\n    ansible.builtin.include_tasks:\n      file: loop.yml\n    loop: &#039;{{ numbers }}&#039;\n    loop_control:\n      loop_var: number\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n# loop.yml\n- debug:\n    msg: &#039;First task of loop.yml&#039;\n\n- debug:\n    var: number\n<\/pre><\/div>\n\n\n<p>The results of running the playbook should be as below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n$ ansible-playbook playbook.yml\n\nPLAY &#x5B;Loop examples] **************************************************************************************************************************************************************************\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;loop multiple tasks with include_task] **************************************************************************************************************************************************\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/loop.yml for localhost =&gt; (item=1)\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/loop.yml for localhost =&gt; (item=2)\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/loop.yml for localhost =&gt; (item=3)\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/loop.yml for localhost =&gt; (item=4)\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/loop.yml for localhost =&gt; (item=5)\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;First task of loop.yml&quot;\n}\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;number&quot;: 1\n}\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;First task of loop.yml&quot;\n}\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;number&quot;: 2\n}\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;First task of loop.yml&quot;\n}\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;number&quot;: 3\n}\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;First task of loop.yml&quot;\n}\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;number&quot;: 4\n}\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;First task of loop.yml&quot;\n}\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;number&quot;: 5\n}\n\nPLAY RECAP ************************************************************************************************************************************************************************************\nlocalhost                  : ok=16   changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conditional-on-include-tasks\">Conditional on include_tasks<\/h2>\n\n\n\n<p>Adding a condition on the include_tasks is only evaluated once. It means that if the condition is True, it will iterate until the end even though the condition might become False, which is the intended result of the condition of include_tasks. See the following example.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n# playbook.yml\n- name: Loop examples\n  hosts: localhost\n  connection: local\n  gather_facts: False\n  tasks:\n\n  - set_fact:\n      numbers: &#x5B;1,2,3,4,5]\n\n  - set_fact: \n      continue_task: true\n\n  - when: continue_task == true\n    name: When on include task only (evaluted at the beginning)\n    ansible.builtin.include_tasks:\n      file: condition.yml\n    loop: &#039;{{ numbers }}&#039;\n    loop_control:\n      loop_var: number\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n# condition.yml\n- debug:\n    var: number\n\n- when: number &gt;= 3\n  set_fact:\n    continue_task: false\n\n- debug:\n    msg: &#039;current number: {{\u00a0number }}. Condition.yml running on number &lt; 3&#039;\n<\/pre><\/div>\n\n\n<p>Results of the running playbook.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n$ ansible-playbook playbook.yml\n\nPLAY &#x5B;Loop examples] **************************************************************************************************************************************************************************\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;When on include task only (evaluted at the beginning)] **********************************************************************************************************************************\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/condition.yml for localhost =&gt; (item=1)\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/condition.yml for localhost =&gt; (item=2)\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/condition.yml for localhost =&gt; (item=3)\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/condition.yml for localhost =&gt; (item=4)\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/condition.yml for localhost =&gt; (item=5)\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;number&quot;: 1\n}\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;current number: 1. Condition.yml running on number &lt; 3&quot;\n}\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;number&quot;: 2\n}\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;current number: 2. Condition.yml running on number &lt; 3&quot;\n}\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;number&quot;: 3\n}\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;current number: 3. Condition.yml running on number &lt; 3&quot;\n}\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;number&quot;: 4\n}\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;current number: 4. Condition.yml running on number &lt; 3&quot;\n}\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;number&quot;: 5\n}\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;current number: 5. Condition.yml running on number &lt; 3&quot;\n}\n\nPLAY RECAP ************************************************************************************************************************************************************************************\nlocalhost                  : ok=20   changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   \n<\/pre><\/div>\n\n\n<p>If you want the condition to apply on every task, you can either add the statement &#8220;when&#8221; on every task in &#8220;condition.yml&#8221;, or you can use the statement &#8220;apply&#8221; on &#8220;ansible.builtin.include_tasks&#8221;. Choose the solution that suits the best for the role or tasks you are writing.<\/p>\n\n\n\n<p>The below example will use the statement &#8220;apply&#8221;.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n# playbook.yml\n\n- name: Loop examples\n  hosts: localhost\n  connection: local\n  gather_facts: False\n  tasks:\n\n  - set_fact:\n      numbers: &#x5B;1,2,3,4,5]\n\n  - set_fact: \n      continue_task: true\n\n  - name: When applied on all tasks included (evaluated each time)\n    ansible.builtin.include_tasks:\n      file: condition.yml\n      apply:  \n        when: continue_task == true\n    loop: &#039;{{ numbers }}&#039;\n    loop_control:\n      loop_var: number\n<\/pre><\/div>\n\n\n<p>Output of the playbook. The results should print the number until &#8220;3&#8221; but not the second debug message  &#8220;current number 3. Condition.yml running on number &lt; 3&#8221;. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n$ ansible-playbook playbook.yml\n\nPLAY &#x5B;Loop examples] **************************************************************************************************************************************************************************\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;When applied on all tasks included (evalued each time)] *********************************************************************************************************************************\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/condition.yml for localhost =&gt; (item=1)\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/condition.yml for localhost =&gt; (item=2)\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/condition.yml for localhost =&gt; (item=3)\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/condition.yml for localhost =&gt; (item=4)\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/condition.yml for localhost =&gt; (item=5)\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;number&quot;: 1\n}\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;current number: 1. Condition.yml running on number &lt; 3&quot;\n}\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;number&quot;: 2\n}\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;current number: 2. Condition.yml running on number &lt; 3&quot;\n}\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;number&quot;: 3\n}\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nPLAY RECAP ************************************************************************************************************************************************************************************\nlocalhost                  : ok=13   changed=0    unreachable=0    failed=0    skipped=9    rescued=0    ignored=0   \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-advanced-loop-do-tasks-until-succeed-or-fail-at-xth-attempt\">Advanced loop: do tasks until succeed, or fail at Xth attempt<\/h2>\n\n\n\n<p>In this last example, I want to make a loop of tasks. The problem is that multiple tasks may fail for multiple reasons (network, unavailability of external services, awaiting process from external services, etc.). Therefore I want to retry the task at least 5th times, before exiting the playbook run. <\/p>\n\n\n\n<p>The example will include a commented task, to provide a more concrete real use case example. The real example is the following. Fetch the ID of an item from an external service, use this ID to fetch its status to the external service, and only proceed if the item status is completed (successful, completed) or fail the task if it returns a failure state. The task may fail on the fetch of ID (due to network issues or unavailability of the external service) and on the status fetching if it is still ongoing, which will result in retrying the whole task. Note that it is not completely optimized to make it simpler to understand and read (for example we could ignore the fetching of ID if it was already gotten in a previous iteration).<\/p>\n\n\n\n<p>For the simplicity of the setup, a simple condition on &#8220;number&#8221; is used instead, to showcase the retry of tasks and failure of the play. It causes the playbook&#8217;s execution to fail if the number is above 3.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n# playbook.yml\n\n- name: Loop examples\n  hosts: localhost\n  connection: local\n  gather_facts: False\n  tasks:\n\n  - set_fact:\n      numbers: &#x5B;1,2,3,4,5]\n\n  - set_fact: \n      continue_task: true\n\n\n  - name: Example of a complex loop. Loop on number, do function(number) until suceed, or fail at the fifth attempt\n    ansible.builtin.include_tasks:\n      file: function.yml\n    loop: &#039;{{ numbers }}&#039;\n    loop_control:\n      loop_var: number\n  \n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n# function.yml\n- block:\n    - when: init_count | default(true)\n      ansible.builtin.set_fact:\n        retry_count: 0\n\n    - debug:\n        msg: &#039;Current number is {{ number }}, and current retry count is {{\u00a0retry_count }}&#039;\n\n    # Do an action, use the result to do another action or checks (for example a wget, curl, or another request to get an ID)\n    #\u00a0For the simplicity of the example, I simply do an echo\n    - name: API - get ID\n      ansible.builtin.shell: &#039;echo {{ number }}&#039;\n      register: _api_result\n\n    #\u00a0Use the result from the precedent task\n    - name: Use the ID to check another API if process is succesful \n      ##\u00a0An exemple of a use case, using the id\n      # ansible.builtin.uri:\n      #   url: &#039;https:\/\/example.com\/status?id={{ _api_result.stdout }}&#039;\n      #   method: GET\n      #   status_code: 200\n      # register: _check_status\n      # until:\n      #   - _check_status.json is defined\n      #   - _check_status.json.status in &#x5B;&quot;SUCCESSFUL&quot;, &quot;COMPLETED&quot;, &quot;FAILURE&quot;]\n      # failed_when: _check_status.json is not defined or _check_status.json.status in &#x5B;&quot;FAILURE&quot;]\n      # delay: &#039;5&#039;\n      # retries: &#039;3&#039;\n\n      ## For the simplicity, I just used a failed_when on debug\n      debug:\n        msg: &#039;Testing that api result is a &gt; 3&#039;\n      failed_when: _api_result.stdout|int &gt; 3\n\n  rescue:\n    - when: _check_status.json is defined and _check_status.json.status in &#x5B;&quot;FAILURE&quot;]\n      name: Fail if process return Failure\n      ansible.builtin.fail:\n        msg: status failure\n\n    - name: Fail Task in case of total failure after a certain amount of retry\n      ansible.builtin.fail:\n        msg: &quot;5 retries attempted, failed perform desired result&quot;\n      when: retry_count | int &gt;= 5 \n\n    #\u00a0Pause the playbook if necessary.\n    # - ansible.builtin.pause:\n    #     seconds: &#039;5&#039;\n\n    - name: Increment Retry Count\n      ansible.builtin.set_fact:\n        retry_count: &quot;{{ retry_count | int + 1 }}&quot;\n\n    # Retry the function.yml and indicate to increment the counter.\n    - name: Retry function\n      ansible.builtin.include_tasks: function.yml\n      vars:\n        init_count: false\n<\/pre><\/div>\n\n\n<p>Result of the execution.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n$ ansible-playbook playbook.yml\n\nPLAY &#x5B;Loop examples] **************************************************************************************************************************************************************************\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;When applied on all tasks included (evalued each time)] *********************************************************************************************************************************\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/condition.yml for localhost =&gt; (item=1)\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/condition.yml for localhost =&gt; (item=2)\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/condition.yml for localhost =&gt; (item=3)\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/condition.yml for localhost =&gt; (item=4)\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/condition.yml for localhost =&gt; (item=5)\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;number&quot;: 1\n}\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;current number: 1. Condition.yml running on number &lt; 3&quot;\n}\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;number&quot;: 2\n}\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;current number: 2. Condition.yml running on number &lt; 3&quot;\n}\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;number&quot;: 3\n}\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nPLAY RECAP ************************************************************************************************************************************************************************************\nlocalhost                  : ok=13   changed=0    unreachable=0    failed=0    skipped=9    rescued=0    ignored=0   \n\nkke@DBI-LT-KKE ansible_loop % ansible-playbook playbook.yml\n\nPLAY &#x5B;Loop examples] **************************************************************************************************************************************************************************\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;set_fact] *******************************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;Example of a complex loop. Loop on number, do function(number) until suceed, or fail at the fifth attempt] ******************************************************************************\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/function.yml for localhost =&gt; (item=1)\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/function.yml for localhost =&gt; (item=2)\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/function.yml for localhost =&gt; (item=3)\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/function.yml for localhost =&gt; (item=4)\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/function.yml for localhost =&gt; (item=5)\n\nTASK &#x5B;ansible.builtin.set_fact] ***************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;Current number is 1, and current retry count is 0&quot;\n}\n\nTASK &#x5B;API - get ID] ***************************************************************************************************************************************************************************\nchanged: &#x5B;localhost]\n\nTASK &#x5B;Use the ID to check another API if process is succesful] ********************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;Testing that api result is a &gt; 3&quot;\n}\n\nTASK &#x5B;ansible.builtin.set_fact] ***************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;Current number is 2, and current retry count is 0&quot;\n}\n\nTASK &#x5B;API - get ID] ***************************************************************************************************************************************************************************\nchanged: &#x5B;localhost]\n\nTASK &#x5B;Use the ID to check another API if process is succesful] ********************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;Testing that api result is a &gt; 3&quot;\n}\n\nTASK &#x5B;ansible.builtin.set_fact] ***************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;Current number is 3, and current retry count is 0&quot;\n}\n\nTASK &#x5B;API - get ID] ***************************************************************************************************************************************************************************\nchanged: &#x5B;localhost]\n\nTASK &#x5B;Use the ID to check another API if process is succesful] ********************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;Testing that api result is a &gt; 3&quot;\n}\n\nTASK &#x5B;ansible.builtin.set_fact] ***************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;Current number is 4, and current retry count is 0&quot;\n}\n\nTASK &#x5B;API - get ID] ***************************************************************************************************************************************************************************\nchanged: &#x5B;localhost]\n\nTASK &#x5B;Use the ID to check another API if process is succesful] ********************************************************************************************************************************\nfatal: &#x5B;localhost]: FAILED! =&gt; {\n    &quot;msg&quot;: &quot;Testing that api result is a &gt; 3&quot;\n}\n\nTASK &#x5B;Fail if process return Failure] *********************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;Fail Task in case of total failure after a certain amount of retry] *********************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;Increment Retry Count] ******************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;Retry function] *************************************************************************************************************************************************************************\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/function.yml for localhost\n\nTASK &#x5B;ansible.builtin.set_fact] ***************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;Current number is 4, and current retry count is 1&quot;\n}\n\nTASK &#x5B;API - get ID] ***************************************************************************************************************************************************************************\nchanged: &#x5B;localhost]\n\nTASK &#x5B;Use the ID to check another API if process is succesful] ********************************************************************************************************************************\nfatal: &#x5B;localhost]: FAILED! =&gt; {\n    &quot;msg&quot;: &quot;Testing that api result is a &gt; 3&quot;\n}\n\nTASK &#x5B;Fail if process return Failure] *********************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;Fail Task in case of total failure after a certain amount of retry] *********************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;Increment Retry Count] ******************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;Retry function] *************************************************************************************************************************************************************************\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/function.yml for localhost\n\nTASK &#x5B;ansible.builtin.set_fact] ***************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;Current number is 4, and current retry count is 2&quot;\n}\n\nTASK &#x5B;API - get ID] ***************************************************************************************************************************************************************************\nchanged: &#x5B;localhost]\n\nTASK &#x5B;Use the ID to check another API if process is succesful] ********************************************************************************************************************************\nfatal: &#x5B;localhost]: FAILED! =&gt; {\n    &quot;msg&quot;: &quot;Testing that api result is a &gt; 3&quot;\n}\n\nTASK &#x5B;Fail if process return Failure] *********************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;Fail Task in case of total failure after a certain amount of retry] *********************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;Increment Retry Count] ******************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;Retry function] *************************************************************************************************************************************************************************\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/function.yml for localhost\n\nTASK &#x5B;ansible.builtin.set_fact] ***************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;Current number is 4, and current retry count is 3&quot;\n}\n\nTASK &#x5B;API - get ID] ***************************************************************************************************************************************************************************\nchanged: &#x5B;localhost]\n\nTASK &#x5B;Use the ID to check another API if process is succesful] ********************************************************************************************************************************\nfatal: &#x5B;localhost]: FAILED! =&gt; {\n    &quot;msg&quot;: &quot;Testing that api result is a &gt; 3&quot;\n}\n\nTASK &#x5B;Fail if process return Failure] *********************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;Fail Task in case of total failure after a certain amount of retry] *********************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;Increment Retry Count] ******************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;Retry function] *************************************************************************************************************************************************************************\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/function.yml for localhost\n\nTASK &#x5B;ansible.builtin.set_fact] ***************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;Current number is 4, and current retry count is 4&quot;\n}\n\nTASK &#x5B;API - get ID] ***************************************************************************************************************************************************************************\nchanged: &#x5B;localhost]\n\nTASK &#x5B;Use the ID to check another API if process is succesful] ********************************************************************************************************************************\nfatal: &#x5B;localhost]: FAILED! =&gt; {\n    &quot;msg&quot;: &quot;Testing that api result is a &gt; 3&quot;\n}\n\nTASK &#x5B;Fail if process return Failure] *********************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;Fail Task in case of total failure after a certain amount of retry] *********************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;Increment Retry Count] ******************************************************************************************************************************************************************\nok: &#x5B;localhost]\n\nTASK &#x5B;Retry function] *************************************************************************************************************************************************************************\nincluded: \/Users\/kke\/dbi\/blog\/ansible_loop\/function.yml for localhost\n\nTASK &#x5B;ansible.builtin.set_fact] ***************************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;debug] **********************************************************************************************************************************************************************************\nok: &#x5B;localhost] =&gt; {\n    &quot;msg&quot;: &quot;Current number is 4, and current retry count is 5&quot;\n}\n\nTASK &#x5B;API - get ID] ***************************************************************************************************************************************************************************\nchanged: &#x5B;localhost]\n\nTASK &#x5B;Use the ID to check another API if process is succesful] ********************************************************************************************************************************\nfatal: &#x5B;localhost]: FAILED! =&gt; {\n    &quot;msg&quot;: &quot;Testing that api result is a &gt; 3&quot;\n}\n\nTASK &#x5B;Fail if process return Failure] *********************************************************************************************************************************************************\nskipping: &#x5B;localhost]\n\nTASK &#x5B;Fail Task in case of total failure after a certain amount of retry] *********************************************************************************************************************\nfatal: &#x5B;localhost]: FAILED! =&gt; {&quot;changed&quot;: false, &quot;msg&quot;: &quot;5 retries attempted, failed perform desired result&quot;}\n\nPLAY RECAP ************************************************************************************************************************************************************************************\nlocalhost                  : ok=42   changed=9    unreachable=0    failed=1    skipped=16   rescued=6    ignored=0   \n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2>\n\n\n\n<p>Loop is a necessity in IT, the base for automation. At first, it might be disorientating with Ansible, but it becomes quite easy to understand and use with a little practice. The <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/playbook_guide\/playbooks_loops.html\">official documentation<\/a> provided by Ansible also covers a lot and explains well the concept of loop, it will be your best friend in your journey to master Ansible.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-links\">Links<\/h2>\n\n\n\n<p>Ansible &#8211; <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/playbook_guide\/playbooks_loops.html\">Official documentation<\/a><br>Blog &#8211; <a href=\"https:\/\/www.dbi-services.com\/blog\/faster-ansible\/\">Faster Ansible<\/a><br>Blog &#8211; <a href=\"https:\/\/www.dbi-services.com\/blog\/ansible-automates-event-driven-lightspeed\/\">Ansible Automates \u2013 Event Driven &amp; Lightspeed<\/a><br>Blog &#8211; <a href=\"https:\/\/www.dbi-services.com\/blog\/specify-hosts-in-ansible-playbook-command-line\/\">Specify hosts in ansible-playbook command line<\/a><br>Blog &#8211; <a href=\"https:\/\/www.dbi-services.com\/blog\/ansible-event-driven-automation\/\">Ansible Event Driven Automation<\/a><br>Blog &#8211; <a href=\"https:\/\/www.dbi-services.com\/blog\/create-and-manage-ansible-execution-environments\/\">Create and manage Ansible Execution Environments<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are writing roles with Ansible, you must already have thought about implementing a loop, a loop of loops with Ansible, and wonder how. The ability to execute tasks in loops is primordial. This guide will provide multiple loop examples in Ansible, starting with a basic loop and progressing to more advanced scenarios. Basic [&hellip;]<\/p>\n","protected":false},"author":132,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1321,1320],"tags":[2600,2667],"type_dbi":[3077,3017],"class_list":["post-33446","post","type-post","status-publish","format-standard","hentry","category-ansible","category-devops","tag-ansible-2","tag-devops-2","type-ansible","type-devops"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Ansible loops: A guide from basic to advanced examples - dbi Blog<\/title>\n<meta name=\"description\" content=\"How to use loop in Ansible. Example from basics loops to advance loops, including conditional, retry and loop of loops.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dbi-services.com\/blog\/ansible-loops-a-guide-from-basic-to-advanced-examples\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Ansible loops: A guide from basic to advanced examples\" \/>\n<meta property=\"og:description\" content=\"How to use loop in Ansible. Example from basics loops to advance loops, including conditional, retry and loop of loops.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/ansible-loops-a-guide-from-basic-to-advanced-examples\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2024-07-09T08:22:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-09T08:22:33+00:00\" \/>\n<meta name=\"author\" content=\"K\u00e9vin Keovilay\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"K\u00e9vin Keovilay\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/ansible-loops-a-guide-from-basic-to-advanced-examples\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/ansible-loops-a-guide-from-basic-to-advanced-examples\\\/\"},\"author\":{\"name\":\"K\u00e9vin Keovilay\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/3fb75c1e02be0c3b331471c8313cd9f7\"},\"headline\":\"Ansible loops: A guide from basic to advanced examples\",\"datePublished\":\"2024-07-09T08:22:30+00:00\",\"dateModified\":\"2024-07-09T08:22:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/ansible-loops-a-guide-from-basic-to-advanced-examples\\\/\"},\"wordCount\":673,\"commentCount\":0,\"keywords\":[\"Ansible\",\"devops\"],\"articleSection\":[\"Ansible\",\"DevOps\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/ansible-loops-a-guide-from-basic-to-advanced-examples\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/ansible-loops-a-guide-from-basic-to-advanced-examples\\\/\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/ansible-loops-a-guide-from-basic-to-advanced-examples\\\/\",\"name\":\"Ansible loops: A guide from basic to advanced examples - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\"},\"datePublished\":\"2024-07-09T08:22:30+00:00\",\"dateModified\":\"2024-07-09T08:22:33+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/3fb75c1e02be0c3b331471c8313cd9f7\"},\"description\":\"How to use loop in Ansible. Example from basics loops to advance loops, including conditional, retry and loop of loops.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/ansible-loops-a-guide-from-basic-to-advanced-examples\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/ansible-loops-a-guide-from-basic-to-advanced-examples\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/ansible-loops-a-guide-from-basic-to-advanced-examples\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Ansible loops: A guide from basic to advanced examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/3fb75c1e02be0c3b331471c8313cd9f7\",\"name\":\"K\u00e9vin Keovilay\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/aea984148a511f3db5117060e702df298f486588cee7781bb56a7bd92ac44a50?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/aea984148a511f3db5117060e702df298f486588cee7781bb56a7bd92ac44a50?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/aea984148a511f3db5117060e702df298f486588cee7781bb56a7bd92ac44a50?s=96&d=mm&r=g\",\"caption\":\"K\u00e9vin Keovilay\"},\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/author\\\/kevinkeovilay\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Ansible loops: A guide from basic to advanced examples - dbi Blog","description":"How to use loop in Ansible. Example from basics loops to advance loops, including conditional, retry and loop of loops.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dbi-services.com\/blog\/ansible-loops-a-guide-from-basic-to-advanced-examples\/","og_locale":"en_US","og_type":"article","og_title":"Ansible loops: A guide from basic to advanced examples","og_description":"How to use loop in Ansible. Example from basics loops to advance loops, including conditional, retry and loop of loops.","og_url":"https:\/\/www.dbi-services.com\/blog\/ansible-loops-a-guide-from-basic-to-advanced-examples\/","og_site_name":"dbi Blog","article_published_time":"2024-07-09T08:22:30+00:00","article_modified_time":"2024-07-09T08:22:33+00:00","author":"K\u00e9vin Keovilay","twitter_card":"summary_large_image","twitter_misc":{"Written by":"K\u00e9vin Keovilay","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/ansible-loops-a-guide-from-basic-to-advanced-examples\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/ansible-loops-a-guide-from-basic-to-advanced-examples\/"},"author":{"name":"K\u00e9vin Keovilay","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/3fb75c1e02be0c3b331471c8313cd9f7"},"headline":"Ansible loops: A guide from basic to advanced examples","datePublished":"2024-07-09T08:22:30+00:00","dateModified":"2024-07-09T08:22:33+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/ansible-loops-a-guide-from-basic-to-advanced-examples\/"},"wordCount":673,"commentCount":0,"keywords":["Ansible","devops"],"articleSection":["Ansible","DevOps"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/ansible-loops-a-guide-from-basic-to-advanced-examples\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/ansible-loops-a-guide-from-basic-to-advanced-examples\/","url":"https:\/\/www.dbi-services.com\/blog\/ansible-loops-a-guide-from-basic-to-advanced-examples\/","name":"Ansible loops: A guide from basic to advanced examples - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2024-07-09T08:22:30+00:00","dateModified":"2024-07-09T08:22:33+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/3fb75c1e02be0c3b331471c8313cd9f7"},"description":"How to use loop in Ansible. Example from basics loops to advance loops, including conditional, retry and loop of loops.","breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/ansible-loops-a-guide-from-basic-to-advanced-examples\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/ansible-loops-a-guide-from-basic-to-advanced-examples\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/ansible-loops-a-guide-from-basic-to-advanced-examples\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Ansible loops: A guide from basic to advanced examples"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/3fb75c1e02be0c3b331471c8313cd9f7","name":"K\u00e9vin Keovilay","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/aea984148a511f3db5117060e702df298f486588cee7781bb56a7bd92ac44a50?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/aea984148a511f3db5117060e702df298f486588cee7781bb56a7bd92ac44a50?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/aea984148a511f3db5117060e702df298f486588cee7781bb56a7bd92ac44a50?s=96&d=mm&r=g","caption":"K\u00e9vin Keovilay"},"url":"https:\/\/www.dbi-services.com\/blog\/author\/kevinkeovilay\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/33446","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/132"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=33446"}],"version-history":[{"count":10,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/33446\/revisions"}],"predecessor-version":[{"id":33952,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/33446\/revisions\/33952"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=33446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=33446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=33446"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=33446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}