- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 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}'
部署成功:
来自 Azure 门户:
部署后, ssh
进入我的虚拟机并运行 Pwsh
检查是否安装了 PowerShell
安装成功:
关于bash - Bicep 文件中的部署后 bash 脚本不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74478948/
我能够使用最新的 Bicep 版本 0.4 创建应用服务计划和应用服务。 resource myAppServicePlan 'Microsoft.Web/serverfarms@2021-01-01
我有一个二头肌文件,应该创建一个虚拟网络网关。我通过创建虚拟网络的单独二头肌文件将其分解。我真正应该做的是有一个用于 vnet 创建的模块和另一个用于虚拟网络网关的模块,因为 bicep 对我来说是新
我正在尝试使用 bicep 将 apim 后端资源部署到现有 apim 实例中。这是我的 apim mdoule 的相关部分: resource apim 'Microsoft.ApiManageme
我想生成一个二头肌来构建逻辑应用程序。其样板是 resource logicApp 'Microsoft.Logic/workflows@2019-05-01' = { name: 'lapp-$
像往常一样与二头肌文档碰撞。我似乎找不到任何有关如何向自动化帐户 Runbook 的作业计划添加参数的帮助。 MS 的示例是 name: 'string' parent: resourceSy
我的情况是,我有一个公司列表,以及一个我想在我的二头肌文件中配对的队列列表。我想使用结果将队列添加到服务总线命名空间。 这是队列数组的示例: `var queues = [ 'first-queu
在二头肌中,我有一个主要的二头肌,它会调用 部署应用服务的模块 部署托管身份的模块 应用程序服务模块如下所示,它使用用户分配的托管 ID 的输出,并在应用程序服务的身份中分配: 主要二头肌 modul
我有一个应用程序,它有一个二头肌文件(用于部署 azure 应用程序服务),它调用另一个存储库中的模板模块(应用程序服务模板)。模板模块引用同一存储库中的多个模块。它调用一个用于应用服务计划的模块,一
尝试将托管标识分配给同一租户中单独订阅中的资源组。我可以通过 Azure 门户实现此目的。我的 Main.bicep,类似这样: targetScope = 'subscription' module
我目前正在尝试手动将现有 ARM 模板转换为二头肌,因为自动反编译方法失败。 现有模板包含以下变量: "environmentSize": { "dev": "small",
我正在运行二头肌 0.4.1318。 我有一个“主”二头肌模块,它调用子模块来配置数据工厂: var adfName = 'adf-ctaxrebate-${envPlusSuffix}-${form
我有一个 Azure Bicep 模板,用于部署存储帐户和容器。但是,当我重命名容器或注释掉模板中的容器资源时,不会触发删除操作。相反,它会创建一个新容器并且不会删除旧容器。有什么想法为什么会发生这种
我想创建一个 Azure 容器应用程序,该应用程序从位于不同订阅中的现有 Azure 容器应用程序注册表中提取其镜像。 我的主要二头肌: 创建用户分配的托管身份 将托管身份的 ACR Pull RBA
我刚开始使用 Azure 二头肌。我正在尝试使用以下二头肌文件部署一个简单的应用程序服务计划/应用程序服务: resource azBicepAsp1 'Microsoft.Web/serverfar
我想要一个可以向现有应用服务(本例中为函数应用)添加一组固定的 IP 限制规则的模块。 我在 main.bicep 中添加了对“ipSecurityRestrictions”模块的调用,如下所示: m
如何使用 bicep 更新字典对象,例如:原始字典对象: var dict = { 'a': {} } 我现在有一个数组:['b', 'c'],我想更新 dict 对象,例如: { 'a':
我需要: 创建数据工厂 创建存储帐户 创建函数应用 将数据工厂的角色分配添加到存储帐户 将函数应用的角色分配添加到存储帐户 数据工厂是在与“主”二头肌不同的模块中创建的。这是为了防止“主”模板太大而难
我正在尝试引用现有的二头肌模块作为另一个资源的父级。 module vnethub 'modules/vnet/vnet.bicep' = { scope: resourceGroup(rg.na
我正在尝试创建一个二头肌模板来根据条件部署具有 1 个或 2 个 NIC 的虚拟机。 有人知道是否有一种方法可以在属性定义中使用条件语句来部署虚拟机网卡吗?似乎资源定义中不允许使用 if 函数,并且由
我正在尝试使用以下代码创建一个简单的应用服务计划。 param Location string = 'eastus' resource appServicePlan1 'Microsoft.Web/s
我是一名优秀的程序员,十分优秀!