After several months of investigations, with breaks, I finally realized what the problems were.
Is it normal that the keys in the map are sorted alphabetically?
I expected that as variables are added in this order they are, although it is not an array of data.
If this is normal behavior, then how can I have something similar to the one shown in the example, but in the same sequence as I set?
Yes, map keys are always iterated in lexicographical order.
To be able to suggest a solution, could you explain why the order of iteration is important? What are you trying to accomplish that is prevented by sorted map keys?
Hello guys,
I face to the same problem and can’t find a solution for the following situation. Let’s say I would like to deploy VM with fixed order of NICs, because my following configuration tool depends on it.
The same problem is nicely interpreted here as well. Could tell me how to get keys or values in the same sequence as it’s set in variable please?
@Koleon Maps don’t have an order, but arrays do (in programming concepts, versus traversal, as referred to above). In the post that you linked to, it seems that their underlying issue could be solved or approached from using for_each to enumerate the resource rather than count. If you’re referencing values in the map by their key, rather than an “index” you’ll always get your expected value, and you can add/remove items from the map without forcing a new resource.
When I’m reading this post you’ve linked to, this line:
Why are they trying to use these functions and accessing the values related to count.index? I suspect because the post is dated about when resource for_each was released, which is now at your disposal (on later versions of 0.12 and up).
When I add or change the order of values in network_conf, the terraform deploy do not preserve it. Is there a way how to change the datasource or resource loop please?
I’d have the same suggestion: Why are you using count here rather than for_each? You could do this same null check and have an empty object in order to get “0” resources created, no? Then your name is each.key
The general rule is if you want ordering use a list with count. Be aware that adding entries not at the end of the list (or removing entries not at the end) would cause multiple delete/create actions (as each entry is removed and replaced with the equivalent of the “next” resource).
If order doesn’t matter (which is often the case) use a map and for_each. You then have the advantage of only adding/removing anything you change, rather than other resources (as you would have with count & a list).
This is an old topic so I must admit I’ve only really skimmed the context up to this point, but I think this new comment is talking about dynamic blocks while the rest of the topic until this point has been talking about resource-level count and for_each.
Although both of these constructs use an argument named for_each, dynamic blocks are different in that they behave as a sort of macro that just generates a series of blocks for the provider to interpret however it wishes to. The resource repetition arguments, on the other hand, generate entirely new resource instances that each need to have a unique identifier.
Because of that difference, Terraform will allow any collection value as the for_each in a dynamic block, including a list. Whether the provider ultimately pays attention to the order of the blocks is a decision that varies depending on the resource type, but from what you’ve said here it seems like the provider does consider the order of component blocks to be significant, in which case using a dynamic block with a list as the for_each would be a reasonable thing to do here, generating blocks in the same order as your list elements.
With that said, the same caveat would apply as for using count with a whole resource: if you remove items from your list later, the provider is might not understand that you removed a component block from the middle of the list and so might understand it as deleting the last item and then updating various others in order to make the indices line up again.
You can use count on the main resource but that doesn’t help you on the dynamic attribute.
I would have sworn in the past I tried an array of strings and it didn’t work, but in the latest version, it looks to have worked.
The document below seems to indicate you need to convert the string array to a set, which is where the issue comes. As soon as you convert it to a set it updates the order.
However, since for_each [“b”, “c”, “a”] seems to work on a dynamic attribute I guess I am good.
The provider uses the order of the component as the order for the recipe.