<ahref="http://github.com/ansible/ansible"><imgstyle="position: absolute; right: 0; border: 0;"src="http://ansible.github.com/github.png"alt="Fork me on GitHub"></a>
<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>
<h2>File Transfer<aclass="headerlink"href="#file-transfer"title="Permalink to this headline">¶</a></h2>
<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>
<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>
<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>
<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>Limiting Selected Hosts<aclass="headerlink"href="#limiting-selected-hosts"title="Permalink to this headline">¶</a></h2>
<pclass="versionadded">
<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>Configuration & Defaults<aclass="headerlink"href="#configuration-defaults"title="Permalink to this headline">¶</a></h2>
<pclass="versionadded">
<spanclass="versionmodified">New in version 0.7.</span></p>
<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. The config file location is controlled by the ANSIBLE_CONFIG environment variable, if set, otherwise ~/ansible.cfg or /etc/ansible/ansible.cfg will be loaded, whichever comes first. 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>
<inputtype="image"src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif"border="0"name="submit"alt="PayPal - The safer, easier way to pay online!">