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

Puppet v7.27.0模块无法执行文件复制与运行求助

Puppet模块未执行问题排查与解决方案

我是Puppet v7.27.0新手,基于voxpupuli/crafty的Docker Compose部署,想要创建模块实现两个功能:

  1. 将服务器上的二进制文件复制到代理节点
  2. 执行该文件

参考文档及旧示例均无效,当前使用临时模块名dsvuhtqkng,文件结构如下:

root@puppet:/etc/puppetlabs/code/environments/production/modules# ls -lahR
.:
total 16K
drwxr-xr-x 1 root root 4.0K Dec 17 21:59 .
drwxr-xr-x 1 root root 4.0K Dec 15 10:22 ..
drwxr-xr-x 4 root root 4.0K Dec 17 21:51 dsvuhtqkng

./dsvuhtqkng:
total 16K
drwxr-xr-x 4 root root 4.0K Dec 17 21:51 .
drwxr-xr-x 1 root root 4.0K Dec 17 21:59 ..
drwxr-xr-x 2 root root 4.0K Dec 17 21:51 files
drwxr-xr-x 2 root root 4.0K Dec 17 21:51 manifests

./dsvuhtqkng/files:
total 12K
drwxr-xr-x 2 root root 4.0K Dec 17 21:51 .
drwxr-xr-x 4 root root 4.0K Dec 17 21:51 ..
-rwx------ 1 root root  250 Dec 17 21:51 wget

./dsvuhtqkng/manifests:
total 16K
drwxr-xr-x 2 root root 4.0K Dec 17 21:51 .
drwxr-xr-x 4 root root 4.0K Dec 17 21:51 ..
-rw-r--r-- 1 root root   53 Dec 17 21:51 init.pp
-rw-r--r-- 1 root root  243 Dec 17 21:51 binary.pp

init.pp代码:

class dsvuhtqkng () {
  contain dsvuhtqkng::binary
}

也尝试过去掉(),用include替代contain。

binary.pp代码:

class binary {
      file { '/tmp/wget':
    ensure => file,
    source => 'puppet:///modules/dsvuhtqkng/wget',
    owner  => root,
    group  => root,
    mode   => '775',
  }
  exec { '/tmp/wget' }
}

执行puppet module list可识别该模块:

root@puppet:/etc/puppetlabs/code/environments/production/modules# puppet module list
/etc/puppetlabs/code/environments/production/modules
└── dsvuhtqkng (???)
/etc/puppetlabs/code/modules (no modules installed)
/opt/puppetlabs/puppet/modules (no modules installed)

但运行puppet agent -t时模块未执行:

root@puppet:/etc/puppetlabs/code/environments/production/modules# puppet agent -t
Info: Using environment 'production'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for puppet
Info: Applying configuration version '1702848637'
Notice: Applied catalog in 0.04 seconds

怀疑init.pp与binary.pp的衔接存在问题,寻求解决建议,同时希望能将所有逻辑整合到init.pp中。


问题原因与解决步骤

核心问题点

  1. 类名命名错误:binary.pp中的类名应为dsvuhtqkng::binary而非binary,否则Puppet无法通过contain dsvuhtqkng::binary定位到该类。
  2. Exec资源语法缺失:exec { '/tmp/wget' }缺少必要参数,且未设置依赖关系,会导致执行操作在文件复制完成前触发。
  3. 模块未纳入节点配置:即使模块结构正确,也需要在节点定义中添加include dsvuhtqkng,否则Agent不会加载该模块。

修正后的代码方案

方案1:修复原模块结构

修改dsvuhtqkng/manifests/binary.pp的类名及资源配置:

class dsvuhtqkng::binary {
  file { '/tmp/wget':
    ensure => file,
    source => 'puppet:///modules/dsvuhtqkng/wget',
    owner  => 'root',  # root需加引号,避免被识别为变量
    group  => 'root',
    mode   => '0775',  # 权限建议添加前导0表示八进制格式
  }

  exec { 'run_wget_binary':
    command => '/tmp/wget',
    require => File['/tmp/wget'],  # 确保文件复制完成后再执行
    path    => '/usr/bin:/bin',    # 指定PATH环境变量,避免执行报错
    onlyif  => 'test -x /tmp/wget', # 仅当文件具备可执行权限时运行
  }
}

同时确保节点配置文件(如/etc/puppetlabs/code/environments/production/manifests/site.pp)中包含模块引用:

node 'puppet' {  # 替换为你的代理节点主机名
  include dsvuhtqkng
}

方案2:整合所有逻辑到init.pp

直接修改dsvuhtqkng/manifests/init.pp,删除binary.pp文件:

class dsvuhtqkng {
  file { '/tmp/wget':
    ensure => file,
    source => 'puppet:///modules/dsvuhtqkng/wget',
    owner  => 'root',
    group  => 'root',
    mode   => '0775',
  }

  exec { 'run_wget_binary':
    command => '/tmp/wget',
    require => File['/tmp/wget'],
    path    => '/usr/bin:/bin',
    onlyif  => 'test -x /tmp/wget',
  }
}

同样需要在site.pp中添加节点引用:

node 'puppet' {
  include dsvuhtqkng
}

验证步骤

  1. 先在Master端验证语法正确性:puppet parser validate /etc/puppetlabs/code/environments/production/modules/dsvuhtqkng/manifests/*.pp
  2. 运行puppet agent -t,观察输出日志确认文件复制及执行操作是否生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 16:20:16