examples.html 17.3 KB
Newer Older
Tim Bielawa committed
1 2 3 4 5 6 7 8

<!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" />
    
9
    <title>Command Line Examples &mdash; Ansible - SSH-Based Configuration Management &amp; Deployment</title>
Tim Bielawa committed
10 11
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
12 13
    <link rel="stylesheet" href="_static/bootstrap.css" type="text/css" />
    <link rel="stylesheet" href="_static/bootstrap-sphinx.css" type="text/css" />
Tim Bielawa committed
14 15 16
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
17
        VERSION:     '0.01',
Tim Bielawa committed
18 19
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
Tim Bielawa committed
20
        HAS_SOURCE:  false
Tim Bielawa committed
21 22 23 24 25
      };
    </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>
26 27
    <script type="text/javascript" src="_static/bootstrap-dropdown.js"></script>
    <script type="text/javascript" src="_static/bootstrap-scrollspy.js"></script>
28
    <link rel="top" title="Ansible - SSH-Based Configuration Management &amp; Deployment" href="index.html" />
Michael DeHaan committed
29
    <link rel="next" title="Ansible Modules" href="modules.html" />
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
    <link rel="prev" title="The Inventory File, Patterns, and Groups" href="patterns.html" />
<script type="text/javascript">
(function () {
  /**
   * Patch TOC list.
   *
   * Will mutate the underlying span to have a correct ul for nav.
   *
   * @param $span: Span containing nested UL's to mutate.
   * @param minLevel: Starting level for nested lists. (1: global, 2: local).
   */
  var patchToc = function ($span, minLevel) {
    var $tocList = $("<ul/>").attr('class', "dropdown-menu"),
      findA;

    // Find all a "internal" tags, traversing recursively.
    findA = function ($elem, level) {
      var level = level || 0,
        $items = $elem.find("> li > a.internal, > ul, > li > ul");

      // Iterate everything in order.
      $items.each(function (index, item) {
        var $item = $(item),
          tag = item.tagName.toLowerCase(),
          pad = 10 + ((level - minLevel) * 10);

        if (tag === 'a' && level >= minLevel) {
          // Add to existing padding.
          $item.css('padding-left', pad + "px");
          // Add list element.
          $tocList.append($("<li/>").append($item));
        } else if (tag === 'ul') {
          // Recurse.
          findA($item, level + 1);
        }
      });
    };

    // Start construction and return.
    findA($span);

    // Wipe out old list and patch in new one.
    return $span.empty("ul").append($tocList);
  };

  $(document).ready(function () {
    // Patch the global and local TOC's to be bootstrap-compliant.
    patchToc($("span.globaltoc"), 1);
    patchToc($("span.localtoc"), 2);

    // Activate.
    $('#topbar').dropdown();
  });
}());
</script>
Michael DeHaan committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
<script type="text/javascript">

 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'UA-29861888-1']);
 _gaq.push(['_trackPageview']);

 (function() {
   var ga = document.createElement('script'); ga.type =
'text/javascript'; ga.async = true;
   ga.src = ('https:' == document.location.protocol ? 'https://ssl' :
'http://www') + '.google-analytics.com/ga.js';
   var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
 })();

</script>
101

Tim Bielawa committed
102 103
  </head>
  <body>
104 105 106 107 108 109 110 111
  <div class="topbar" data-scrollspy="scrollspy" >
    <div class="topbar-inner">
      <div class="container">
        <a class="brand" href="index.html">Ansible</a>
        <ul class="nav">
          
            <li class="dropdown" data-dropdown="dropdown">
  <a href="index.html"
112
     class="dropdown-toggle">Chapter</a>
113
  <span class="globaltoc"><ul class="current">
114
<li class="toctree-l1"><a class="reference internal" href="gettingstarted.html">Downloads &amp; Getting Started</a></li>
115 116 117
<li class="toctree-l1"><a class="reference internal" href="patterns.html">The Inventory File, Patterns, and Groups</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="">Command Line Examples</a></li>
<li class="toctree-l1"><a class="reference internal" href="modules.html">Ansible Modules</a></li>
118
<li class="toctree-l1"><a class="reference internal" href="YAMLSyntax.html">YAML Syntax</a></li>
119
<li class="toctree-l1"><a class="reference internal" href="playbooks.html">Playbooks</a></li>
120
<li class="toctree-l1"><a class="reference internal" href="api.html">API &amp; Integrations</a></li>
121
<li class="toctree-l1"><a class="reference internal" href="moduledev.html">Module Development Guide</a></li>
122
<li class="toctree-l1"><a class="reference internal" href="faq.html">Frequently Asked Questions</a></li>
123 124 125 126
<li class="toctree-l1"><a class="reference internal" href="man.html">Man Pages</a></li>
</ul>
</span>
</li>
127 128 129 130 131 132 133 134
            <li class="dropdown" data-dropdown="dropdown">
  <a href="#"
     class="dropdown-toggle">Page</a>
  <span class="localtoc"><ul>
<li><a class="reference internal" href="#">Command Line Examples</a><ul>
<li><a class="reference internal" href="#parallelism-and-shell-commands">Parallelism and Shell Commands</a></li>
<li><a class="reference internal" href="#file-transfer-templating">File Transfer &amp; Templating</a></li>
<li><a class="reference internal" href="#managing-packages">Managing Packages</a></li>
135
<li><a class="reference internal" href="#users-and-groups">Users and Groups</a></li>
136 137 138 139 140 141 142 143
<li><a class="reference internal" href="#deploying-from-source-control">Deploying From Source Control</a></li>
<li><a class="reference internal" href="#managing-services">Managing Services</a></li>
<li><a class="reference internal" href="#time-limited-background-operations">Time Limited Background Operations</a></li>
</ul>
</li>
</ul>
</span>
</li>
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
          
        </ul>
        <ul class="nav secondary-nav">
          
            
<form class="pull-left" action="search.html" method="get">
  <input type="text" name="q" placeholder="Search" />
  <input type="hidden" name="check_keywords" value="yes" />
  <input type="hidden" name="area" value="default" />
</form>
          
        </ul>
      </div>
    </div>
  </div>

160

161 162
<div class="container">
   
Michael DeHaan committed
163 164 165
  <div class="section" id="command-line-examples">
<h1>Command Line Examples<a class="headerlink" href="#command-line-examples" title="Permalink to this headline"></a></h1>
<p>The following examples show how to use <cite>/usr/bin/ansible</cite> for running ad-hoc tasks.
166 167 168 169
Start here.</p>
<p>For configuration management and deployments, you&#8217;ll want to pick up on
using <cite>/usr/bin/ansible-playbook</cite> &#8211; the concepts port over directly.
(See <a class="reference internal" href="playbooks.html"><em>Playbooks</em></a> for more information about those)</p>
Michael DeHaan committed
170 171
<div class="section" id="parallelism-and-shell-commands">
<h2>Parallelism and Shell Commands<a class="headerlink" href="#parallelism-and-shell-commands" title="Permalink to this headline"></a></h2>
172 173
<p>Let&#8217;s use ansible&#8217;s command line tool to reboot all web servers in Atlanta, 10 at a time.  First, let&#8217;s
set up SSH-agent so it can remember our credentials:</p>
Michael DeHaan committed
174
<div class="highlight-python"><pre>ssh-agent bash
175
ssh-add ~/.ssh/id_rsa.pub</pre>
Michael DeHaan committed
176
</div>
177 178
<p>Now to run the command on all servers in a group, in this case, &#8216;atlanta&#8217;:</p>
<div class="highlight-python"><pre>ansible atlanta -a "/sbin/reboot" -f 10</pre>
179
</div>
180 181 182 183 184 185 186
<p>If you didn&#8217;t read about patterns and groups yet, go back and read <a class="reference internal" href="patterns.html"><em>The Inventory File, Patterns, and Groups</em></a>.</p>
<p>The -f 10 in the above specifies the usage of 10 simultaneous processes.  Normally commands also take
a <cite>-m</cite> for module name, but the default module name is &#8216;command&#8217;, so we didn&#8217;t need to specify that
here.  We&#8217;ll use <cite>-m</cite> later to run some other <a class="reference internal" href="modules.html"><em>Ansible Modules</em></a>.</p>
<p>The command module requires absolute paths and does not support shell variables.  If we want to
execute a module using the shell, we can do those things, and also use pipe and redirection operators.
Read more about the differences on the <a class="reference internal" href="modules.html"><em>Ansible Modules</em></a> page.  The shell
187
module looks like this:</p>
188
<div class="highlight-python"><pre>ansible raleigh -m shell -a 'echo $TERM'</pre>
189
</div>
190
<p>When running any command with the ansible &#8220;ad hoc&#8221; CLI (as opposed to playbooks), pay particular attention
191 192
to shell quoting rules, so the shell doesn&#8217;t eat a variable before it gets passed to Ansible.  For example, u
using double vs single quotes would evaluate the variable on the box you were on.</p>
193 194 195 196
<p>So far we&#8217;ve been demoing simple command execution, but most ansible modules usually do not work like
simple scripts. They make the remote system look like you state, and run the commands necessary to
get it there.  This is commonly referred to as &#8216;idempotence&#8217;, and is a core design goal of ansible.
However, we also recognize that running ad-hoc commands is equally imporant, so Ansible easily supports both.</p>
Michael DeHaan committed
197
</div>
Michael DeHaan committed
198 199
<div class="section" id="file-transfer-templating">
<h2>File Transfer &amp; Templating<a class="headerlink" href="#file-transfer-templating" title="Permalink to this headline"></a></h2>
200
<p>Here&#8217;s another use case for the <cite>/usr/bin/ansible</cite> command line.</p>
201 202
<p>Ansible can SCP lots of files to multiple machines in parallel, and
optionally use them as template sources.</p>
Michael DeHaan committed
203
<p>To just transfer a file directly to many different servers:</p>
204
<div class="highlight-python"><pre>ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"</pre>
Michael DeHaan committed
205
</div>
206 207
<p>To use templating, first run the setup module to put the template
variables you would like to use on the remote host. Then use the
208 209
template module to write the files using those templates.</p>
<p>Templates are written in Jinja2 format. Playbooks (covered elsewhere in the
210
documentation) will run the setup module for you, making this even
211
simpler:</p>
Michael DeHaan committed
212 213 214
<div class="highlight-python"><pre>ansible webservers -m setup    -a "favcolor=red ntp_server=192.168.1.1"
ansible webservers -m template -a "src=/srv/motd.j2 dest=/etc/motd"
ansible webservers -m template -a "src=/srv/ntp.j2 dest=/etc/ntp.conf"</pre>
Tim Bielawa committed
215
</div>
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
<p>Ansible variables are used in templates by using the name surrounded by double
curly-braces.  If facter or ohai were installed on the remote machine, variables
from those programs can be accessed too, which the appropriate prefix:</p>
<div class="highlight-python"><pre>This is an Ansible variable: {{ favcolor }}
This is a facter variable: {{ facter_hostname }}
This is an ohai variable: {{ ohai_foo }}</pre>
</div>
<p>The <cite>file</cite> module allows changing ownership and permissions on files.  These
same options can be passed directly to the <cite>copy</cite> or <cite>template</cite> modules as well:</p>
<div class="highlight-python"><pre>ansible webservers -m file -a "dest=/srv/foo/a.txt mode=600"
ansible webservers -m file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"</pre>
</div>
<p>The <cite>file</cite> module can also create directories, similar to <cite>mkdir -p</cite>:</p>
<div class="highlight-python"><pre>ansible webservers -m file -a "dest=/path/to/c mode=644 owner=mdehaan group=mdehaan state=directory"</pre>
</div>
<p>As well as delete directories (recursively) and delete files:</p>
<div class="highlight-python"><pre>ansible webservers -m file -a "dest=/path/to/c state=absent"</pre>
</div>
234
<p>The mode, owner, and group arguments can also be used on the copy or template lines.</p>
Tim Bielawa committed
235
</div>
236 237 238 239 240 241 242 243 244 245 246 247
<div class="section" id="managing-packages">
<h2>Managing Packages<a class="headerlink" href="#managing-packages" title="Permalink to this headline"></a></h2>
<p>Ensure a package is installed, but don&#8217;t update it:</p>
<div class="highlight-python"><pre>ansible webservers -m yum -a "pkg=acme state=installed"</pre>
</div>
<p>Ensure a package is installed to a specific version:</p>
<div class="highlight-python"><pre>ansible-webservers -m yum -a "pkg=acme-1.5 state=installed"</pre>
</div>
<p>Ensure a package is at the latest version:</p>
<div class="highlight-python"><pre>ansible webservers -m yum -a "pkg=acme state=latest"</pre>
</div>
<p>Ensure a package is not installed:</p>
Michael DeHaan committed
248 249
<div class="highlight-python"><pre>ansible-webservers -m yum -a "pkg=acme state=removed"</pre>
</div>
250
<p>Currently Ansible only has a module for managing packages with yum.  You can install
251
for other packages for now using the command module or (better!) contribute a module
252 253
for other package managers.  Stop by the mailing list for info/details.</p>
</div>
254 255 256 257 258 259 260 261 262 263
<div class="section" id="users-and-groups">
<h2>Users and Groups<a class="headerlink" href="#users-and-groups" title="Permalink to this headline"></a></h2>
<p>The user module allows easy creation and manipulation of existing user accounts, as well
as removal of user accounts that may exist:</p>
<div class="highlight-python"><pre>ansible all -m user -a "name=foo password=&lt;crypted password here&gt;"

ansible all -m user -a "name=foo state=absent"</pre>
</div>
<p>See the <a class="reference internal" href="modules.html"><em>Ansible Modules</em></a> section for details on all of the available options.</p>
</div>
Michael DeHaan committed
264 265
<div class="section" id="deploying-from-source-control">
<h2>Deploying From Source Control<a class="headerlink" href="#deploying-from-source-control" title="Permalink to this headline"></a></h2>
Michael DeHaan committed
266
<p>Deploy your webapp straight from git:</p>
267
<div class="highlight-python"><pre>ansible webservers -m git -a "repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD"</pre>
Tim Bielawa committed
268
</div>
269 270 271 272
<p>Since ansible modules can notify change handlers (see
<a class="reference internal" href="playbooks.html"><em>Playbooks</em></a>) it is possible to tell ansible to run specific tasks
when the code is updated, such as deploying Perl/Python/PHP/Ruby
directly from git and then restarting apache.</p>
Tim Bielawa committed
273
</div>
Michael DeHaan committed
274 275 276
<div class="section" id="managing-services">
<h2>Managing Services<a class="headerlink" href="#managing-services" title="Permalink to this headline"></a></h2>
<p>Ensure a service is started on all webservers:</p>
277
<div class="highlight-python"><pre>ansible webservers -m service -a "name=httpd state=started"</pre>
Michael DeHaan committed
278 279
</div>
<p>Alternatively, restart a service on all webservers:</p>
280 281 282 283
<div class="highlight-python"><pre>ansible webservers -m service -a "name=httpd state=restarted"</pre>
</div>
<p>Ensure a service is stopped:</p>
<div class="highlight-python"><pre>ansible webservers -m service -a "name=httpd state=stopped"</pre>
Michael DeHaan committed
284 285
</div>
</div>
Michael DeHaan committed
286 287
<div class="section" id="time-limited-background-operations">
<h2>Time Limited Background Operations<a class="headerlink" href="#time-limited-background-operations" title="Permalink to this headline"></a></h2>
288 289
<p>Long running operations can be backgrounded, and their status can be
checked on later. The same job ID is given to the same task on all
290 291 292
hosts, so you won&#8217;t lose track.  If you kick hosts and don&#8217;t want
to poll, it looks like this:</p>
<div class="highlight-python"><pre>ansible all -B 3600 -a "/usr/bin/long_running_operation --do-stuff"</pre>
Michael DeHaan committed
293
</div>
294
<p>If you do decide you want to check on the job status later, you can:</p>
295
<div class="highlight-python"><pre>ansible all -m async_status -a "jid=123456789"</pre>
296 297 298 299
</div>
<p>Polling is built-in and looks like this:</p>
<div class="highlight-python"><pre>ansible all -B 3600 -P 60 -a "/usr/bin/long_running_operation --do-stuff"</pre>
</div>
300 301
<p>The above example says &#8220;run for 60 minutes max (60*60=3600), poll for status every 60 seconds&#8221;.</p>
<p>Poll mode is smart so all jobs will be started before polling will begin on any machine.
302
Be sure to use a high enough <cite>&#8211;forks</cite> value if you want to get all of your jobs started
Michael DeHaan committed
303 304
very quickly. After the time limit (in seconds) runs out (<tt class="docutils literal"><span class="pre">-B</span></tt>), the process on
the remote nodes will be terminated.</p>
Michael DeHaan committed
305
<p>Any module other than <cite>copy</cite> or <cite>template</cite> can be
306
backgrounded.  Typically you&#8217;ll be backgrounding long-running
Michael DeHaan committed
307
shell commands or software upgrades only.  <a class="reference internal" href="playbooks.html"><em>Playbooks</em></a> also support polling, and have
308
a simplified syntax for this.</p>
309 310 311 312 313 314 315
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference internal" href="modules.html"><em>Ansible Modules</em></a></dt>
<dd>A list of available modules</dd>
<dt><a class="reference internal" href="playbooks.html"><em>Playbooks</em></a></dt>
<dd>Using ansible for configuration management &amp; deployment</dd>
316 317 318 319
<dt><a class="reference external" href="http://groups.google.com/group/ansible-project">Mailing List</a></dt>
<dd>Questions? Help? Ideas?  Stop by the list on Google Groups</dd>
<dt><a class="reference external" href="http://irc.freenode.net">irc.freenode.net</a></dt>
<dd>#ansible IRC chat channel</dd>
320 321
</dl>
</div>
Michael DeHaan committed
322
</div>
Tim Bielawa committed
323 324 325 326
</div>


</div>
327 328 329 330 331
<footer class="footer">
  <div class="container">
    <p class="pull-right"><a href="#">Back to top</a></p>
    <p>
        &copy; Copyright 2012 Michael DeHaan.<br/>
332
      Last updated on Mar 23, 2012.<br/>
333
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.<br/>
334 335 336
    </p>
  </div>
</footer>
Tim Bielawa committed
337 338
  </body>
</html>