<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/beware-ansible-reorders-list-values-in-loops-ipspace-net-blog-7a7peyivs" -->

---
title: Beware: Ansible Reorders List Values in Loops «...
description: Ansible has a subtle but critical bug: when using `include_tasks` with a `loop` parameter across multiple hosts in a batch, Ansible may reorder the loop...
canonical: https://daily.dev/posts/beware-ansible-reorders-list-values-in-loops-ipspace-net-blog-7a7peyivs
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Beware: Ansible Reorders List Values in Loops « ipSpace.net blog | daily.dev
og:description: Ansible has a subtle but critical bug: when using `include_tasks` with a `loop` parameter across multiple hosts in a batch, Ansible may reorder the loop...
og:url: https://daily.dev/posts/beware-ansible-reorders-list-values-in-loops-ipspace-net-blog-7a7peyivs
og:image: https://api.daily.dev/og/posts/7a7PeYIVs.png
og:image:alt: Beware: Ansible Reorders List Values in Loops « ipSpace.net blog
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Beware: Ansible Reorders List Values in Loops « ipSpace.net blog

**[ipSpace.net Blog](https://daily.dev/sources/ipspace)** · 6 min read · 0 upvotes · 0 comments

## Summary

Ansible has a subtle but critical bug: when using `include_tasks` with a `loop` parameter across multiple hosts in a batch, Ansible may reorder the loop iterations based on the first host's list order, ignoring the per-host ordering. This causes incorrect execution order — for example, deploying an MPLS module before BGP is configured. The bug affects `include_tasks` but not regular tasks. Adding a `when` condition to `include_tasks` also triggers the reordering, even when iterating over a shared global list. The only reliable workarounds are setting `serial: 1` (slow) or moving the `when` condition inside the included task file so the top-level includes always execute unconditionally.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://blog.ipspace.net/2022/03/ansible-reorder-list-values>

## Questions this post answers

### Why does Ansible include_tasks execute loop items in the wrong order when running across multiple hosts?

When using `include_tasks` with a `loop` across multiple hosts in a batch, Ansible optimizes by using the loop order of the first host in the batch for all hosts. If different hosts have different orderings of the same list variable, the actual execution order follows the first host's order, not each host's own order. This affects Ansible versions 2.9 through 5.5 and does not affect regular (non-include) tasks.

_Teams running order-sensitive Ansible automation track gotchas like this on daily.dev before they hit production._

### How do I fix Ansible include_tasks reordering loop values across hosts?

Two workarounds exist. Setting `serial: 1` forces Ansible to process one host at a time, eliminating the reordering but slowing deployment significantly. Alternatively, move the `when` condition inside the included task file rather than on the `include_tasks` directive itself — unconditional top-level includes preserve order, while a `when` on `include_tasks` triggers the same reordering bug even with a shared global loop list.

_Ansible automation engineers dealing with ordering bugs like this share fixes on daily.dev._

## Similar posts on daily.dev

- [How to collaborate with AI to improve your Ansible skills](https://daily.dev/posts/how-to-collaborate-with-ai-to-improve-your-ansible-skills-izxeypqlm) · Red Hat Developer · 0 upvotes · 0 comments
- [Ansible Security Automation: Risks & 7 Best Practices](https://daily.dev/posts/ansible-security-automation-risks-7-best-practices-lvgbynirc) · Spacelift · 2 upvotes · 0 comments
- [How To Define and Use Handlers in Ansible Playbooks](https://daily.dev/posts/how-to-define-and-use-handlers-in-ansible-playbooks-gdaa6qawd) · DigitalOcean Community · 3 upvotes · 0 comments
- [If It Quacks Like a Package Manager](https://daily.dev/posts/if-it-quacks-like-a-package-manager-rsetoxikw) · Lobsters · 0 upvotes · 0 comments

---

Tags: [#networking](https://daily.dev/tags/networking), [#ansible](https://daily.dev/tags/ansible)

[View this post on daily.dev](https://daily.dev/posts/beware-ansible-reorders-list-values-in-loops-ipspace-net-blog-7a7peyivs)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Beware: Ansible Reorders List Values in Loops « ipSpace.net blog","url":"https://daily.dev/posts/beware-ansible-reorders-list-values-in-loops-ipspace-net-blog-7a7peyivs","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/beware-ansible-reorders-list-values-in-loops-ipspace-net-blog-7a7peyivs"},"datePublished":"2026-08-11T08:52:46.144Z","dateModified":"2026-08-24T06:52:43.700Z","description":"Ansible has a subtle but critical bug: when using `include_tasks` with a `loop` parameter across multiple hosts in a batch, Ansible may reorder the loop...","image":"https://media.daily.dev/image/upload/s--2-1xRawN--/f_auto/v1722860399/public/Placeholder%2011","thumbnailUrl":"https://media.daily.dev/image/upload/s--2-1xRawN--/f_auto/v1722860399/public/Placeholder%2011","isAccessibleForFree":true,"articleSection":"ipSpace.net Blog","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"ipSpace.net Blog","logo":"https://media.daily.dev/image/upload/s--xS7E82MT--/f_auto,q_auto/v1786438303/logos/ipspace?_a=BAMAMicg0","url":"https://daily.dev/sources/ipspace"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/beware-ansible-reorders-list-values-in-loops-ipspace-net-blog-7a7peyivs","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"networking,ansible","timeRequired":"PT6M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"ipSpace.net Blog","item":"https://daily.dev/sources/ipspace"},{"@type":"ListItem","position":3,"name":"Beware: Ansible Reorders List Values in Loops « ipSpace.net blog"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/beware-ansible-reorders-list-values-in-loops-ipspace-net-blog-7a7peyivs#faq","mainEntity":[{"@type":"Question","name":"Why does Ansible include_tasks execute loop items in the wrong order when running across multiple hosts?","acceptedAnswer":{"@type":"Answer","text":"When using `include_tasks` with a `loop` across multiple hosts in a batch, Ansible optimizes by using the loop order of the first host in the batch for all hosts. If different hosts have different orderings of the same list variable, the actual execution order follows the first host's order, not each host's own order. This affects Ansible versions 2.9 through 5.5 and does not affect regular (non-include) tasks. Teams running order-sensitive Ansible automation track gotchas like this on daily.dev before they hit production."}},{"@type":"Question","name":"How do I fix Ansible include_tasks reordering loop values across hosts?","acceptedAnswer":{"@type":"Answer","text":"Two workarounds exist. Setting `serial: 1` forces Ansible to process one host at a time, eliminating the reordering but slowing deployment significantly. Alternatively, move the `when` condition inside the included task file rather than on the `include_tasks` directive itself — unconditional top-level includes preserve order, while a `when` on `include_tasks` triggers the same reordering bug even with a shared global loop list. Ansible automation engineers dealing with ordering bugs like this share fixes on daily.dev."}}]}
```

