简体   繁体   中英

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? The script included in the exec can produce the output listed below and can also write to a file.

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

Output of getAgentList.sh :

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

I can pass it to a resource using 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

Unfortunately, the output from your executable external fact is limited to simple data types like String . You are attempting to construct a Hash , which you cannot do because the stdout of the script must be in the format:

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:

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

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.

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 . These would dynamically and easily store the data you are looking to use in this question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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