knife 节点列表 VS Knife exec -E 'nodes.all.each {|n|放置“#{n.name}”}'

问题描述 投票:0回答:2

我试图理解为什么

的输出
knife node list

knife exec -E 'nodes.all.each {|n| puts "#{n.name}"}'

不要给我相同的节点名称。我希望上面的命令显示相同的名称,但目前存在差异,似乎第二个命令中没有拾取任何新节点(

knife exec..

知道什么可能导致此问题以及如何调试它吗?

提前致谢

chef-infra devops knife infrastructure
2个回答
0
投票

查看

knife node list
的代码,如果您的knife.rb文件中列出了环境,则该命令似乎不会输出所有节点。看起来运行
knife node exec
返回原始节点对象。

请参阅此处的代码

knife node list
https://github.com/chef/chef/blob/f53ad6511b7fcc40576f05688dc17b11ad58e089/knife/lib/chef/knife/node_list.rb#L37-L40

请参阅此处的

nodes
对象代码:https://github.com/chef/chef/blob/f53ad6511b7fcc40576f05688dc17b11ad58e089/lib/chef/shell/ext.rb#L461-L463


0
投票

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Chef Commands Comparison</title>
</head>
<body>

    <h2>knife node list vs. [knife][1] exec</h2>

    <p>The <code>knife node list</code> and <code>knife exec -E 'nodes.all.each {|n| puts "#{n.name}"}'</code> commands in Chef serve similar purposes but have some differences in how they operate.</p>

    <h3><code>knife node list</code></h3>

    <ul>
        <li><strong>Usage:</strong> This command is used to list all the nodes that are registered with the Chef Server.</li>
        <li><strong>Example:</strong></li>
        <pre><code>knife node list</code></pre>
        <li><strong>Output:</strong> It will print a simple list of node names.</li>
    </ul>

    <h3><code>knife exec -E 'nodes.all.each {|n| puts "#{n.name}"}'</code></h3>

    <ul>
        <li><strong>Usage:</strong> This command uses the knife exec plugin to execute arbitrary Ruby code on the local workstation. In this case, it retrieves and prints the names of all nodes using the nodes.all.each Ruby code.</li>
        <li><strong>Example:</strong></li>
        <pre><code>knife exec -E 'nodes.all.each {|n| puts "#{n.name}"}'</code></pre>
        <li><strong>Output:</strong> Similar to knife node list, it will print a list of node names.</li>
    </ul>

    <h3>Differences:</h3>

    <ul>
        <li><strong>Source of Information:</strong></li>
        <ul>
            <li><code>knife node list</code> directly queries the Chef Server for a list of registered nodes.</li>
            <li><code>knife exec</code> with <code>nodes.all.each</code> executes Ruby code locally and may not interact directly with the Chef Server, depending on the context.</li>
        </ul>

        <li><strong>Flexibility:</strong></li>
        <ul>
            <li><code>knife node list</code> is a built-in command specifically designed for listing nodes.</li>
            <li><code>knife exec</code> with custom Ruby code allows more flexibility as you can execute any Ruby code, making it more powerful but potentially more complex.</li>
        </ul>
    </ul>

    <p>In most cases, if you simply want to list the nodes registered with the Chef Server, <code>knife node list</code> is more straightforward. The <code>knife exec</code> approach is useful when you need to perform more complex or customized operations on the node data using Ruby code.</p>

</body>
</html>

© www.soinside.com 2019 - 2024. All rights reserved.