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

Salt中pkgrepo.managed每次执行重复添加源行引发APT错误求助

Salt pkgrepo.managed 重复添加APT源行问题的解决

我在Salt配置文件中定义pkgrepo.managed状态部署New Relic基础设施代理的APT源,每次执行Salt时,都会在sources.list.d目录下的对应文件中重复添加完全相同的行,导致APT报错。谷歌搜索发现有其他用户遇到相同问题,但未找到解决方案。

我的配置代码

newrelic-infrastructure-repo:
    pkgrepo.managed:
      - humanname: Newrelic Infrastructure Agent
      - name: deb https://download.newrelic.com/infrastructure_agent/linux/apt xenial main
      - dist: stable
      - file: /etc/apt/sources.list.d/newrelic-infrastructure.list
      - require_in:
        - pkg: newrelic-infra-pkg
      - gpgcheck: 1
      - key_url: https://download.newrelic.com/infrastructure_agent/gpg/newrelic-infra.gpg

问题现象

执行上述状态后,原本预期/etc/apt/sources.list.d/newrelic-infrastructure.list文件仅包含一行源配置,但多次执行后文件中出现大量重复内容:

staging-i-0e4fb7971dd8c0f05:/etc/apt/sources.list.d$ cat newrelic-infrastructure.list
deb https://download.newrelic.com/infrastructure_agent/linux/apt stable main
deb https://download.newrelic.com/infrastructure_agent/linux/apt stable main
deb https://download.newrelic.com/infrastructure_agent/linux/apt stable main
deb https://download.newrelic.com/infrastructure_agent/linux/apt stable main
deb https://download.newrelic.com/infrastructure_agent/linux/apt stable main
deb https://download.newrelic.com/infrastructure_agent/linux/apt stable main
deb https://download.newrelic.com/infrastructure_agent/linux/apt stable main

问题原因

配置中name字段指定的发行版是xenial,但同时设置了dist: stable。Salt的pkgrepo.managed会用dist参数的值替换name里的发行版部分,最终写入文件的是deb https://download.newrelic.com/infrastructure_agent/linux/apt stable main。但Salt检查源是否存在时,会对比配置里的name(即带xenial的版本)和文件中的实际行,两者不匹配,所以每次执行都会判定源未配置,重复添加相同的行。

解决方法

修正配置,让name字段中的发行版与dist参数保持一致,或者直接移除dist参数,在name中写完整的正确源地址。

修正后的配置示例1(移除dist参数)

newrelic-infrastructure-repo:
    pkgrepo.managed:
      - humanname: Newrelic Infrastructure Agent
      - name: deb https://download.newrelic.com/infrastructure_agent/linux/apt stable main
      - file: /etc/apt/sources.list.d/newrelic-infrastructure.list
      - require_in:
        - pkg: newrelic-infra-pkg
      - gpgcheck: 1
      - key_url: https://download.newrelic.com/infrastructure_agent/gpg/newrelic-infra.gpg

修正后的配置示例2(保持dist参数,同步name中的发行版)

newrelic-infrastructure-repo:
    pkgrepo.managed:
      - humanname: Newrelic Infrastructure Agent
      - name: deb https://download.newrelic.com/infrastructure_agent/linux/apt stable main
      - dist: stable
      - file: /etc/apt/sources.list.d/newrelic-infrastructure.list
      - require_in:
        - pkg: newrelic-infra-pkg
      - gpgcheck: 1
      - key_url: https://download.newrelic.com/infrastructure_agent/gpg/newrelic-infra.gpg

修正后再执行Salt,就不会重复添加源行了,文件里只会保留一行正确的配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 12:44:52