首页 > 解决方案 > 使用 puppet 使 git 命令行变得丰富多彩

问题描述

我正在尝试使用 puppet 使 git 命令行变得丰富多彩并出现错误。我错过了什么?

exec { 'make-git-color':
  command => '/usr/bin/git config --global color.ui auto',
  logoutput => 'on_failure',
  user      => 'vagrant',
  timeout   => 1200,
  require   => Package['git']
}

错误是:

 /Exec[make-git-color]/returns: fatal: $HOME not set
Error: '/usr/bin/git config --global color.ui auto' returned 128 instead of one of [0]

直接运行的命令可以正常工作。/usr/bin/git config --global color.ui auto

但我需要通过木偶来做到这一点。

标签: gitpuppet

解决方案


如错误消息所述,未设置 $HOME。您需要将代码更改为这样的内容,以设置缺少的环境变量:

exec { 'make-git-color':
  command     => '/usr/bin/git config --global color.ui auto',
  logoutput   => 'on_failure',
  user        => 'vagrant',
  environment => 'HOME=/home/vagrant',
  require     => Package['git']
}

这会起作用(我测试过)。将环境变量传递给 exec 的文档在这里

请注意,我还删除了超时,这不是必需的。

如果您还需要确保幂等性,请根据以下评论将其更改为:

exec { 'make-git-color':
  command     => 'git config --global color.ui auto',
  unless      => 'git config --list --global | grep -q color.ui=auto',
  path        => '/usr/bin',
  logoutput   => 'on_failure',
  user        => 'vagrant',
  environment => 'HOME=/home/vagrant',
  require     => Package['git']
}

推荐阅读