<h1><aclass="toc-backref"href="#contents">Command Line Examples And Next Steps</a><aclass="headerlink"href="#command-line-examples-and-next-steps"title="Permalink to this headline">¶</a></h1>
<h2><aclass="toc-backref"href="#contents">Parallelism and Shell Commands</a><aclass="headerlink"href="#parallelism-and-shell-commands"title="Permalink to this headline">¶</a></h2>
<p>If you don’t want to use ssh-agent and want to instead SSH with a
password instead of keys, you can with <ttclass="docutils literal"><spanclass="pre">--ask-pass</span></tt> (<ttclass="docutils literal"><spanclass="pre">-k</span></tt>), but
it’s much better to just use ssh-agent.</p>
<p>Now to run the command on all servers in a group, in this case,
<em>atlanta</em>, in 10 parallel forks:</p>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible atlanta -a <spanclass="s2">"/sbin/reboot"</span> -f 10
<p>In 0.7 and later, this will default to running from your user account. If you do not like this
behavior, pass in “-u username”. (In 0.6 and before, it defaulted to root. Most folks prefered
defaulting to the current user, so we changed it).</p>
<p>If you want to run commands as a different user, it looks like this:</p>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible atlanta -a <spanclass="s2">"/usr/bin/foo"</span> -u username
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible atlanta -a <spanclass="s2">"/usr/bin/foo"</span> -u username --sudo <spanclass="o">[</span>--ask-sudo-pass<spanclass="o">]</span>
<p>Use <ttclass="docutils literal"><spanclass="pre">--ask-sudo-pass</span></tt> (<ttclass="docutils literal"><spanclass="pre">-K</span></tt>) if you are not using passwordless
sudo. This will interactively prompt you for the password to use.
Use of passwordless sudo makes things easier to automate, but it’s not
required.</p>
<p>It is also possible to sudo to a user other than root using
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible atlanta -a <spanclass="s2">"/usr/bin/foo"</span> -u username -U otheruser <spanclass="o">[</span>--ask-sudo-pass<spanclass="o">]</span>
<p>Ok, so those are basics. If you didn’t read about patterns and groups yet, go back and read <aclass="reference internal"href="patterns.html"><em>Inventory & Patterns</em></a>.</p>
<p>Here’s another use case for the <cite>/usr/bin/ansible</cite> command line. Ansible can SCP lots of files to multiple machines in parallel.</p>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible atlanta -m copy -a <spanclass="s2">"src=/etc/hosts dest=/tmp/hosts"</span>
<p>The <ttclass="docutils literal"><spanclass="pre">file</span></tt> module can also create directories, similar to <ttclass="docutils literal"><spanclass="pre">mkdir</span><spanclass="pre">-p</span></tt>:</p>
<h2><aclass="toc-backref"href="#contents">Managing Packages</a><aclass="headerlink"href="#managing-packages"title="Permalink to this headline">¶</a></h2>
<h2><aclass="toc-backref"href="#contents">Users and Groups</a><aclass="headerlink"href="#users-and-groups"title="Permalink to this headline">¶</a></h2>
<p>The <aclass="reference internal"href="modules.html#user"><em>user</em></a> module allows easy creation and manipulation of
existing user accounts, as well as removal of user accounts that may
exist:</p>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible all -m user -a <spanclass="s2">"name=foo password=<crypted password here>"</span>
<h2><aclass="toc-backref"href="#contents">Deploying From Source Control</a><aclass="headerlink"href="#deploying-from-source-control"title="Permalink to this headline">¶</a></h2>
<h2><aclass="toc-backref"href="#contents">Managing Services</a><aclass="headerlink"href="#managing-services"title="Permalink to this headline">¶</a></h2>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible webservers -m service -a <spanclass="s2">"name=httpd state=started"</span>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible webservers -m service -a <spanclass="s2">"name=httpd state=restarted"</span>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible webservers -m service -a <spanclass="s2">"name=httpd state=stopped"</span>
<h2><aclass="toc-backref"href="#contents">Time Limited Background Operations</a><aclass="headerlink"href="#time-limited-background-operations"title="Permalink to this headline">¶</a></h2>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible all -B 3600 -a <spanclass="s2">"/usr/bin/long_running_operation --do-stuff"</span>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible all -m async_status -a <spanclass="s2">"jid=123456789"</span>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible all -B 1800 -P 60 -a <spanclass="s2">"/usr/bin/long_running_operation --do-stuff"</span>
<p>Typically you’ll be only be backgrounding long-running
shell commands or software upgrades only. Backgrounding the copy module does not do a background file transfer. <aclass="reference internal"href="playbooks.html"><em>Playbooks</em></a> also support polling, and have a simplified syntax for this.</p>
<h2><aclass="toc-backref"href="#contents">Limiting Selected Hosts</a><aclass="headerlink"href="#limiting-selected-hosts"title="Permalink to this headline">¶</a></h2>
<spanclass="versionmodified">New in version 0.7.</span></p>
<p>What hosts you select to manage can be additionally constrained by using the ‘–limit’ parameter or
by using ‘batch’ (or ‘range’) selectors.</p>
<p>As mentioned above, patterns can be strung together to select hosts in more than one group:</p>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible webservers:dbservers -m <spanclass="nb">command</span> -a <spanclass="s2">"/bin/foo xyz"</span>
</pre></div>
</div>
<p>This is an “or” condition. If you want to further constrain the selection, use –limit, which
also works with <ttclass="docutils literal"><spanclass="pre">ansible-playbook</span></tt>:</p>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible webservers:dbservers -m <spanclass="nb">command</span> -a <spanclass="s2">"/bin/foo xyz"</span> region
</pre></div>
</div>
<p>Now let’s talk about range selection. Suppose you have 1000 servers in group ‘datacenter’, but only want to target one at a time. This is also easy:</p>
<h2><aclass="toc-backref"href="#contents">Configuration & Defaults</a><aclass="headerlink"href="#configuration-defaults"title="Permalink to this headline">¶</a></h2>
<p>Ansible has an optional configuration file that can be used to tune settings and also eliminate the need to pass various command line flags. Ansible will look for the config file in the following order, using
the first config file it finds present:</p>
<olclass="arabic simple">
<li>File specified by the <ttclass="docutils literal"><spanclass="pre">ANSIBLE_CONFIG</span></tt> environment variable</li>
<li><ttclass="docutils literal"><spanclass="pre">ansible.cfg</span></tt> in the current working directory. (version 0.8 and up)</li>
<p>For those running from source, a sample configuration file lives in the examples/ directory. The RPM will install configuration into /etc/ansible/ansible.cfg automatically.</p>