<p>If you don’t want to use ssh-agent and want to instead SSH with a password instead of keys, you can with
</div>
–ask-pass (-k), but it’s much better to just use ssh-agent.</p>
<p>If you don’t want to use ssh-agent and want to instead SSH with a
<p>Now to run the command on all servers in a group, in this case, ‘atlanta’, in 10 parallel forks:</p>
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
<divclass="highlight-python"><pre>ansible atlanta -a "/sbin/reboot" -f 10</pre>
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
</pre></div>
</div>
</div>
<p>If you want to run commands as a different user than root, it looks like this:</p>
<p>If you want to run commands as a different user than root, it looks like this:</p>
<divclass="highlight-python"><pre>ansible atlanta -a "/usr/bin/foo" -u yourname</pre>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible atlanta -a <spanclass="s2">"/usr/bin/foo"</span> -u yourname
</pre></div>
</div>
</div>
<p>If you want to run commands through sudo:</p>
<p>If you want to run commands through sudo:</p>
<divclass="highlight-python"><pre>ansible atlanta -a "/usr/bin/foo" -u yourname --sudo [--ask-sudo-pass]</pre>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible atlanta -a <spanclass="s2">"/usr/bin/foo"</span> -u yourname --sudo <spanclass="o">[</span>--ask-sudo-pass<spanclass="o">]</span>
</div>
</pre></div>
<p>Use –ask-sudo-pass (-K) if you are not using passwordless sudo. This will interactively prompt
</div>
you for the password to use. Use of passwordless sudo makes things easier to automate, but it’s
<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
not required.</p>
sudo. This will interactively prompt you for the password to use.
<p>It is also possible to sudo to a user other than root using –sudo-user (-U):</p>
Use of passwordless sudo makes things easier to automate, but it’s not
<divclass="highlight-python"><pre>ansible atlanta -a "/usr/bin/foo" -u yourname -U otheruser [--ask-sudo-pass]</pre>
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 yourname -U otheruser <spanclass="o">[</span>--ask-sudo-pass<spanclass="o">]</span>
</pre></div>
</div>
</div>
<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>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>The -f 10 in the above specifies the usage of 10 simultaneous processes. Normally commands also take
<p>The <ttclass="docutils literal"><spanclass="pre">-f</span><spanclass="pre">10</span></tt> in the above specifies the usage of 10 simultaneous
a <cite>-m</cite> for module name, but the default module name is ‘command’, so we didn’t need to specify that
processes. Normally commands also take a <ttclass="docutils literal"><spanclass="pre">-m</span></tt> for module name, but
all of the time. We’ll use <cite>-m</cite> in later examples to run some other <aclass="reference internal"href="modules.html"><em>Ansible Modules</em></a>.</p>
the default module name is <aclass="reference internal"href="modules.html#command"><em>command</em></a>, so we didn’t need to
<p>Note that the command module requires absolute paths and does not support shell variables. If we want to
specify that all of the time. We’ll use <ttclass="docutils literal"><spanclass="pre">-m</span></tt> in later examples to
execute a module using the shell, we can do those things, and also use pipe and redirection operators.
run some other <aclass="reference internal"href="modules.html"><em>Ansible Modules</em></a>.</p>
Read more about the differences on the <aclass="reference internal"href="modules.html"><em>Ansible Modules</em></a> page. The shell
<divclass="admonition note">
module looks like this:</p>
<pclass="first admonition-title">Note</p>
<divclass="highlight-python"><pre>ansible raleigh -m shell -a 'echo $TERM'</pre>
<pclass="last">The <aclass="reference internal"href="modules.html#command"><em>command</em></a> module requires absolute paths and does not
</div>
support shell variables. If we want to execute a module using a
<p>When running any command with the ansible “ad hoc” CLI (as opposed to playbooks), pay particular attention
shell, we can do those things, and also use pipe and redirection
to shell quoting rules, so the shell doesn’t eat a variable before it gets passed to Ansible. For example,
operators. Read more about the differences on the <aclass="reference internal"href="modules.html"><em>Ansible Modules</em></a>
using double vs single quotes in the above example would evaluate the variable on the box you were on.</p>
page.</p>
<p>So far we’ve been demoing simple command execution, but most ansible modules usually do not work like
</div>
<p>Using the <aclass="reference internal"href="modules.html#shell"><em>shell</em></a> module looks like this:</p>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible raleigh -m shell -a <spanclass="s1">'echo $TERM'</span>
</pre></div>
</div>
<p>When running any command with the ansible <em>ad hoc</em> CLI (as opposed to
<aclass="reference internal"href="playbooks.html"><em>Playbooks</em></a>), pay particular attention to shell quoting rules, so
the shell doesn’t eat a variable before it gets passed to Ansible.
For example, using double vs single quotes in the above example would
evaluate the variable on the box you were on.</p>
<p>So far we’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
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 ‘idempotence’, and is a core design goal of ansible.
get it there. This is commonly referred to as ‘idempotence’, and is a core design goal of ansible.
However, we also recognize that running ad-hoc commands is equally important, so Ansible easily supports both.</p>
However, we also recognize that running <em>ad hoc</em> commands is equally important, so Ansible easily supports both.</p>
</div>
</div>
<divclass="section"id="file-transfer-templating">
<divclass="section"id="file-transfer-templating">
<h2>File Transfer & Templating<aclass="headerlink"href="#file-transfer-templating"title="Permalink to this headline">¶</a></h2>
<h2>File Transfer & Templating<aclass="headerlink"href="#file-transfer-templating"title="Permalink to this headline">¶</a></h2>
@ -239,59 +258,70 @@ However, we also recognize that running ad-hoc commands is equally important, so
<p>Ansible can SCP lots of files to multiple machines in parallel, and
<p>Ansible can SCP lots of files to multiple machines in parallel, and
optionally use them as template sources.</p>
optionally use them as template sources.</p>
<p>To transfer a file directly to many different servers:</p>
<p>To transfer a file directly to many different servers:</p>
<divclass="highlight-python"><pre>ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"</pre>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible atlanta -m copy -a <spanclass="s2">"src=/etc/hosts dest=/tmp/hosts"</span>
</pre></div>
</div>
</div>
<p>To use templating, first run the setup module to put the template
<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
variables you would like to use on the remote host. Then use the
template module to write the files using those templates.</p>
template module to write the files using those templates.</p>
<p>Templates are written in <aclass="reference external"href="http://jinja.pocoo.org/docs/">Jinja2</a> format.
<p>Templates are written in <aclass="reference external"href="http://jinja.pocoo.org/docs/">Jinja2</a>
Playbooks (covered elsewhere in the
format. <aclass="reference internal"href="playbooks.html"><em>Playbooks</em></a> will run the setup module for you, making
documentation) will run the setup module for you, making this even
this even simpler:</p>
simpler:</p>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible webservers -m setup -a <spanclass="s2">"favcolor=red ntp_server=192.168.1.1"</span>
<divclass="highlight-python"><pre>ansible webservers -m setup -a "favcolor=red ntp_server=192.168.1.1"
<spanclass="nv">$ </span>ansible webservers -m template -a <spanclass="s2">"src=/srv/motd.j2 dest=/etc/motd"</span>
ansible webservers -m template -a "src=/srv/motd.j2 dest=/etc/motd"
<spanclass="nv">$ </span>ansible webservers -m template -a <spanclass="s2">"src=/srv/ntp.j2 dest=/etc/ntp.conf"</span>
ansible webservers -m template -a "src=/srv/ntp.j2 dest=/etc/ntp.conf"</pre>
</pre></div>
</div>
</div>
<p>Ansible variables are used in templates by using the name surrounded by double
<p>Ansible variables are used in templates by using the name surrounded
curly-braces. Ansible provides some ‘facts’ about the system being managed
by double curly-braces. Ansible provides some <em>facts</em> about the
automatically in playbooks or when the setup module is run manually. If facter or ohai
system being managed automatically in playbooks or when the setup
were installed on the remote machine, variables
module is run manually. If facter or ohai were installed on the
from those programs can be accessed too, using the appropriate prefix:</p>
remote machine, variables from those programs can be accessed too,
<divclass="highlight-python"><pre>This is an Ansible variable: {{ favcolor }}
using the appropriate prefix:</p>
This is an Ansible fact: {{ ansible_hostname }}
<divclass="highlight-django"><divclass="highlight"><pre><spanclass="x">This is an Ansible variable: </span><spanclass="cp">{{</span><spanclass="nv">favcolor</span><spanclass="cp">}}</span><spanclass="x"></span>
This is a facter fact: {{ facter_hostname }}
<spanclass="x">This is an Ansible fact: </span><spanclass="cp">{{</span><spanclass="nv">ansible_hostname</span><spanclass="cp">}}</span><spanclass="x"></span>
This is an ohai fact: {{ ohai_foo }}</pre>
<spanclass="x">This is a facter fact: </span><spanclass="cp">{{</span><spanclass="nv">facter_hostname</span><spanclass="cp">}}</span><spanclass="x"></span>
<spanclass="x">This is an ohai fact: </span><spanclass="cp">{{</span><spanclass="nv">ohai_foo</span><spanclass="cp">}}</span><spanclass="x"></span>
</pre></div>
</div>
</div>
<p>Using the Ansible facts is generally preferred as that way you can avoid a dependency
<p>Using the Ansible facts is generally preferred as that way you can avoid a dependency
on ruby. If you want to use facter instead, you will also need rubygem-json because
on ruby. If you want to use facter instead, you will also need rubygem-json because
the facter packages may forget this as a dependency.</p>
the facter packages may forget this as a dependency.</p>
<p>The <cite>file</cite> module allows changing ownership and permissions on files. These
<p>The <ttclass="docutils literal"><spanclass="pre">file</span></tt> 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>
same options can be passed directly to the <ttclass="docutils literal"><spanclass="pre">copy</span></tt> or <ttclass="docutils literal"><spanclass="pre">template</span></tt> modules as well:</p>
<divclass="highlight-python"><pre>ansible webservers -m file -a "dest=/srv/foo/a.txt mode=600"
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible webservers -m file -a <spanclass="s2">"dest=/srv/foo/a.txt mode=600"</span>
ansible webservers -m file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"</pre>
<spanclass="nv">$ </span>ansible webservers -m file -a <spanclass="s2">"dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"</span>
</pre></div>
</div>
</div>
<p>The <cite>file</cite> module can also create directories, similar to <cite>mkdir -p</cite>:</p>
<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>
<divclass="highlight-python"><pre>ansible webservers -m file -a "dest=/path/to/c mode=644 owner=mdehaan group=mdehaan state=directory"</pre>
<p>As well as delete directories (recursively) and delete files:</p>
<p>As well as delete directories (recursively) and delete files:</p>
<divclass="highlight-python"><pre>ansible webservers -m file -a "dest=/path/to/c state=absent"</pre>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible webservers -m file -a <spanclass="s2">"dest=/path/to/c state=absent"</span>
</pre></div>
</div>
</div>
<p>The mode, owner, and group arguments can also be used on the copy or template lines.</p>
<p>The mode, owner, and group arguments can also be used on the copy or template lines.</p>
</div>
</div>
<divclass="section"id="managing-packages">
<divclass="section"id="managing-packages">
<h2>Managing Packages<aclass="headerlink"href="#managing-packages"title="Permalink to this headline">¶</a></h2>
<h2>Managing Packages<aclass="headerlink"href="#managing-packages"title="Permalink to this headline">¶</a></h2>
<p>There are modules available for yum and apt. Here are some examples with yum.</p>
<p>There are modules available for yum and apt. Here are some examples
with <aclass="reference internal"href="modules.html#yum"><em>yum</em></a>.</p>
<p>Ensure a package is installed, but don’t update it:</p>
<p>Ensure a package is installed, but don’t update it:</p>
<divclass="highlight-python"><pre>ansible webservers -m yum -a "pkg=acme state=installed"</pre>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible webservers -m yum -a <spanclass="s2">"pkg=acme state=installed"</span>
</pre></div>
</div>
</div>
<p>Ensure a package is installed to a specific version:</p>
<p>Ensure a package is installed to a specific version:</p>
<divclass="highlight-python"><pre>ansible webservers -m yum -a "pkg=acme-1.5 state=installed"</pre>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible webservers -m yum -a <spanclass="s2">"pkg=acme-1.5 state=installed"</span>
</pre></div>
</div>
</div>
<p>Ensure a package is at the latest version:</p>
<p>Ensure a package is at the latest version:</p>
<divclass="highlight-python"><pre>ansible webservers -m yum -a "pkg=acme state=latest"</pre>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible webservers -m yum -a <spanclass="s2">"pkg=acme state=latest"</span>
</pre></div>
</div>
</div>
<p>Ensure a package is not installed:</p>
<p>Ensure a package is not installed:</p>
<divclass="highlight-python"><pre>ansible webservers -m yum -a "pkg=acme state=removed"</pre>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible webservers -m yum -a <spanclass="s2">"pkg=acme state=removed"</span>
</pre></div>
</div>
</div>
<p>Currently Ansible only has modules for managing packages with yum and apt. You can install
<p>Currently Ansible only has modules for managing packages with yum and apt. You can install
for other packages for now using the command module or (better!) contribute a module
for other packages for now using the command module or (better!) contribute a module
@ -299,11 +329,13 @@ for other package managers. Stop by the mailing list for info/details.</p>
</div>
</div>
<divclass="section"id="users-and-groups">
<divclass="section"id="users-and-groups">
<h2>Users and Groups<aclass="headerlink"href="#users-and-groups"title="Permalink to this headline">¶</a></h2>
<h2>Users and Groups<aclass="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
<p>The <aclass="reference internal"href="modules.html#user"><em>user</em></a> module allows easy creation and manipulation of
as removal of user accounts that may exist:</p>
existing user accounts, as well as removal of user accounts that may
<divclass="highlight-python"><pre>ansible all -m user -a "name=foo password=<crypted password here>"
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>
ansible all -m user -a "name=foo state=absent"</pre>
<spanclass="nv">$ </span>ansible all -m user -a <spanclass="s2">"name=foo state=absent"</span>
</pre></div>
</div>
</div>
<p>See the <aclass="reference internal"href="modules.html"><em>Ansible Modules</em></a> section for details on all of the available options, including
<p>See the <aclass="reference internal"href="modules.html"><em>Ansible Modules</em></a> section for details on all of the available options, including
how to manipulate groups and group membership.</p>
how to manipulate groups and group membership.</p>
@ -311,23 +343,27 @@ how to manipulate groups and group membership.</p>
<h2>Deploying From Source Control<aclass="headerlink"href="#deploying-from-source-control"title="Permalink to this headline">¶</a></h2>
<h2>Deploying From Source Control<aclass="headerlink"href="#deploying-from-source-control"title="Permalink to this headline">¶</a></h2>
<p>Deploy your webapp straight from git:</p>
<p>Deploy your webapp straight from git:</p>
<divclass="highlight-python"><pre>ansible webservers -m git -a "repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD"</pre>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible webservers -m git -a <spanclass="s2">"repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD"</span>
</pre></div>
</div>
</div>
<p>Since ansible modules can notify change handlers (see
<p>Since ansible modules can notify change handlers it is possible to
<aclass="reference internal"href="playbooks.html"><em>Playbooks</em></a>) it is possible to tell ansible to run specific tasks
tell ansible to run specific tasks when the code is updated, such as
when the code is updated, such as deploying Perl/Python/PHP/Ruby
deploying Perl/Python/PHP/Ruby directly from git and then restarting
directly from git and then restarting apache.</p>
apache.</p>
</div>
</div>
<divclass="section"id="managing-services">
<divclass="section"id="managing-services">
<h2>Managing Services<aclass="headerlink"href="#managing-services"title="Permalink to this headline">¶</a></h2>
<h2>Managing Services<aclass="headerlink"href="#managing-services"title="Permalink to this headline">¶</a></h2>
<p>Ensure a service is started on all webservers:</p>
<p>Ensure a service is started on all webservers:</p>
<divclass="highlight-python"><pre>ansible webservers -m service -a "name=httpd state=started"</pre>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible webservers -m service -a <spanclass="s2">"name=httpd state=started"</span>
</pre></div>
</div>
</div>
<p>Alternatively, restart a service on all webservers:</p>
<p>Alternatively, restart a service on all webservers:</p>
<divclass="highlight-python"><pre>ansible webservers -m service -a "name=httpd state=restarted"</pre>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible webservers -m service -a <spanclass="s2">"name=httpd state=restarted"</span>
</pre></div>
</div>
</div>
<p>Ensure a service is stopped:</p>
<p>Ensure a service is stopped:</p>
<divclass="highlight-python"><pre>ansible webservers -m service -a "name=httpd state=stopped"</pre>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible webservers -m service -a <spanclass="s2">"name=httpd state=stopped"</span>
@ -336,20 +372,24 @@ directly from git and then restarting apache.</p>
checked on later. The same job ID is given to the same task on all
checked on later. The same job ID is given to the same task on all
hosts, so you won’t lose track. If you kick hosts and don’t want
hosts, so you won’t lose track. If you kick hosts and don’t want
to poll, it looks like this:</p>
to poll, it looks like this:</p>
<divclass="highlight-python"><pre>ansible all -B 3600 -a "/usr/bin/long_running_operation --do-stuff"</pre>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible all -B 3600 -a <spanclass="s2">"/usr/bin/long_running_operation --do-stuff"</span>
</pre></div>
</div>
</div>
<p>If you do decide you want to check on the job status later, you can:</p>
<p>If you do decide you want to check on the job status later, you can:</p>
<divclass="highlight-python"><pre>ansible all -m async_status -a "jid=123456789"</pre>
<divclass="highlight-bash"><divclass="highlight"><pre><spanclass="nv">$ </span>ansible all -m async_status -a <spanclass="s2">"jid=123456789"</span>
</pre></div>
</div>
</div>
<p>Polling is built-in and looks like this:</p>
<p>Polling is built-in and looks like this:</p>
<divclass="highlight-python"><pre>ansible all -B 3600 -P 60 -a "/usr/bin/long_running_operation --do-stuff"</pre>
<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>
</pre></div>
</div>
</div>
<p>The above example says “run for 60 minutes max (60*60=3600), poll for status every 60 seconds”.</p>
<p>The above example says “run for 30 minutes max (<ttclass="docutils literal"><spanclass="pre">-B</span></tt>: 30*60=1800),
poll for status (<ttclass="docutils literal"><spanclass="pre">-P</span></tt>) every 60 seconds”.</p>
<p>Poll mode is smart so all jobs will be started before polling will begin on any machine.
<p>Poll mode is smart so all jobs will be started before polling will begin on any machine.
Be sure to use a high enough <cite>–forks</cite> value if you want to get all of your jobs started
Be sure to use a high enough <tt class="docutils literal"><spanclass="pre">--forks</span></tt> value if you want to get all of your jobs started
very quickly. After the time limit (in seconds) runs out (<ttclass="docutils literal"><spanclass="pre">-B</span></tt>), the process on
very quickly. After the time limit (in seconds) runs out (<ttclass="docutils literal"><spanclass="pre">-B</span></tt>), the process on
the remote nodes will be terminated.</p>
the remote nodes will be terminated.</p>
<p>Any module other than <cite>copy</cite> or <cite>template</cite> can be
<p>Any module other than <tt class="docutils literal"><spanclass="pre">copy</span></tt> or <ttclass="docutils literal"><spanclass="pre">template</span></tt> can be
backgrounded. Typically you’ll be backgrounding long-running
backgrounded. Typically you’ll be backgrounding long-running
shell commands or software upgrades only. <aclass="reference internal"href="playbooks.html"><em>Playbooks</em></a> also support polling, and have
shell commands or software upgrades only. <aclass="reference internal"href="playbooks.html"><em>Playbooks</em></a> also support polling, and have