<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/ansible-set-operations-do-not-preserve-list-order-ipspace-net-blog-pa9mqlpwb" -->

---
title: Ansible Set Operations Do Not Preserve List Order «...
description: Ansible's `intersect` filter does not preserve list order because it internally converts both arguments to Python sets, which are unordered collections. This...
canonical: https://daily.dev/posts/ansible-set-operations-do-not-preserve-list-order-ipspace-net-blog-pa9mqlpwb
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Ansible Set Operations Do Not Preserve List Order « ipSpace.net blog | daily.dev
og:description: Ansible's `intersect` filter does not preserve list order because it internally converts both arguments to Python sets, which are unordered collections. This...
og:url: https://daily.dev/posts/ansible-set-operations-do-not-preserve-list-order-ipspace-net-blog-pa9mqlpwb
og:image: https://api.daily.dev/og/posts/pA9MqLPWb.png
og:image:alt: Ansible Set Operations Do Not Preserve List Order « 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.

# Ansible Set Operations Do Not Preserve List Order « ipSpace.net blog

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

## Summary

Ansible's `intersect` filter does not preserve list order because it internally converts both arguments to Python sets, which are unordered collections. This causes non-deterministic ordering of configuration modules — a real problem when deployment order matters (e.g., VLANs must be configured before OSPF). The fix is to replace `intersect` with a `when` condition that filters items while iterating over the original ordered list, preserving the intended sequence.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://blog.ipspace.net/2024/02/ansible-set-operations>

## Questions this post answers

### Does Ansible's intersect filter preserve the order of elements in a list?

No, Ansible's `intersect` filter does not preserve list order. It converts both input arguments into Python sets before performing the intersection, and Python sets are unordered collections, so the resulting list can come back in a completely different and non-deterministic order. The workaround is to loop over the original ordered list and use a `when: item in other_list` condition instead of `intersect`.

_Developers debugging non-deterministic Ansible playbook behavior track quirks like this on daily.dev._

## Similar posts on daily.dev

- [Deep Dive: Automating NetOps with the cisco.catalystcenter Ansible Collection](https://daily.dev/posts/deep-dive-automating-netops-with-the-cisco-catalystcenter-ansible-collection-2bstaaosj) · Cisco · 0 upvotes · 0 comments
- [Netmiko vs Ansible: Does It Matter? « ipSpace.net blog](https://daily.dev/posts/netmiko-vs-ansible-does-it-matter-ipspace-net-blog-pmun2fqpj) · ipSpace.net Blog · 1 upvotes · 1 comments
- [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 Configuration Drift Management, Detection & Fixes](https://daily.dev/posts/ansible-configuration-drift-management-detection-fixes-1zgvgsewr) · Spacelift · 1 upvotes · 0 comments
- [Terraform Dependency Management: When Ordering Matters More Than You Think](https://daily.dev/posts/terraform-dependency-management-when-ordering-matters-more-than-you-think-zfx5ncdth) · Build5Nines · 0 upvotes · 0 comments

---

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

[View this post on daily.dev](https://daily.dev/posts/ansible-set-operations-do-not-preserve-list-order-ipspace-net-blog-pa9mqlpwb)

```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":"Ansible Set Operations Do Not Preserve List Order « ipSpace.net blog","url":"https://daily.dev/posts/ansible-set-operations-do-not-preserve-list-order-ipspace-net-blog-pa9mqlpwb","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/ansible-set-operations-do-not-preserve-list-order-ipspace-net-blog-pa9mqlpwb"},"datePublished":"2026-08-11T08:55:11.006Z","dateModified":"2026-08-24T06:51:12.053Z","description":"Ansible's `intersect` filter does not preserve list order because it internally converts both arguments to Python sets, which are unordered collections. This...","image":"https://media.daily.dev/image/upload/s--58gMhC4P--/f_auto/v1722860399/public/Placeholder%2012","thumbnailUrl":"https://media.daily.dev/image/upload/s--58gMhC4P--/f_auto/v1722860399/public/Placeholder%2012","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/ansible-set-operations-do-not-preserve-list-order-ipspace-net-blog-pa9mqlpwb","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"python,networking,ansible","timeRequired":"PT3M"}
{"@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":"Ansible Set Operations Do Not Preserve List Order « ipSpace.net blog"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/ansible-set-operations-do-not-preserve-list-order-ipspace-net-blog-pa9mqlpwb#faq","mainEntity":[{"@type":"Question","name":"Does Ansible's intersect filter preserve the order of elements in a list?","acceptedAnswer":{"@type":"Answer","text":"No, Ansible's `intersect` filter does not preserve list order. It converts both input arguments into Python sets before performing the intersection, and Python sets are unordered collections, so the resulting list can come back in a completely different and non-deterministic order. The workaround is to loop over the original ordered list and use a `when: item in other_list` condition instead of `intersect`. Developers debugging non-deterministic Ansible playbook behavior track quirks like this on daily.dev."}}]}
```

