与 Chef 中的 'ignore_failure' 或 Ansible 中的 'ignore_errors' 等价的 Puppet 是什么?

What is Puppet equivalent of the 'ignore_failure' in Chef or the 'ignore_errors' in Ansible?

我在 Puppet Master 上有以下清单:

exec { 'DatabaseCreation':
  command => '/usr/bin/mysqladmin  -u root --password="system" create gitHandson'
}

当我在 Puppet Agent 上 运行 puppet agent --test 时,出现以下错误:

Notice: /Stage[main]/Deployment/Exec[DatabaseCreation]/returns: /usr/bin/mysqladmin: CREATE DATABASE failed; error: 'Can't create database 'gitHandson'; database exists'
Error: /usr/bin/mysqladmin  -u root --password="system" create gitHandson returned 1 instead of one of [0]

与其给出错误,不如忽略错误。为此,我们在 Chef 中有 'ignore_failure',在 Ansible 中有 'ignore_errors'。它的 Puppet 等价物是什么?

简答:没有。 Puppet 资源必须成功,没有办法忽略错误。

最简单(也是最简单)的解决方案是在命令末尾添加一个 && true,这样它就会 return 0 而不会失败。

但是exec的问题在于它不是幂等的。 Puppet 是关于描述状态并确保事情只需要 运行 一次。

因此,对于您的示例,扩展 exec 以添加 unlessonlyif 参数可能会更好,因此如果数据库没有,则命令仅 运行s'已经不存在了。

exec { 'DatabaseCreation':
  command => '/usr/bin/mysqladmin  -u root --password="system" create gitHandson',
  unless  => 'Some command that exits 0 if the gitHandson database exists'
}

更多详情here.

更好,有一个 Puppetlabs MySQL module,它允许您安装 mysql,设置 root 密码并创建 MySQL数据库。

示例:

mysql::db { 'mydb':
  user     => 'myuser',
  password => 'mypass',
  host     => 'localhost',
  grant    => ['SELECT', 'UPDATE'],
}

这是 exec 非幂等资源的经典示例。好消息是:有一些工具可以自动检测脚本中的这些问题。看看这个:https://github.com/citac/citac

Citac 以各种配置、不同的资源执行顺序等系统地执行您的 Puppet 清单。生成的测试报告会告知您有关非幂等资源的问题、与收敛相关的问题等。

该工具使用 Docker 个容器执行,因此您的系统在测试时保持不变。

还有一个评估页面,其中包含许多在 public Puppet 脚本中发现的类似错误:http://citac.github.io/eval

祝 Puppet 测试愉快!