I’m creating a GitLab server and project using Terraform.
I’m having a problem with the provider’s token.
I create a instance for the server, and with a script I define the token for GitLab.
"echo 'Creating GitLab access token script'",
"echo "user = User.find_by(username: 'root')" > create_token.rb",
"echo "token = user.personal_access_tokens.create!(name: 'Automated Token', scopes: [:api, :read_user, :read_repository, :write_repository, :sudo], expires_at: 30.days.from_now)" >> create_token.rb",
"echo "token.set_token('${var.gitlab_token}')" >> create_token.rb",
"echo "token.save!" >> create_token.rb",
"echo "File.open('/tmp/gitlab_token.txt', 'w') { |file| file.write(token.token) }" >> create_token.rb",
"sudo gitlab-rails runner -e production /home/ubuntu/create_token.rb"
After that I want to create a project using the GitLab provider:
provider "gitlab" {
token = var.gitlab_token
base_url = "https://<gitlab_server>/api/v4/"
}
resource "gitlab_project" "example" {...}
My problem is that when I define the gitlab_token manually it works (but security wise it isn’t good), but when I try using some kind of random definition (I treid random_password provider or openssl rand) it doesn’t work and I get:
Error: GET http://<ip>/api/v4/user: 401 {message: 401 Unauthorized}
│
│ with provider["registry.terraform.io/gitlabhq/gitlab"],.
I tested the token I created randomly with curl and I get the right response.
I also treid making sure the random token is a string but that didn’t help.
ישראל ושלומית is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.