<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/precedence-of-ansible-extra-variables-ipspace-net-blog-o2ar3ieh1" -->

---
title: Precedence of Ansible Extra Variables « ipSpace.net blog
description: Ansible's extra variables (passed via `-e` on the command line) have the highest precedence in its variable system, which means `set_fact` cannot override...
canonical: https://daily.dev/posts/precedence-of-ansible-extra-variables-ipspace-net-blog-o2ar3ieh1
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Precedence of Ansible Extra Variables « ipSpace.net blog | daily.dev
og:description: Ansible's extra variables (passed via `-e` on the command line) have the highest precedence in its variable system, which means `set_fact` cannot override...
og:url: https://daily.dev/posts/precedence-of-ansible-extra-variables-ipspace-net-blog-o2ar3ieh1
og:image: https://api.daily.dev/og/posts/O2Ar3iEH1.png
og:image:alt: Precedence of Ansible Extra Variables « 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.

# Precedence of Ansible Extra Variables « ipSpace.net blog

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

## Summary

Ansible's extra variables (passed via `-e` on the command line) have the highest precedence in its variable system, which means `set_fact` cannot override them. This creates a subtle but dangerous gotcha: if you pass a variable as an extra variable and then try to transform it inside a playbook (e.g., splitting a string into a list), the transformed value is silently ignored and the original extra variable value persists. The post walks through concrete playbook examples demonstrating this behavior, then offers practical lessons: never reuse input variables, always create new variables for transformed values, and consider a naming convention (e.g., underscore prefix) for internal variables to reduce accidental overrides. A bonus section covers Python's behavior when splitting an empty string, which returns a list containing one empty string rather than an empty list.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://blog.ipspace.net/2024/01/ansible-extra-variable-precedence>

## Questions this post answers

### Why does set_fact not override a variable passed with -e in Ansible?

Ansible maintains multiple values for each variable, each with a precedence level, and selects the highest-precedence value at evaluation time. Extra variables passed via `-e` have the highest precedence in Ansible's system, so `set_fact` — which has lower precedence — cannot overwrite them. The original `-e` value persists even after `set_fact` runs, which is why the debug output shows the command-line value rather than the one set in the task.

_Teams writing Ansible playbooks with dynamic variable transforms track gotchas like this on daily.dev._

### What happens when I try to split an empty string using Ansible's split filter?

Splitting an empty string with Ansible's `split` filter returns a list containing one empty string — `['']` — not an empty list. This is because Ansible's `split` filter is a direct passthrough to Python's `str.split()` method, which behaves the same way. To get an empty list when the input is empty, use the conditional expression `x.split(',') if x else []`.

_Developers working around Ansible filter edge cases find practical workarounds discussed on daily.dev._

## Similar posts on daily.dev

- [Environment variables are a legacy mess: Let's dive deep into them](https://daily.dev/posts/environment-variables-are-a-legacy-mess-let-s-dive-deep-into-them-ziqlt8wpu) · Hacker News · 1 upvotes · 0 comments
- [Use Ansible Vault to Protect Sensitive Playbook Data](https://daily.dev/posts/use-ansible-vault-to-protect-sensitive-playbook-data-pvxp9e6ud) · DigitalOcean Community · 5 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 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
- [The Bug That Made Me Question Reality for a Few Hours](https://daily.dev/posts/the-bug-that-made-me-question-reality-for-a-few-hours-ndxvzfmjj) · DEV · 157 upvotes · 13 comments

---

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

[View this post on daily.dev](https://daily.dev/posts/precedence-of-ansible-extra-variables-ipspace-net-blog-o2ar3ieh1)

```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":"Precedence of Ansible Extra Variables « ipSpace.net blog","url":"https://daily.dev/posts/precedence-of-ansible-extra-variables-ipspace-net-blog-o2ar3ieh1","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/precedence-of-ansible-extra-variables-ipspace-net-blog-o2ar3ieh1"},"datePublished":"2026-08-11T08:55:41.994Z","dateModified":"2026-08-11T09:40:04.776Z","description":"Ansible's extra variables (passed via `-e` on the command line) have the highest precedence in its variable system, which means `set_fact` cannot override...","image":"https://media.daily.dev/image/upload/s--ZrL_HSsR--/f_auto/v1722860399/public/Placeholder%2006","thumbnailUrl":"https://media.daily.dev/image/upload/s--ZrL_HSsR--/f_auto/v1722860399/public/Placeholder%2006","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/precedence-of-ansible-extra-variables-ipspace-net-blog-o2ar3ieh1","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"ansible","timeRequired":"PT5M"}
{"@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":"Precedence of Ansible Extra Variables « ipSpace.net blog"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/precedence-of-ansible-extra-variables-ipspace-net-blog-o2ar3ieh1#faq","mainEntity":[{"@type":"Question","name":"Why does set_fact not override a variable passed with -e in Ansible?","acceptedAnswer":{"@type":"Answer","text":"Ansible maintains multiple values for each variable, each with a precedence level, and selects the highest-precedence value at evaluation time. Extra variables passed via `-e` have the highest precedence in Ansible's system, so `set_fact` — which has lower precedence — cannot overwrite them. The original `-e` value persists even after `set_fact` runs, which is why the debug output shows the command-line value rather than the one set in the task. Teams writing Ansible playbooks with dynamic variable transforms track gotchas like this on daily.dev."}},{"@type":"Question","name":"What happens when I try to split an empty string using Ansible's split filter?","acceptedAnswer":{"@type":"Answer","text":"Splitting an empty string with Ansible's `split` filter returns a list containing one empty string — `['']` — not an empty list. This is because Ansible's `split` filter is a direct passthrough to Python's `str.split()` method, which behaves the same way. To get an empty list when the input is empty, use the conditional expression `x.split(',') if x else []`. Developers working around Ansible filter edge cases find practical workarounds discussed on daily.dev."}}]}
```

