| Teaching some of the people on my team how to use Bolt and several of them have been confused by the way that ResultSet is displayed when printed out with notice() from a plan. By default the ResultSet object's to_s() function returns a JSON representation that looks like:
[{"node":"winrm://hostname.domain.tld","status":"success","result":{"os":{"name":"windows","release":{"full":"2012 R2","major":"2012 R2"},"family":"windows"}}}]
|
Their initial reaction is to try and read parts of this data by accessing it like an array + dictionary that is shown:
plan encore_st2::get_facts ( |
TargetSpec $nodes |
) { |
$res = run_task('facts', $nodes) |
notice($res) |
$first = $res[0] |
$result = $first['result'] |
$os = $result['os'] |
notice($os) |
}
|
This returns an error because there is no [] function on a ResultSet:
{ |
"kind": "bolt/pal-error", |
"msg": "Evaluation Error: Operator '[]' is not applicable to an Object. (file: /home/user/bolt/modules/encore_st2/plans/get_facts.pp, line: 6, column: 12)", |
"details": { |
} |
} |
|
I then showed them the `first` function which leads to the following code:
plan encore_st2::get_facts ( |
TargetSpec $nodes |
) { |
$res = run_task('facts', $nodes) |
notice($res) |
$first = $res.first |
$result = $first['result'] |
$os = $result['os'] |
notice($os) |
}
|
This then returns the following error:
{ |
"kind": "bolt/pal-error", |
"msg": "Evaluation Error: Operator '[]' is not applicable to an Undef Value. (file: /home/nuserbolt/modules/encore_st2/plans/get_facts.pp, line: 8, column: 9)", |
"details": { |
} |
} |
|
Finally we modify the lines: $result = $first['result'] $os = $result['os'] And it becomes working code:
plan encore_st2::get_facts ( |
TargetSpec $nodes |
) { |
$res = run_task('facts', $nodes) |
notice($res) |
$first = $res.first |
$os = $first['os'] |
notice($os) |
}
|
I think the root of the problem is that the JSON representation is NOT consistent with the way you access the data programatically within a plan. I'm sure there are lots of possible solutions, but i think that no matter what direction is taken, the printable output should be consistent with the way that the user needs to access the data. I'm sure the same goes for other defined types like Target. |