简体   繁体   English

up-脚本或文件中资源的值

[英]puppet - value of a resource from a script or from file

How to pass the output of the script getAgentList.sh to a variable list_of_agents as an array in Puppet? 如何将脚本getAgentList.sh的输出作为Puppet中的数组getAgentList.sh给变量list_of_agents The script included in the exec can produce the output listed below and can also write to a file. exec包含的脚本可以产生下面列出的输出,也可以写入文件。

exec { 'get agent list':
  path    => '/usr/bin:/bin:/tmp:/usr/sbin',
  command => 'bash /opt/getAgentList.sh'
}

Output of getAgentList.sh : getAgentList.sh输出:

devagent1: devagent1.example.com
devagent2: devagent2.example.com
testagent3: testagent3.example.com

I can pass it to a resource using Hiera. 我可以使用Hiera将其传递给资源。 However, I would like to get these values generated during the runtime and use it in the variable to attach the agents. 但是,我想获取这些在运行时生成的值,并在变量中使用它来附加代理。

$list_of_agents.each |String $index, String $value| {
  agent { "${index} Agent":
    home  => "/opt/agent",
    trans => "http://${value}:80",
  }
}

You would use an external fact for this: https://docs.puppet.com/facter/3.9/custom_facts.html#external-facts 您将为此使用外部事实: https : //docs.puppet.com/facter/3.9/custom_facts.html#external-facts

Unfortunately, the output from your executable external fact is limited to simple data types like String . 不幸的是,可执行外部事实的输出仅限于简单数据类型,如String You are attempting to construct a Hash , which you cannot do because the stdout of the script must be in the format: 您正在尝试构造一个Hash ,但是您无法执行该操作,因为脚本的stdout必须采用以下格式:

key1=value1
key2=value2
key3=value3

If you wanted a hash, then you would have to use a file for the external fact. 如果要散列,则必须将文件用于外部事实。 A yaml example would be: 一个yaml示例将是:

list_of_agents:
  devagent1: devagent1.example.com
  devagent2: devagent2.example.com
  testagent3: testagent3.example.com

json: json:

{
  "list_of_agents": {
    "devagent1": "devagent1.example.com"
    "devagent2": "devagent2.example.com"
    "testagent3": "testagent3.example.com"
  }
}

These files, and your executable script, would be placed in the facts.d directory of your module. 这些文件和可执行脚本将放置在模块的facts.d目录中。

A side note is your: 旁注是您的:

$list_of_agents.each |String $index, String $value|

and beginning of the question implies you are expecting an Array. 问题的开始意味着您期望使用数组。 You can manipulate your data struct to be an Array if you want and the code would still work fine, but it currently is a Hash as you have formatted it. 如果需要,您可以将数据结构操作为数组,并且代码仍然可以正常工作,但是当前它是已格式化的哈希。

Another side note is that this really a job for a node classifier or CMDB: https://docs.puppet.com/puppet/5.3/nodes_external.html . 另一个注意事项是,这对于节点分类器或CMDB确实是一项工作: https : //docs.puppet.com/puppet/5.3/nodes_external.html These would dynamically and easily store the data you are looking to use in this question. 这些将动态,轻松地存储您要在此问题中使用的数据。

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

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