如果满足条件,我如何 运行 执行傀儡?

How do I run a puppet exec if a condition met?

仅提供一些详细信息 - 在 AWS 上构建,使用 Puppet 处理 DSC,并引导 Puppet 进行配置,并在新配置的节点上安装 Puppet。

我使用 Puppet 已经有一段时间了,我发现自己想要编写一个仅在创建 vm 时执行的模块。

我的特定用例是我想通过 Puppet 自动将防病毒软件(特别是 Trend Micro Deep Security)安装到新配置的节点上。

运行 的脚本只需要下载、运行 和一些 TMDS 特定命令来激活自身等。

如果我使用 Puppet,它会在每次 运行(下载、尝试安装、尝试激活)时执行此操作,这绝对不是我想要的。

但是,我不认为 Puppet 'knows' 关于 Trend Micro,或者如何获得它,或者 URL 等。所以我不能使用类似的东西:

  service { "trend micro":
    ensure => running,
    ensure => present,
  }

做一些研究并查看 blog posts,我知道我的代码结构应该类似于(我知道这是不正确的):

exec {'function_name':
  # the script that downloads/installs/activates etc.
  command => '/path/to/script.sh', 
  onlyif  => systemctl service_trendmicro, 
  # which system should return 0 or 1 for pass/fail - I only want it to exec on 0 ofc.
}

因此我的问题是:如何将它们组合在一起?

看看这个模块:https://github.com/echocat/puppet-redis

它从源代码安装Redis。因此它会下载、安装、配置和启动服务。本质上,它的功能等同于您正在寻找的功能。

您可以使用 Puppet 来 运行 脚本,正如您正在做的那样。但是,如果您在没有 Puppet 的情况下 运行 使用脚本,您最终会遇到同样的问题:很难使它们具有幂等性,它们维护起来很烦人,而且它们不能移植到其他平台。

该公司似乎提供了 Chef cookbooks and Ansible Playbooks 来安装代理。这些应该让您大致了解如何使用 Puppet。

通过查看 Ansible 剧本,很容易将其转换为等效的 Puppet 代码:

exec {'download Trend RPM':
  command => '/bin/wget https://app.deepsecurity.trendmicro.com:443/software/agent/RedHat_EL7/x86_64/ O /tmp/agent.rpm --quiet',
  creates => '/tmp/agent.rpm',
}
->
package {'ds_agent':
  ensure   => 'installed',
  provider => 'rpm',
  source => '/tmp/agent.rpm',
}
~>
service {'ds_agent':
  ensure => running,
  enable => true,
}

我刚刚快速检查了 CentOS 7 虚拟机,它似乎对我有用:

Notice: Compiled catalog for centos7.vm in environment production in 1.06 seconds
Notice: /Stage[main]/Main/Exec[download Trend RPM]/returns: executed successfully
Notice: /Stage[main]/Main/Package[ds_agent]/ensure: created
Notice: /Stage[main]/Main/Service[ds_agent]/enable: enable changed 'false' to 'true'
Notice: /Stage[main]/Main/Service[ds_agent]: Triggered 'refresh' from 1 events
Notice: Applied catalog in 8.45 seconds

我建议查看现有的 Ansible 剧本并了解如何完成其​​余设置步骤:https://github.com/deep-security/ansible/blob/master/playbook/