summaryrefslogtreecommitdiff
path: root/distro/common/html/knife_exec.html
blob: c2fb2c7f470c5decf4f469ce6528e84290bee620 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>knife exec &mdash; chef-client Man Pages</title>
    
    <link rel="stylesheet" href="_static/guide.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>


  </head>
  <body>
<div style="background-color: #212c35; text-align: left; padding: 0px 0px 0px 0px">
<a href="http://docs.opscode.com/"><img src="_static/chef_html_logo.png" border="0" alt="Chef"/></a>
</div>


  

    <div class="document">
      <div class="documentwrapper">

          <div class="body">
            
  <div class="section" id="knife-exec">
<h1>knife exec<a class="headerlink" href="#knife-exec" title="Permalink to this headline">¶</a></h1>
<p>The <strong>knife exec</strong> subcommand uses the Knife configuration file to execute Ruby scripts in the context of a fully configured chef-client. This subcommand is most often used to run scripts that will only access Chef server one time (or otherwise very infrequently). Use this subcommand any time that an operation does not warrant full usage of the Knife subcommand library.</p>
<div class="section" id="authenticated-api-requests">
<h2>Authenticated API Requests<a class="headerlink" href="#authenticated-api-requests" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="docutils literal"><span class="pre">knife</span> <span class="pre">exec</span></tt> subcommand can be used to make authenticated API requests to the Chef server using the following methods:</p>
<table border="1" class="docutils">
<colgroup>
<col width="13%" />
<col width="88%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Method</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">api.delete</span></tt></td>
<td>Use to delete an object from the Chef server.</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">api.get</span></tt></td>
<td>Use to get the details of an object on the Chef server.</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">api.post</span></tt></td>
<td>Use to add an object to the Chef server.</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">api.put</span></tt></td>
<td>Use to update an object on the Chef server.</td>
</tr>
</tbody>
</table>
<p>These methods are used with the <tt class="docutils literal"><span class="pre">-E</span></tt> option, which executes that string locally on the workstation using chef-shell. These methods have the following syntax:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>knife <span class="nb">exec</span> -E <span class="s1">&#39;api.method(/endpoint)&#39;</span>
</pre></div>
</div>
<p>where:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">api.method</span></tt> is the corresponding authentication method &#8212; <tt class="docutils literal"><span class="pre">api.delete</span></tt>, <tt class="docutils literal"><span class="pre">api.get</span></tt>, <tt class="docutils literal"><span class="pre">api.post</span></tt>, or <tt class="docutils literal"><span class="pre">api.put</span></tt></li>
<li><tt class="docutils literal"><span class="pre">/endpoint</span></tt> is an endpoint in the Chef Server API</li>
</ul>
<p>For example, to get the data for a node named &#8220;Example_Node&#8221;:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>knife <span class="nb">exec</span> -E <span class="s1">&#39;puts api.get(&quot;/nodes/Example_Node&quot;)&#39;</span>
</pre></div>
</div>
<p>and to ensure that the output is visible in the console, add the <tt class="docutils literal"><span class="pre">puts</span></tt> in front of the API authorization request:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>knife <span class="nb">exec</span> -E <span class="s1">&#39;puts api.get(&quot;/nodes/Example_Node&quot;)&#39;</span>
</pre></div>
</div>
<p>where <tt class="docutils literal"><span class="pre">puts</span></tt> is the shorter version of the <tt class="docutils literal"><span class="pre">$stdout.puts</span></tt> predefined variable in Ruby.</p>
<p>The following example shows how to add a client named &#8220;IBM305RAMAC&#8221; and the <tt class="docutils literal"><span class="pre">/clients</span></tt> endpoint, and then return the private key for that user in the console:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ client_desc</span> <span class="o">=</span> <span class="o">{</span>
    <span class="s2">&quot;name&quot;</span>  <span class="o">=</span>&gt; <span class="s2">&quot;IBM305RAMAC&quot;</span>,
    <span class="s2">&quot;admin&quot;</span> <span class="o">=</span>&gt; <span class="nb">false</span>
  <span class="o">}</span>

  <span class="nv">new_client</span> <span class="o">=</span> api.post<span class="o">(</span><span class="s2">&quot;/clients&quot;</span>, client_desc<span class="o">)</span>
  puts new_client<span class="o">[</span><span class="s2">&quot;private_key&quot;</span><span class="o">]</span>
</pre></div>
</div>
</div>
<div class="section" id="ruby-scripts">
<h2>Ruby Scripts<a class="headerlink" href="#ruby-scripts" title="Permalink to this headline">¶</a></h2>
<p>For Ruby scripts that will be run using the <tt class="docutils literal"><span class="pre">exec</span></tt> subcommand, note the following:</p>
<blockquote>
<div><ul class="simple">
<li>The Ruby script must be located on the system from which Knife is run (and not be located on any of the systems that Knife will be managing).</li>
<li>Shell commands will be run from a management workstation. For example, something like <tt class="docutils literal"><span class="pre">%x[ls</span> <span class="pre">-lash</span> <span class="pre">/opt/only-on-a-node]</span></tt> would give you the directory listing for the &#8220;opt/only-on-a-node&#8221; directory or a &#8220;No such file or directory&#8221; error if the file does not already exist locally.</li>
<li>When the chef-shell DSL is available, the chef-client DSL will not be (unless the management workstation is also a chef-client). Without the chef-client DSL, a bash block cannot be used to run bash commands.</li>
</ul>
</div></blockquote>
</div>
<div class="section" id="syntax">
<h2>Syntax<a class="headerlink" href="#syntax" title="Permalink to this headline">¶</a></h2>
<p>This subcommand has the following syntax:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>knife <span class="nb">exec </span>SCRIPT <span class="o">(</span>options<span class="o">)</span>
</pre></div>
</div>
</div>
<div class="section" id="options">
<h2>Options<a class="headerlink" href="#options" title="Permalink to this headline">¶</a></h2>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Review the list of <a class="reference internal" href="knife_common_options.html"><em>common options</em></a> available to this (and all) Knife subcommands and plugins.</p>
</div>
<p>This subcommand has the following options:</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">-E</span> <span class="pre">CODE</span></tt>, <tt class="docutils literal"><span class="pre">--exec</span> <span class="pre">CODE</span></tt></dt>
<dd>A string of code that will be executed.</dd>
<dt><tt class="docutils literal"><span class="pre">-p</span> <span class="pre">PATH:PATH</span></tt>, <tt class="docutils literal"><span class="pre">--script-path</span> <span class="pre">PATH:PATH</span></tt></dt>
<dd>A colon-separated path at which Ruby scripts are located. Use to override the default location for scripts. When this option is not specified, Knife will look for scripts located in <tt class="docutils literal"><span class="pre">chef-repo/.chef/scripts</span></tt> directory.</dd>
</dl>
</div>
<div class="section" id="examples">
<h2>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h2>
<p>The following examples show how to use this Knife subcommand:</p>
<p><strong>Run Ruby scripts</strong></p>
<p>There are three ways to use <strong>knife exec</strong> to run Ruby script files. For example:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>knife <span class="nb">exec</span> /path/to/script_file
</pre></div>
</div>
<p>Or:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>knife <span class="nb">exec</span> -E <span class="s1">&#39;RUBY CODE&#39;</span>
</pre></div>
</div>
<p>Or:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>knife <span class="nb">exec</span>
RUBY CODE
^D
</pre></div>
</div>
<p><strong>Chef Knife status</strong></p>
<p>To check the status of Knife using a Ruby script named &#8220;status.rb&#8221; (which looks like):</p>
<div class="highlight-ruby"><div class="highlight"><pre><span class="nb">printf</span> <span class="s2">&quot;%-5s %-12s %-8s %s</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">,</span> <span class="s2">&quot;Check In&quot;</span><span class="p">,</span> <span class="s2">&quot;Name&quot;</span><span class="p">,</span> <span class="s2">&quot;Ruby&quot;</span><span class="p">,</span> <span class="s2">&quot;Recipes&quot;</span>
<span class="n">nodes</span><span class="o">.</span><span class="n">all</span> <span class="k">do</span> <span class="o">|</span><span class="n">n</span><span class="o">|</span>
   <span class="n">checkin</span> <span class="o">=</span> <span class="no">Time</span><span class="o">.</span><span class="n">at</span><span class="p">(</span><span class="n">n</span><span class="o">[</span><span class="s1">&#39;ohai_time&#39;</span><span class="o">]</span><span class="p">)</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%F %R&quot;</span><span class="p">)</span>
   <span class="n">rubyver</span> <span class="o">=</span> <span class="n">n</span><span class="o">[</span><span class="s1">&#39;languages&#39;</span><span class="o">][</span><span class="s1">&#39;ruby&#39;</span><span class="o">][</span><span class="s1">&#39;version&#39;</span><span class="o">]</span>
   <span class="n">recipes</span> <span class="o">=</span> <span class="n">n</span><span class="o">.</span><span class="n">run_list</span><span class="o">.</span><span class="n">expand</span><span class="p">(</span><span class="n">_default</span><span class="p">)</span><span class="o">.</span><span class="n">recipes</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s2">&quot;, &quot;</span><span class="p">)</span>
   <span class="nb">printf</span> <span class="s2">&quot;%-20s %-12s %-8s %s</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">,</span> <span class="n">checkin</span><span class="p">,</span> <span class="n">n</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">rubyver</span><span class="p">,</span> <span class="n">recipes</span>
<span class="k">end</span>
</pre></div>
</div>
<p>and is located in a directory named &#8220;scripts&#8221;, enter:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>knife <span class="nb">exec </span>scripts/status.rb
</pre></div>
</div>
<p><strong>List available free memory</strong></p>
<p>To show the available free memory for all nodes, enter:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>knife <span class="nb">exec</span> -E <span class="s1">&#39;nodes.all {|n| puts &quot;#{n.name} has #{n.memory.total} free memory&quot;}&#39;</span>
</pre></div>
</div>
<p><strong>List available search indexes</strong></p>
<p>To list all of the available search indexes, enter:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>knife <span class="nb">exec</span> -E <span class="s1">&#39;puts api.get(&quot;search&quot;).keys&#39;</span>
</pre></div>
</div>
<p><strong>Query for multiple attributes</strong></p>
<p>To query a node for multiple attributes using a Ruby script named <tt class="docutils literal"><span class="pre">search_attributes.rb</span></tt> (which looks like):</p>
<div class="highlight-ruby"><div class="highlight"><pre><span class="sx">% cat </span><span class="n">scripts</span><span class="o">/</span><span class="n">search_attributes</span><span class="o">.</span><span class="n">rb</span>
<span class="n">query</span> <span class="o">=</span> <span class="no">ARGV</span><span class="o">[</span><span class="mi">2</span><span class="o">]</span>
<span class="n">attributes</span> <span class="o">=</span> <span class="no">ARGV</span><span class="o">[</span><span class="mi">3</span><span class="o">].</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;,&quot;</span><span class="p">)</span>
<span class="nb">puts</span> <span class="s2">&quot;Your query: </span><span class="si">#{</span><span class="n">query</span><span class="si">}</span><span class="s2">&quot;</span>
<span class="nb">puts</span> <span class="s2">&quot;Your attributes: </span><span class="si">#{</span><span class="n">attributes</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s2">&quot; &quot;</span><span class="p">)</span><span class="si">}</span><span class="s2">&quot;</span>
<span class="n">results</span> <span class="o">=</span> <span class="p">{}</span>
<span class="n">search</span><span class="p">(</span><span class="ss">:node</span><span class="p">,</span> <span class="n">query</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">n</span><span class="o">|</span>
   <span class="n">results</span><span class="o">[</span><span class="n">n</span><span class="o">.</span><span class="n">name</span><span class="o">]</span> <span class="o">=</span> <span class="p">{}</span>
   <span class="n">attributes</span><span class="o">.</span><span class="n">each</span> <span class="p">{</span><span class="o">|</span><span class="n">a</span><span class="o">|</span> <span class="n">results</span><span class="o">[</span><span class="n">n</span><span class="o">.</span><span class="n">name</span><span class="o">][</span><span class="n">a</span><span class="o">]</span> <span class="o">=</span> <span class="n">n</span><span class="o">[</span><span class="n">a</span><span class="o">]</span><span class="p">}</span>
<span class="k">end</span>

<span class="nb">puts</span> <span class="n">results</span>
<span class="nb">exit</span> <span class="mi">0</span>
</pre></div>
</div>
<p>enter:</p>
<div class="highlight-bash"><div class="highlight"><pre>% knife <span class="nb">exec </span>scripts/search_attributes.rb <span class="s2">&quot;hostname:test_system&quot;</span> ipaddress,fqdn
</pre></div>
</div>
<p>to return something like:</p>
<div class="highlight-bash"><div class="highlight"><pre>Your query: hostname:test_system
Your attributes: ipaddress fqdn
<span class="o">{</span><span class="s2">&quot;test_system.example.com&quot;</span><span class="o">=</span>&gt;<span class="o">{</span><span class="s2">&quot;ipaddress&quot;</span><span class="o">=</span>&gt;<span class="s2">&quot;10.1.1.200&quot;</span>, <span class="s2">&quot;fqdn&quot;</span><span class="o">=</span>&gt;<span class="s2">&quot;test_system.example.com&quot;</span><span class="o">}}</span>
</pre></div>
</div>
</div>
</div>


          </div>

      </div>

  
      <div class="clearer"></div>
    </div>




  </body>
</html>