Azure 机器配置、Linux 和 DSC 配置

发布: (2026年1月14日 GMT+8 06:03)
4 min read
原文: Dev.to

Source: Dev.to

Azure机器配置、Linux和DSC配置的封面图片

介绍

Azure 机器配置支持 Linux 虚拟机,允许您将 PowerShell DSC 配置(或 Chef InSpec)应用于 Linux 工作负载。
要在 Linux 上运行 DSC 配置,虚拟机必须具备 AzurePolicyForLinux 扩展和受管身份。该扩展会将 PowerShell 安装在专用文件夹中,但不会将其添加到系统 PATH,因此只能由策略引擎调用 PowerShell。

Linux 的 DSC 资源

最完整的 Linux DSC 资源集合是 NxTools 模块。它提供了围绕常用 Linux 命令的 POSIX 风格包装,并包含以下 DSC 资源:

  • nxFiles – 管理文件和文件夹
  • nxGroup – 管理组及组成员关系
  • nxUser – 管理用户
  • nxPackages – 管理软件包(仅支持 apt
  • nxService – 管理服务(systemd)
  • nxScript – 在 DSC 配置中执行任意脚本

建议将该模块与 Azure Machine Configuration 一起使用。
GitHub: (link omitted in original)

演示 DSC 配置

下面的示例创建了一个名为 DevTo 的用户,将其添加到 publishers 组,并使用 nxScript 资源确保文件 /home/DevTo/publisher.txt 存在。

configuration demoDSCLinux
{
    Import-DscResource -ModuleName 'nxtools'

    nxUser ensureDevToUser {
        Ensure        = 'Present'
        UserName      = 'DevTo'
        FullName      = 'Dev To Demo user'
        HomeDirectory = '/home/DevTo'
        Description   = 'Ensure that DevTo user is present on the system'
    }

    nxGroup ensurePublishersGroup {
        # The group must be present and have DevTo as a member
        Ensure    = 'Present'
        GroupName = 'publishers'
        Members   = @('DevTo')
    }

    nxScript ensurePublisherFilePresent {
        GetScript = {
            $Reason = [Reason]::new()
            $Reason.Code   = "Script:Script:FileMissing"
            $Reason.Phrase = "File does not exist"

            if (Test-Path -Path "/home/DevTo/publisher.txt") {
                $Reason.Code   = "Script:Script:Success"
                $Reason.Phrase = "File exists"
            }

            return @{ Reasons = @($Reason) }
        }

        TestScript = {
            Test-Path -Path "/home/DevTo/publisher.txt"
        }

        SetScript = {
            $null = New-Item -Path "/home/DevTo/publisher.txt" -ItemType "File" -Force
        }
    }
}
  • 前两个资源创建 DevTo 用户和 publishers 组,并将该用户添加到组中。
  • nxScript 资源确保文件的存在,并提供一个 Reason 对象,当配置不符合要求时,Azure 门户会显示该对象。

编译和打包配置

DSC 配置必须在 Linux 主机(或在 Linux CI 运行器)上编译,因为 AzurePolicyForLinux 扩展需要 PowerShell for Linux。

# Verify the script is running on Linux
if ($IsLinux -eq $false) {
    Write-Error "This script must be run on a Linux system."
    exit 1
}

# Install required modules
Install-Module -Name nxtools -Force
Install-Module -Name PSDesiredStateConfiguration -RequiredVersion 3.0.0-beta1 -Force -AllowPrerelease
Install-Module -Name GuestConfiguration -RequiredVersion 4.1.0 -Force

# Load the DSC configuration script
. ./DSC-Linux/demolinux.dsc.ps1

# Compile the configuration
demoDSCLinux

# Rename the generated MOF file
Rename-Item -Path .\demoDSCLinux\localhost.mof -NewName demoDSCLinux.mof -Force

# Create the Guest Configuration package for Azure Machine Configuration
New-GuestConfigurationPackage `
    -Name "demoDSCLinux" `
    -Type AuditAndSet `
    -Configuration .\demoDSCLinux\demoDSCLinux.mof `
    -Force $true

脚本:

  1. 确认正在 Linux 上运行。
  2. 安装 nxtoolsPSDesiredStateConfigurationGuestConfiguration
  3. 导入 DSC 配置文件并编译,生成 MOF 文件。
  4. 将 MOF 打包成 Guest Configuration 包,可通过 Azure Machine Configuration 部署到 Linux 虚拟机。
Back to Blog

相关文章

阅读更多 »