Search code examples
linuxcurlgithub-apicloud-init

How to set and use variables in cloud-init?


I am trying to call an API and get a token to be used in the next API call at runcmd during cloud-config as part of Azure VMSS deployment with Terraform. How this can be achieved in cloud-init?

This is what I have tried, and it fails.

- [su, runner-admin, -c, 'REG_TOKEN=$(curl -sX POST -H "Accept: application/vnd.github.v3+json" -H "Authorization: token ${var.runner_manage_token}" https://api.github.com/orgs/${var.github_organisation}/actions/runners/registration-token | jq .token --raw-output)']
- [su, runner-admin, -c, '/actions-runner/config.sh --url https://github.com/${var.github_organisation} --token $${REG_TOKEN} --runnergroup ${var.runner_group}']

Is there any other way to achieve the same result?

Thanks


Solution

  • So, I ended up using write_files in cloud-init to solve my issue like this:

      write_files:    
        - path: /run/create-runner.sh
          content: |
            #!/bin/bash
            REG_TOKEN=$(curl -sX POST -H "Accept: application/vnd.github.v3+json" -H "Authorization: token ${var.runner_manage_token}" https://api.github.com/orgs/${var.github_organisation}/actions/runners/registration-token | jq .token --raw-output)
            /actions-runner/config.sh --unattended --url https://github.com/${var.github_organisation} --token $${REG_TOKEN} --runnergroup ${var.runner_group} --labels ${var.runner_labels} --replace
          owner: 'runner-admin'
          permissions: '0700'
          defer: true
    

    And then run it in runcmd:

      runcmd:
      - [mkdir, '/actions-runner']
      - cd /actions-runner
      - [curl, -o, 'actions-runner.tar.gz', -L, 'https://github.com/actions/runner/releases/download/v${var.runner_version}/actions-runner-linux-x64-${var.runner_version}.tar.gz']
      - [tar, -xzf, 'actions-runner.tar.gz']
      - [chmod, -R, 777, '/actions-runner']
      - [su, runner-admin, -c, /run/create-runner.sh]
      - ./svc.sh install runner-admin
      - ./svc.sh start
      - [rm, '/actions-runner/actions-runner.tar.gz']