linux 如何在cloud-init中设置和使用变量?

luaexgnf  于 2023-01-12  发布在  Linux
关注(0)|答案(1)|浏览(141)

作为使用Terraform部署Azure VMSS的一部分,我正在尝试调用API并获取令牌,以便在cloud-config期间在runcmd的下一个API调用中使用。如何在cloud-init中实现这一点?
这是我曾经尝试过的,但失败了。

- [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}']

有没有其他方法可以达到同样的效果?
谢谢

jm81lzqq

jm81lzqq1#

因此,我最终在cloud-init中使用write_files来解决我的问题,如下所示:

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

然后在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']

相关问题