2

What I'm trying to do: I have set up a self hosted gitlab instance, and I'm working on automating the terraform process with GitLab CI, for which I have to set up a terraform backend. I followed the steps in the official documentation, but unfortunately I run into an error when running the pipeline. This code is found in my .gitlab-ci.yml file, so it runs on my GitLab runner, which uses a docker image based on python:3.9-buster (Debian)

Here is the code in question, of course I edited out the IP address:

- terraform init \ 
        -backend-config="address=http://ipaddress//api/v4/projects/3/terraform/state/terraform-state/" \ 
        -backend-config="lock_address=http://ipaddress//api/v4/projects/3/terraform/state/terraform-state/lock" \ 
        -backend-config="unlock_address=http://ipaddress//api/v4/projects/3/terraform/state/terraform-state/lock" \ 
        -backend-config="username=root" \ 
        -backend-config="password=$ACCESSTOKEN" \ 
        -backend-config="lock_method=POST" \ 
        -backend-config="unlock_method=DELETE" \ 
        -backend-config="retry_wait_min=5"

Unfortunately I'm getting this error when the above command gets executed:

Too many command line arguments. Did you mean to use -chdir?
Bonewind
  • 41
  • 1
  • 5

2 Answers2

2

Just in case anyone would get stuck on a similar issue and happens to find this post, changing my script to this format solved the issue:

  - export TF_ADDRESS=http://ipaddress//api/v4/projects/3/terraform/state/terraform-state/
  - export TF_HTTP_ADDRESS=${TF_ADDRESS}
  - export TF_HTTP_LOCK_ADDRESS=${TF_ADDRESS}/lock
  - export TF_HTTP_LOCK_METHOD=POST
  - export TF_HTTP_UNLOCK_ADDRESS=${TF_ADDRESS}/lock
  - export TF_HTTP_UNLOCK_METHOD=DELETE
  - export TF_HTTP_USERNAME=root
  - export TF_HTTP_PASSWORD=$ACCESSTOKEN
  - export TF_HTTP_RETRY_WAIT_MIN=5
  - terraform init -reconfigure
Bonewind
  • 41
  • 1
  • 5
  • For what it's worth, when Terraform talks about "command line arguments" here it's talking about positional arguments, without a leading dash, rather than options like `-backend-config` (which Terraform calls "options"). I'm not sure exactly what was going wrong here but since what you shared seems to be a script embedded in some other syntax (YAML?) I wonder if something's getting lost in the YAML decoding that's causing Terraform to see a different set of command line arguments than it might appear. Environment variables are another reasonable way to get it done, though! – Martin Atkins Jul 08 '21 at 19:09
0

Block Scalar also works

- >- 
      terraform init
      -backend-config="address=${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/terraform/state/${TF_STATE_NAME}"
      -backend-config="lock_address=${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/terraform/state/${TF_STATE_NAME}/lock"
      -backend-config="unlock_address=${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/terraform/state/${TF_STATE_NAME}/lock"
      -backend-config="username=${USERNAME}"
      -backend-config="password=${TF_BOT_GLPAT}"
      -backend-config="lock_method=POST"
      -backend-config="unlock_method=DELETE"
      -backend-config="retry_wait_min=5"
      -backend-config="skip_cert_verification=true"  
soulmanos
  • 26
  • 5