bash - Bicep 文件中的部署后 bash 脚本不执行

标签 bash azure powershell azure-bicep

我想在 Azure 上部署 Ubuntu VM,并在部署 VM 后立即自动执行几行 Bash 代码。 Bash 代码应该在虚拟机上安装 PowerShell。为此,我使用此 Bicep file 。下面您可以看到 Bicep 文件的摘录,其中我指定了部署后要执行的 Bash 代码。

resource deploymentscript 'Microsoft.Compute/virtualMachines/runCommands@2022-08-01' = {
  parent: virtualMachine
  name: 'postDeploymentPSInstall'
  location: location
  properties: {
    source: {
      script: '''sudo apt-get update &&\
      sudo apt-get install -y wget apt-transport-https software-properties-common &&\
      wget -q "https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb" &&\
      sudo dpkg -i packages-microsoft-prod.deb &&\
      sudo apt-get update &&\
      sudo apt-get install -y powershell &&\
      pwsh'''
    }
  }
}

我在网上搜索了解决方案,但只发现了相互矛盾的解释。我在这个 tutorial 的帮助下编写了上面的代码。我发现的唯一区别是我使用的是 Bash,而不是像博文作者那样使用 PowerShell。感谢您的帮助。

最佳答案

To deploy an Ubuntu VM on Azure and automatically execute a few lines of Bash code right after the VM is deployed:

我尝试创建一个 Linux VM,并在部署时使用 run 命令在 VM 内安装 PowerShell,并且能够通过运行以下 来实现所需的结果二头肌 文件。

@description('Name of the Network Security Group')
param  networkSecurityGroupName  string = 'SecGroupNet'
var  publicIPAddressName = '${vmName}PublicIP'
var  networkInterfaceName = '${vmName}NetInt'
var  osDiskType = 'Standard_LRS'
var  subnetAddressPrefix = '10.1.0.0/24'
var  addressPrefix = '10.1.0.0/16'
var  linuxConfiguration = {
disablePasswordAuthentication: true
ssh: {
publicKeys: [
 {
path: '/home/${adminUsername}/.ssh/authorized_keys'
keyData: adminPassword
 }
]
}
}
resource  nic 'Microsoft.Network/networkInterfaces@2021-05-01' = {
name: networkInterfaceName
location: location
properties: {
ipConfigurations: [
{
name: 'ipconfig1'
properties: {
subnet: {
id: subnet.id
}
privateIPAllocationMethod: 'Dynamic'
publicIPAddress: {
id: publicIP.id
}
}
}
]
networkSecurityGroup: {
id: nsg.id
}
}
}
resource  nsg  'Microsoft.Network/networkSecurityGroups@2021-05-01' = {
name: networkSecurityGroupName
location: location
properties: {
securityRules: [
{
 name: 'SSH'
 properties: {
 priority: 1000
 protocol: 'Tcp'
 access: 'Allow'
 direction: 'Inbound'
 sourceAddressPrefix: '*'
 sourcePortRange: '*'
 destinationAddressPrefix: '*'
 destinationPortRange: '22'
  }
 }
]
}
}
resource  vnet  'Microsoft.Network/virtualNetworks@2021-05-01' = {
name: virtualNetworkName
location: location
properties: {
addressSpace: {
addressPrefixes: [
addressPrefix
]
}
}
}
resource  subnet  'Microsoft.Network/virtualNetworks/subnets@2021-05-01' = {
parent: vnet
name: subnetName
properties: {
addressPrefix: subnetAddressPrefix
privateEndpointNetworkPolicies: 'Enabled'
privateLinkServiceNetworkPolicies: 'Enabled'
}
}
resource  publicIP  'Microsoft.Network/publicIPAddresses@2021-05-01' = {
name: publicIPAddressName
location: location
sku: {
name: 'Basic'
}
properties: {
publicIPAllocationMethod: 'Dynamic'
publicIPAddressVersion: 'IPv4'
dnsSettings: {
domainNameLabel: dnsLabelPrefix
}
idleTimeoutInMinutes: 4
}
}
resource  vm  'Microsoft.Compute/virtualMachines@2021-11-01' = {
name: vmName
location: location
properties: {
hardwareProfile: {
vmSize: vmSize
}
storageProfile: {
osDisk: {
createOption: 'FromImage'
managedDisk: {
storageAccountType: osDiskType
}
}
imageReference: {
publisher: 'Canonical'
offer: 'UbuntuServer'
sku: ubuntuOSVersion
version: 'latest'
}
}
networkProfile: {
networkInterfaces: [
{
id: nic.id
}
]
}
osProfile: {
computerName: vmName
adminUsername: adminUsername
adminPassword: adminPassword
linuxConfiguration: ((authenticationType == 'password') ? null : linuxConfiguration)
}
}
}
resource  deploymentscript  'Microsoft.Compute/virtualMachines/runCommands@2022-03-01' = {
parent: vm
name: 'linuxscript'
location: location
properties: {
source: {
script: '''# Update the list of packages
sudo apt-get update;
#Install pre-requisite packages.
sudo apt-get install -y wget apt-transport-https software-properties-common;
#Download the Microsoft repository GPG keys
wget -q "https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb";
#Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb;
#Update the list of packages after we added packages.microsoft.com
sudo apt-get update;
#Install PowerShell
sudo apt-get install -y powershell;
#Start PowerShell
pwsh'''
}
}
}
output  adminUsername  string = adminUsername
output  hostname  string = publicIP.properties.dnsSettings.fqdn
output  sshCommand  string = 'ssh $ {adminUsername}@${publicIP.properties.dnsSettings.fqdn}'

部署成功:

enter image description here

enter image description here

来自 Azure 门户:

enter image description here

部署后, ssh 进入我的虚拟机并运行 Pwsh 检查是否安装了 PowerShell

安装成功:

enter image description here

引用MSDoc , run command template-MSDoc

关于bash - Bicep 文件中的部署后 bash 脚本不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74478948/

相关文章:

linux - 为什么 cd 会调用函数 ':' ?

bash - 使用 OpenSSL 生成质数

json - 如何添加一个新的 {key :value} per array element merging others identically structured JSON with jq

bash - 如何最好地包含其他脚本?

azure - Azure Web 应用程序中插槽的自定义域

c# - 如何通过选项配置设置自定义私有(private)DbContext包中的Azure默认TenantId

c# - 如何从 Azure 存储队列中的消息文本字段读取 JSON 字符串(使用 Azure 函数)?

powershell - 当在db2命令中使用时,PowerShell tee-Object在输出中生成空行

powershell - 在 ffmpeg 循环命令中出现关键字错误后,缺少开头 '('

c# - 在服务器上执行 powershell .ps1 文件并查看结果 C# asp.net