You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过流水线读取Azure DevOps Agent的hosts文件内容?

可以通过Azure DevOps流水线脚本实现该需求

前置要求

  • 流水线作业需指定运行在目标Agent对应的自托管代理池,不要使用微软托管代理,否则读取到的是托管实例的hosts文件
  • Agent运行账号默认具备hosts文件的读取权限,无需额外配置管理员权限

具体操作步骤

根据Agent的操作系统选择对应脚本任务即可:

Windows系统Agent

添加PowerShell脚本任务,执行读取命令:

Get-Content -Path C:\Windows\System32\drivers\etc\hosts

YAML流水线配置示例:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: 'Get-Content -Path C:\Windows\System32\drivers\etc\hosts'
  displayName: '读取Windows Agent hosts文件'

Linux/macOS系统Agent

添加Bash脚本任务,执行读取命令:

cat /etc/hosts

YAML流水线配置示例:

- task: Bash@3
  inputs:
    targetType: 'inline'
    script: 'cat /etc/hosts'
  displayName: '读取Linux/macOS Agent hosts文件'

结果查看

流水线运行完成后,打开对应脚本任务的运行日志,即可看到完整的hosts文件内容,可直接用于排查域名解析类的CI/CD网络问题。

注意:如果hosts文件包含内部敏感IP、域名信息,建议调整该流水线的查看权限,仅授权给排查问题的相关人员,避免敏感信息泄露。

内容的提问来源于stack exchange,提问作者Kenny_I

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 15:36:04