简体   繁体   English

让puppet有条件地执行多个命令

[英]Have puppet conditionally execute multiple commands

I have a working puppet configuration to help installing mysql instances on a machine. 我有一个工作的puppet配置来帮助在机器上安装mysql实例。 My environment is setup such that there are multiple instances running on the same machine (with different configs/ports/etc). 我的环境设置为在同一台机器上运行多个实例(具有不同的配置/端口/等)。

The basic setup I have in a manifest looks like 我在清单中的基本设置看起来像

File{
    owner => $owner,
    group => $group,
    before => Exec["mysql_install_db-${name}"],
}

exec{"mysql_install_db-${name}":
    creates => "/var/lib/mysql/${name}/mysql",
    command => "/usr/local/percona/mysql-${version}/usr/bin/mysql_install_db --user=mysql --datadir=/var/lib/mysql/${name} --basedir=/usr/local/percona/mysql-${version}/usr",
    logoutput => true,
}

This works perfectly fine, however now I'd like to modify this install process to run some subsequent commands to bootstrap the fresh install with some internal stored procedures and do some other 'prep work' we do for a new install. 这非常好用,但是现在我想修改这个安装过程来运行一些后续命令,用一些内部存储过程来引导全新安装,并为新安装做一些其他的“准备工作”。

The commands would be basically like 命令基本上就像

mysql -u user < /path/to/bootstrap1.sql
mysql -u user < /path/to/bootstrap2.sql
mysql -u user < /path/to/bootstrap3.sql

I only want these run once, after the mysql_install_db command but somewhat under the same "creates" guard. 我只希望这些运行一次,在mysql_install_db命令之后,但有点在同一“创建”后卫。

I found some references to just passing an array to the command argument but that reference was in the form of a bug report of it not always working consistently. 我发现了一些只是将数组传递给命令参数的引用,但该引用的形式是错误报告并不总是一致地工作。

What's the preferred method to accomplish something like this, and ensure the commands get executed in a deterministic order and only after mysql_install_db was run? 什么是首选的方法来完成这样的事情,并确保命令以确定的顺序执行,并且只有在运行mysql_install_db之后?

There are several ways to run exec only once, and only after "mysql_install_db-${name}" : 有几种方法只运行一次exec ,并且仅在"mysql_install_db-${name}"

  • Just change the command line to add all the other commands ( cmd1 && cmd2 && cmd3 ... ). 只需更改命令行即可添加所有其他命令( cmd1 && cmd2 && cmd3 ... )。
  • Use the unless or onlyif parameters, so as to check if your stored procedure or whatsoever already exists before running the command. 使用unlessonlyif参数,以便在运行命令之前检查存储过程或是否已存在。 This might be complex, so another method is to have the command create a file ( "command && touch /root/blah-${name}" ) and use the creates parameter. 这可能很复杂,所以另一种方法是让命令创建一个文件( "command && touch /root/blah-${name}" )并使用creates参数。
  • Set refreshonly , and subscribe to the previous exec. 设置refreshonly ,并subscribe以前的exec。

While all these solutions will work, you will not be respecting Puppet's spirit of describing the final state of your system. 虽然所有这些解决方案都有效,但您不会尊重Puppet描述系统最终状态的精神。 For database, user and grant settings you can use the puppetlabs-mysql module, that will let you describe them in a natural way. 对于数据库,用户和授权设置,您可以使用puppetlabs-mysql模块,它可以让您以自然的方式描述它们。 Stored resources are another matter, that might be packaged more logically with the application deployment process (as they must be kept in sync with the application). 存储的资源是另一个问题,可以在应用程序部署过程中更加逻辑地打包(因为它们必须与应用程序保持同步)。 If this is not possible, then you can make your SQL scripts idempotent, using conditionals. 如果这是不可能的,那么您可以使用条件使SQL脚本具有幂等性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM