【问题标题】:Azure: How to run a command as part of deployment process?Azure:如何在部署过程中运行命令?
【发布时间】:2021-02-17 10:23:38
【问题描述】:

我想在 Azure 公共云中部署一台 redHat 企业 Linux 机器,它的包都是最新的。

有什么方法可以在部署过程中或在机器首次启动后立即运行命令“yum update -y”(可能还有其他“yum install”命令)自动 ?

谢谢

【问题讨论】:

标签: linux azure redhat cloud-init


【解决方案1】:

在 Azure 上创建 VM 时,您可以在自定义数据部分下提供cloud-init 配置文件(采用 YAML 格式)。

#cloud-config
package_upgrade: true
packages:
  - nginx
  - nodejs
  - npm
runcmd:
  - yum update -y #same as package_upgrade: true
  - npm i express -y

文档link

【讨论】:

  • 您好,感谢您的回答。不幸的是,它似乎对我不起作用.. 正如你所建议的,在自定义数据窗口中,我通过了一个简短的 YAML 配置文件:#config-file package_upgrade: true runcmd: - mkdir /mydir #for sainty check None的软件包已更新,并且未创建目录“mydir”。你有什么原因吗?谢谢
【解决方案2】:

出于您的目的,我建议您使用VM extension 或CLI 命令az vm run-command invoke

az vm run-command invoke -g MyResourceGroup -n MyVm --command-id RunShellScript --scripts "yum update -y"

或 PowerShell 命令Invoke-AzVMRunCommand:

Invoke-AzVMRunCommand -ResourceGroupName 'rgname' -VMName 'vmname' -CommandId 'RunPowerShellScript' -ScriptPath 'sample.ps1' -Parameter @{param1 = "var1"; param2 = "var2"}

要更新虚拟机中的包,应该在虚拟机创建之后,因为虚拟机总是在那个时间供应虚拟机,而不是更新。所以我不认为 cloud-init 是一种可能的方式。

【讨论】:

    猜你喜欢
    • 2022-11-30
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多