<p>I don't know that you can specify ranges. On Linux, you can add a bash function such as from <a href="https://github.com/hashicorp/terraform/issues/2182#issuecomment-898798893" rel="nofollow noreferrer">here</a>.</p>
<p>A slight modification should work,</p>
<pre><code>terraform-replace () {
    sed 's/\x1b\[[0-9;]*m//g' | grep -o '# [^( ]* ' | grep '\.' | sed &quot; s/^# /-replace '/; s/ $/'/; &quot;
}
</code></pre>
<p>Then use it like this,</p>
<pre><code>terraform plan | terraform-replace | grep 'azurerm_windows_virtual_machine.vm\[[0-4]\]' | xargs -r terraform apply -auto-approve
</code></pre>

CC BY-SA 4.0
2022-04-14T12:13:44.133Z
Yes, it looks like you can pass a list of machines with single index value in each, separated by space. I suppose it is what it is.
Wojtas.Zet
CC BY-SA 4.0
2022-04-14T12:54:17.567Z
Right, this is what my answer does
superAnnoyingUser
CC BY-SA 4.0
2022-04-14T12:57:17.810Z
It is not necessary for you, I just find it useful to list targets, (if you would use the original from github).
superAnnoyingUser
CC BY-SA 4.0
2022-04-12T06:45:12.210Z
2022-04-12T08:07:37.180Z
2022-04-12T08:07:37.180Z
superAnnoyingUser
<p>I have 10 virtual machines to be re-created.
I want to do it in 2 groups, first 5 machines [0-4] and second 5 machines [5-9]</p>
<pre><code>terraform apply -replace=module.ci.azurerm_windows_virtual_machine.vm[0-4]
</code></pre>
<p>But it does not work, says:</p>
<pre><code>│ Index key must be followed by a closing bracket.
</code></pre>
<p>Is there any way to use a wildcard, or point a group of machines in general in command above?</p>

<terraform>
terraform apply - how to use wildcard or range for indexes?
Wojtas.Zet
# terraform apply - how to use wildcard or range for indexes?

(Asked by: username_0 on 2022-04-11)

I have 10 virtual machines to be re-created.
I want to do it in 2 groups, first 5 machines [0-4] and second 5 machines [5-9]
terraform apply -replace=module.ci.azurerm_windows_virtual_machine.vm[0-4]

But it does not work, says:
│ Index key must be followed by a closing bracket.

Is there any way to use a wildcard, or point a group of machines in general in command above?

**Tags:** terraform


---

## 1 answers

### Answer by username_1 on 2022-04-12. Score: 1 | Accepted answer

I don't know that you can specify ranges. On Linux, you can add a bash function such as from here.
A slight modification should work,
terraform-replace () {
    sed 's/\x1b\[[0-9;]*m//g' | grep -o '# [^( ]* ' | grep '\.' | sed " s/^# /-replace '/; s/ $/'/; "
}

Then use it like this,
terraform plan | terraform-replace | grep 'azurerm_windows_virtual_machine.vm\[[0-4]\]' | xargs -r terraform apply -auto-approve

> **Comments:**
> **username_0**: Yes, it looks like you can pass a list of machines with single index value in each, separated by space. I suppose it is what it is.  
> **username_1**: Right, this is what my answer does  
> **username_1**: It is not necessary for you, I just find it useful to list targets, (if you would use the original from github).  

---

