terraform apply - how to use wildcard or range for indexes?



  • 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?



  • I don't know that you can specify ranges. On Linux, you can add a bash function such as from https://github.com/hashicorp/terraform/issues/2182#issuecomment-898798893 .

    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
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2