1

I want to fetch sshkey with digital ocean token with get_sshkey.sh script:

do_token=$1


curl -X GET -s -H "Authorization: Bearer ${do_token//\"}" "https://api.digitalocean.com/v2/account/keys?page=1" | jq -r --arg queryname "User's key" '.ssh_keys[] | select(.name == $queryname).public_key'

I have a declared variable of DO token var.do-token, I am trying to use $1 bash concept to pass parameter to get_sshkey.sh and run it in terraform data "external" in following:

data "external" "fetchssh" {
  program = ["sh", "/input/get_sshkey.sh `echo "var.do-token" | terraform -chdir=/input console -var-file terraform.auto.tfvars`"]

}

but get the error: Expected a comma to mark the beginning of the next item.

1
  • Any updates on this? Have the same issue. Commented Jun 12, 2021 at 8:40

1 Answer 1

1

In order to include literal quotes in your shell command, you'll need to escape them so that Terraform can see that you don't intend to end the quoted string template:

data "external" "fetchssh" {
  program = ["sh", "/input/get_sshkey.sh `echo \"var.do-token\" | terraform -chdir=/input console -var-file terraform.auto.tfvars`"]
}

Using the external data source in Terraform to recursively run another terraform is a pretty irregular thing to do, but as long as there's such a variable defined in that configuration I suppose it could work.

Sign up to request clarification or add additional context in comments.

1 Comment

This returns Error: failed to execute "sh": sh: can't open '/input/get_sshkey.sh echo "var.do-token" | terraform -chdir=/input console -var-file terraform.auto.tfvars': No such file or directory, however if I run '/input/get_sshkey.sh "echo "var.do-token" | terraform -chdir=/input console -var-file terraform.auto.tfvars" separately it returns appropriate token hash: sh: "tokenhash": not found

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.