2012-03-07 17:35:18 +01:00
< !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" / >
2012-05-13 17:00:02 +02:00
< title > Command Line — Ansible - SSH-Based Configuration Management & Deployment< / title >
2012-03-07 17:35:18 +01:00
< link rel = "stylesheet" href = "_static/default.css" type = "text/css" / >
< link rel = "stylesheet" href = "_static/pygments.css" type = "text/css" / >
2012-03-09 17:44:14 +01:00
< link rel = "stylesheet" href = "_static/bootstrap.css" type = "text/css" / >
< link rel = "stylesheet" href = "_static/bootstrap-sphinx.css" type = "text/css" / >
2012-03-07 17:35:18 +01:00
< script type = "text/javascript" >
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '',
2012-03-13 00:13:19 +01:00
VERSION: '0.01',
2012-03-07 17:35:18 +01:00
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
2012-03-09 17:50:07 +01:00
HAS_SOURCE: false
2012-03-07 17:35:18 +01:00
};
< / 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 >
2012-03-09 17:44:14 +01:00
< script type = "text/javascript" src = "_static/bootstrap-dropdown.js" > < / script >
< script type = "text/javascript" src = "_static/bootstrap-scrollspy.js" > < / script >
2012-04-01 04:44:02 +02:00
< link rel = "shortcut icon" href = "_static/favicon.ico" / >
2012-03-13 00:13:19 +01:00
< link rel = "top" title = "Ansible - SSH-Based Configuration Management & Deployment" href = "index.html" / >
2012-03-09 04:50:12 +01:00
< link rel = "next" title = "Ansible Modules" href = "modules.html" / >
2012-05-13 17:00:02 +02:00
< link rel = "prev" title = "Inventory & Patterns" href = "patterns.html" / >
2012-03-09 17:44:14 +01:00
< 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();
});
}());
2012-03-10 04:31:54 +01:00
< / script >
2012-04-28 16:46:09 +02:00
2012-03-10 04:31:54 +01:00
< 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);
})();
2012-03-09 17:44:14 +01:00
< / script >
2012-04-28 16:46:09 +02:00
< script type = "text/javascript" >
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
< / script >
2012-04-28 16:42:32 +02:00
< script > ( f u n c t i o n ( d , s , i d ) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));< / script >
2012-03-09 17:44:14 +01:00
2012-03-07 17:35:18 +01:00
< / head >
< body >
2012-03-09 17:44:14 +01:00
< div class = "topbar" data-scrollspy = "scrollspy" >
< div class = "topbar-inner" >
< div class = "container" >
2012-04-18 02:47:29 +02:00
<!-- <a class="brand" href="index.html">Ansible</a> -->
2012-03-09 17:44:14 +01:00
< ul class = "nav" >
< li class = "dropdown" data-dropdown = "dropdown" >
< a href = "index.html"
2012-03-31 15:29:31 +02:00
class="dropdown-toggle">Chapter< / a >
2012-03-09 17:44:14 +01:00
< span class = "globaltoc" > < ul class = "current" >
2012-05-13 17:00:02 +02:00
< li class = "toctree-l1" > < a class = "reference internal" href = "gettingstarted.html" > Getting Started< / a > < / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "patterns.html" > Inventory & Patterns< / a > < / li >
< li class = "toctree-l1 current" > < a class = "current reference internal" href = "" > Command Line< / a > < / li >
2012-03-09 17:44:14 +01:00
< li class = "toctree-l1" > < a class = "reference internal" href = "modules.html" > Ansible Modules< / a > < / li >
2012-03-11 20:34:21 +01:00
< li class = "toctree-l1" > < a class = "reference internal" href = "YAMLSyntax.html" > YAML Syntax< / a > < / li >
2012-03-09 17:44:14 +01:00
< li class = "toctree-l1" > < a class = "reference internal" href = "playbooks.html" > Playbooks< / a > < / li >
2012-05-13 17:00:02 +02:00
< li class = "toctree-l1" > < a class = "reference internal" href = "playbooks2.html" > Advanced Playbooks< / a > < / li >
2012-05-13 17:56:09 +02:00
< li class = "toctree-l1" > < a class = "reference internal" href = "bestpractices.html" > Best Practices< / a > < / li >
2012-03-31 04:28:30 +02:00
< li class = "toctree-l1" > < a class = "reference internal" href = "api.html" > API & Integrations< / a > < / li >
2012-05-13 17:00:02 +02:00
< li class = "toctree-l1" > < a class = "reference internal" href = "moduledev.html" > Module Development< / a > < / li >
2012-05-13 17:56:09 +02:00
< li class = "toctree-l1" > < a class = "reference internal" href = "faq.html" > FAQ< / a > < / li >
2012-07-26 05:33:32 +02:00
< li class = "toctree-l1" > < a class = "reference internal" href = "who_uses_ansible.html" > Who Uses Ansible< / a > < / li >
2012-03-09 17:44:14 +01:00
< / ul >
< / span >
2012-03-31 15:36:37 +02:00
< / li >
< li class = "dropdown" data-dropdown = "dropdown" >
< a href = "#"
class="dropdown-toggle">Page< / a >
< span class = "localtoc" > < ul >
2012-05-13 17:00:02 +02:00
< li > < a class = "reference internal" href = "#" > Command Line< / a > < ul >
2012-03-31 15:36:37 +02:00
< 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 & Templating< / a > < / li >
< li > < a class = "reference internal" href = "#managing-packages" > Managing Packages< / a > < / li >
2012-03-22 06:01:02 +01:00
< li > < a class = "reference internal" href = "#users-and-groups" > Users and Groups< / a > < / li >
2012-03-31 15:36:37 +02:00
< 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 >
2012-08-15 04:07:00 +02:00
< li > < a class = "reference internal" href = "#limiting-selected-hosts" > Limiting Selected Hosts< / a > < / li >
2012-03-31 15:36:37 +02:00
< / ul >
< / li >
< / ul >
< / span >
2012-03-09 17:44:14 +01:00
< / li >
< / 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 >
2012-04-28 17:20:38 +02:00
< a href = "http://github.com/ansible/ansible" > < img style = "position: absolute; right: 0; border: 0;" src = "http://ansible.github.com/github.png" alt = "Fork me on GitHub" > < / a >
2012-03-09 17:44:14 +01:00
2012-03-10 16:39:34 +01:00
2012-03-09 17:44:14 +01:00
< div class = "container" >
2012-04-19 15:24:45 +02:00
< a href = "http://ansible.github.com" > < img src = "http://ansible.github.com/ansible-logo.png" alt = "Ansible" / > < / a > < br / >
2012-04-28 16:42:32 +02:00
< br / >
2012-03-09 17:44:14 +01:00
2012-05-13 17:00:02 +02:00
< div class = "section" id = "command-line" >
< h1 > Command Line< a class = "headerlink" href = "#command-line" title = "Permalink to this headline" > ¶< / a > < / h1 >
2012-08-10 04:07:09 +02:00
< p > The following examples show how to use < cite > /usr/bin/ansible< / cite > for running
ad hoc tasks. Start here.< / p >
2012-03-22 06:14:26 +01:00
< p > For configuration management and deployments, you’ ll want to pick up on
using < cite > /usr/bin/ansible-playbook< / cite > – the concepts port over directly.
(See < a class = "reference internal" href = "playbooks.html" > < em > Playbooks< / em > < / a > for more information about those)< / p >
2012-03-08 19:36:47 +01:00
< 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 >
2012-03-22 06:14:26 +01:00
< p > Let’ s use ansible’ s command line tool to reboot all web servers in Atlanta, 10 at a time. First, let’ s
set up SSH-agent so it can remember our credentials:< / p >
2012-08-10 04:07:09 +02:00
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ssh-agent bash
< span class = "nv" > $ < / span > ssh-add ~/.ssh/id_rsa.pub
< / pre > < / div >
2012-03-08 19:36:47 +01:00
< / div >
2012-08-10 04:07:09 +02:00
< p > If you don’ t want to use ssh-agent and want to instead SSH with a
password instead of keys, you can with < tt class = "docutils literal" > < span class = "pre" > --ask-pass< / span > < / tt > (< tt class = "docutils literal" > < span class = "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 >
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible atlanta -a < span class = "s2" > " /sbin/reboot" < / span > -f 10
< / pre > < / div >
2012-03-31 16:50:47 +02:00
< / div >
2012-05-10 07:41:31 +02:00
< p > If you want to run commands as a different user than root, it looks like this:< / p >
2012-08-10 04:07:09 +02:00
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible atlanta -a < span class = "s2" > " /usr/bin/foo" < / span > -u yourname
< / pre > < / div >
2012-03-31 16:43:34 +02:00
< / div >
< p > If you want to run commands through sudo:< / p >
2012-08-10 04:07:09 +02:00
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible atlanta -a < span class = "s2" > " /usr/bin/foo" < / span > -u yourname --sudo < span class = "o" > [< / span > --ask-sudo-pass< span class = "o" > ]< / span >
< / pre > < / div >
2012-05-02 06:55:54 +02:00
< / div >
2012-08-10 04:07:09 +02:00
< p > Use < tt class = "docutils literal" > < span class = "pre" > --ask-sudo-pass< / span > < / tt > (< tt class = "docutils literal" > < span class = "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
< tt class = "docutils literal" > < span class = "pre" > --sudo-user< / span > < / tt > (< tt class = "docutils literal" > < span class = "pre" > -U< / span > < / tt > ):< / p >
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible atlanta -a < span class = "s2" > " /usr/bin/foo" < / span > -u yourname -U otheruser < span class = "o" > [< / span > --ask-sudo-pass< span class = "o" > ]< / span >
< / pre > < / div >
2012-05-10 07:41:31 +02:00
< / div >
2012-05-13 17:00:02 +02:00
< p > Ok, so those are basics. If you didn’ t read about patterns and groups yet, go back and read < a class = "reference internal" href = "patterns.html" > < em > Inventory & Patterns< / em > < / a > .< / p >
2012-08-10 04:07:09 +02:00
< p > The < tt class = "docutils literal" > < span class = "pre" > -f< / span > < span class = "pre" > 10< / span > < / tt > in the above specifies the usage of 10 simultaneous
processes. Normally commands also take a < tt class = "docutils literal" > < span class = "pre" > -m< / span > < / tt > for module name, but
the default module name is < a class = "reference internal" href = "modules.html#command" > < em > command< / em > < / a > , so we didn’ t need to
specify that all of the time. We’ ll use < tt class = "docutils literal" > < span class = "pre" > -m< / span > < / tt > in later examples to
run some other < a class = "reference internal" href = "modules.html" > < em > Ansible Modules< / em > < / a > .< / p >
< div class = "admonition note" >
< p class = "first admonition-title" > Note< / p >
< p class = "last" > The < a class = "reference internal" href = "modules.html#command" > < em > command< / em > < / a > module requires absolute paths and does not
support shell variables. If we want to execute a module using a
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.< / p >
2012-03-31 16:50:47 +02:00
< / div >
2012-08-10 04:07:09 +02:00
< p > Using the < a class = "reference internal" href = "modules.html#shell" > < em > shell< / em > < / a > module looks like this:< / p >
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible raleigh -m shell -a < span class = "s1" > ' echo $TERM' < / span >
< / pre > < / div >
< / div >
< p > When running any command with the ansible < em > ad hoc< / em > CLI (as opposed to
< a class = "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
2012-03-22 06:14:26 +01:00
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.
2012-08-10 04:07:09 +02:00
However, we also recognize that running < em > ad hoc< / em > commands is equally important, so Ansible easily supports both.< / p >
2012-03-08 19:36:47 +01:00
< / div >
2012-03-09 04:50:12 +01:00
< div class = "section" id = "file-transfer-templating" >
< h2 > File Transfer & Templating< a class = "headerlink" href = "#file-transfer-templating" title = "Permalink to this headline" > ¶< / a > < / h2 >
2012-03-22 06:14:26 +01:00
< p > Here’ s another use case for the < cite > /usr/bin/ansible< / cite > command line.< / p >
2012-03-10 00:29:01 +01:00
< p > Ansible can SCP lots of files to multiple machines in parallel, and
optionally use them as template sources.< / p >
2012-05-02 06:55:54 +02:00
< p > To transfer a file directly to many different servers:< / p >
2012-08-10 04:07:09 +02:00
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible atlanta -m copy -a < span class = "s2" > " src=/etc/hosts dest=/tmp/hosts" < / span >
< / pre > < / div >
2012-03-08 19:36:47 +01:00
< / div >
2012-08-15 04:07:00 +02:00
< p > If you use playbooks, you can also take advantage of the template module,
which takes this another step further.< / p >
2012-08-10 04:07:09 +02:00
< p > The < tt class = "docutils literal" > < span class = "pre" > file< / span > < / tt > module allows changing ownership and permissions on files. These
same options can be passed directly to the < tt class = "docutils literal" > < span class = "pre" > copy< / span > < / tt > or < tt class = "docutils literal" > < span class = "pre" > template< / span > < / tt > modules as well:< / p >
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible webservers -m file -a < span class = "s2" > " dest=/srv/foo/a.txt mode=600" < / span >
< span class = "nv" > $ < / span > ansible webservers -m file -a < span class = "s2" > " dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan" < / span >
< / pre > < / div >
2012-03-16 03:57:10 +01:00
< / div >
2012-08-10 04:07:09 +02:00
< p > The < tt class = "docutils literal" > < span class = "pre" > file< / span > < / tt > module can also create directories, similar to < tt class = "docutils literal" > < span class = "pre" > mkdir< / span > < span class = "pre" > -p< / span > < / tt > :< / p >
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible webservers -m file -a < span class = "s2" > " dest=/path/to/c mode=644 owner=mdehaan group=mdehaan state=directory" < / span >
< / pre > < / div >
2012-03-16 03:57:10 +01:00
< / div >
< p > As well as delete directories (recursively) and delete files:< / p >
2012-08-10 04:07:09 +02:00
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible webservers -m file -a < span class = "s2" > " dest=/path/to/c state=absent" < / span >
< / pre > < / div >
2012-03-16 03:57:10 +01:00
< / div >
2012-03-31 17:19:35 +02:00
< p > The mode, owner, and group arguments can also be used on the copy or template lines.< / p >
2012-03-07 17:35:18 +01:00
< / div >
2012-03-10 17:40:49 +01:00
< div class = "section" id = "managing-packages" >
< h2 > Managing Packages< a class = "headerlink" href = "#managing-packages" title = "Permalink to this headline" > ¶< / a > < / h2 >
2012-08-10 04:07:09 +02:00
< p > There are modules available for yum and apt. Here are some examples
with < a class = "reference internal" href = "modules.html#yum" > < em > yum< / em > < / a > .< / p >
2012-03-10 17:40:49 +01:00
< p > Ensure a package is installed, but don’ t update it:< / p >
2012-08-10 04:07:09 +02:00
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible webservers -m yum -a < span class = "s2" > " pkg=acme state=installed" < / span >
< / pre > < / div >
2012-03-10 17:40:49 +01:00
< / div >
< p > Ensure a package is installed to a specific version:< / p >
2012-08-10 04:07:09 +02:00
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible webservers -m yum -a < span class = "s2" > " pkg=acme-1.5 state=installed" < / span >
< / pre > < / div >
2012-03-10 17:40:49 +01:00
< / div >
< p > Ensure a package is at the latest version:< / p >
2012-08-10 04:07:09 +02:00
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible webservers -m yum -a < span class = "s2" > " pkg=acme state=latest" < / span >
< / pre > < / div >
2012-03-10 17:40:49 +01:00
< / div >
< p > Ensure a package is not installed:< / p >
2012-08-10 04:07:09 +02:00
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible webservers -m yum -a < span class = "s2" > " pkg=acme state=removed" < / span >
< / pre > < / div >
2012-03-31 16:40:01 +02:00
< / div >
2012-05-02 06:55:54 +02:00
< p > Currently Ansible only has modules for managing packages with yum and apt. You can install
2012-03-22 06:14:26 +01:00
for other packages for now using the command module or (better!) contribute a module
2012-03-10 17:40:49 +01:00
for other package managers. Stop by the mailing list for info/details.< / p >
< / div >
2012-03-22 06:01:02 +01:00
< 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 >
2012-08-10 04:07:09 +02:00
< p > The < a class = "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 >
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible all -m user -a < span class = "s2" > " name=foo password=< crypted password here> " < / span >
2012-03-22 06:01:02 +01:00
2012-08-10 04:07:09 +02:00
< span class = "nv" > $ < / span > ansible all -m user -a < span class = "s2" > " name=foo state=absent" < / span >
< / pre > < / div >
2012-03-22 06:01:02 +01:00
< / div >
2012-04-06 17:41:18 +02:00
< p > See the < a class = "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 >
2012-03-22 06:01:02 +01:00
< / div >
2012-03-09 04:50:12 +01:00
< 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 >
2012-03-08 19:36:47 +01:00
< p > Deploy your webapp straight from git:< / p >
2012-08-10 04:07:09 +02:00
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible webservers -m git -a < span class = "s2" > " repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD" < / span >
< / pre > < / div >
2012-03-07 17:35:18 +01:00
< / div >
2012-08-10 04:07:09 +02:00
< p > Since ansible modules can notify change handlers 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 >
2012-03-07 17:35:18 +01:00
< / div >
2012-03-09 04:50:12 +01:00
< 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 >
2012-08-10 04:07:09 +02:00
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible webservers -m service -a < span class = "s2" > " name=httpd state=started" < / span >
< / pre > < / div >
2012-03-09 04:50:12 +01:00
< / div >
< p > Alternatively, restart a service on all webservers:< / p >
2012-08-10 04:07:09 +02:00
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible webservers -m service -a < span class = "s2" > " name=httpd state=restarted" < / span >
< / pre > < / div >
2012-03-10 17:40:49 +01:00
< / div >
< p > Ensure a service is stopped:< / p >
2012-08-10 04:07:09 +02:00
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible webservers -m service -a < span class = "s2" > " name=httpd state=stopped" < / span >
< / pre > < / div >
2012-03-09 04:50:12 +01:00
< / div >
< / div >
2012-03-09 13:42:53 +01:00
< 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 >
2012-03-10 00:29:01 +01:00
< 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
2012-03-12 02:09:21 +01:00
hosts, so you won’ t lose track. If you kick hosts and don’ t want
to poll, it looks like this:< / p >
2012-08-10 04:07:09 +02:00
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible all -B 3600 -a < span class = "s2" > " /usr/bin/long_running_operation --do-stuff" < / span >
< / pre > < / div >
2012-03-09 13:42:53 +01:00
< / div >
2012-03-12 02:09:21 +01:00
< p > If you do decide you want to check on the job status later, you can:< / p >
2012-08-10 04:07:09 +02:00
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible all -m async_status -a < span class = "s2" > " jid=123456789" < / span >
< / pre > < / div >
2012-03-12 02:09:21 +01:00
< / div >
< p > Polling is built-in and looks like this:< / p >
2012-08-10 04:07:09 +02:00
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible all -B 1800 -P 60 -a < span class = "s2" > " /usr/bin/long_running_operation --do-stuff" < / span >
< / pre > < / div >
2012-03-12 02:09:21 +01:00
< / div >
2012-08-10 04:07:09 +02:00
< p > The above example says “ run for 30 minutes max (< tt class = "docutils literal" > < span class = "pre" > -B< / span > < / tt > : 30*60=1800),
poll for status (< tt class = "docutils literal" > < span class = "pre" > -P< / span > < / tt > ) every 60 seconds” .< / p >
2012-03-31 16:41:29 +02:00
< p > Poll mode is smart so all jobs will be started before polling will begin on any machine.
2012-08-10 04:07:09 +02:00
Be sure to use a high enough < tt class = "docutils literal" > < span class = "pre" > --forks< / span > < / tt > value if you want to get all of your jobs started
2012-03-12 04:20:55 +01:00
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 >
2012-08-10 04:07:09 +02:00
< p > Any module other than < tt class = "docutils literal" > < span class = "pre" > copy< / span > < / tt > or < tt class = "docutils literal" > < span class = "pre" > template< / span > < / tt > can be
2012-03-15 01:57:35 +01:00
backgrounded. Typically you’ ll be backgrounding long-running
2012-03-17 22:16:31 +01:00
shell commands or software upgrades only. < a class = "reference internal" href = "playbooks.html" > < em > Playbooks< / em > < / a > also support polling, and have
2012-03-31 16:41:29 +02:00
a simplified syntax for this.< / p >
2012-08-15 04:07:00 +02:00
< / div >
< div class = "section" id = "limiting-selected-hosts" >
< h2 > Limiting Selected Hosts< a class = "headerlink" href = "#limiting-selected-hosts" title = "Permalink to this headline" > ¶< / a > < / h2 >
< p class = "versionadded" >
< span class = "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 >
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible webservers:dbservers -m < span class = "nb" > command< / span > -a < span class = "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 < tt class = "docutils literal" > < span class = "pre" > ansible-playbook< / span > < / tt > :< / p >
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible webservers:dbservers -m < span class = "nb" > command< / span > -a < span class = "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 >
2012-08-15 04:11:56 +02:00
< div class = "highlight-bash" > < div class = "highlight" > < pre > < span class = "nv" > $ < / span > ansible webservers< span class = "o" > [< / span > 0-99< span class = "o" > ]< / span > -m < span class = "nb" > command< / span > -a < span class = "s2" > " /bin/foo xyz" < / span >
< span class = "nv" > $ < / span > ansible webservers< span class = "o" > [< / span > 100-199< span class = "o" > ]< / span > -m < span class = "nb" > command< / span > -a < span class = "s2" > " /bin/foo xyz" < / span >
2012-08-15 04:07:00 +02:00
< / pre > < / div >
< / div >
2012-08-15 04:11:56 +02:00
< p > This will select the first 100, then the second 100, host entries in the webservers group. (It does not matter
what their names or IP addresses are).< / p >
2012-08-15 04:07:00 +02:00
< p > Both of these methods can be used at the same time, and ranges can also be passed to the – limit parameter.< / p >
2012-03-31 15:29:31 +02:00
< 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 & deployment< / dd >
2012-03-31 15:55:37 +02:00
< 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 >
2012-03-31 15:29:31 +02:00
< / dl >
< / div >
2012-03-09 13:42:53 +01:00
< / div >
2012-03-07 17:35:18 +01:00
< / div >
2012-04-28 16:42:32 +02:00
< br / >
2012-03-07 17:35:18 +01:00
< / div >
2012-04-28 16:53:15 +02:00
< footer class = "footer" >
< div class = "container" >
< div id = "fb-root" > < / div >
2012-04-28 16:50:20 +02:00
< p >
2012-07-31 02:08:13 +02:00
< form action = "https://www.paypal.com/cgi-bin/webscr" method = "post" >
< input type = "hidden" name = "cmd" value = "_donations" >
< input type = "hidden" name = "business" value = "michael.dehaan@gmail.com" >
< input type = "hidden" name = "lc" value = "US" >
< input type = "hidden" name = "item_name" value = "Ansible" >
< input type = "hidden" name = "no_note" value = "0" >
< input type = "hidden" name = "currency_code" value = "USD" >
< input type = "hidden" name = "bn" value = "PP-DonationsBF:btn_donate_LG.gif:NonHostedGuest" >
< input type = "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!" >
< img alt = "" border = "0" src = "https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width = "1" height = "1" >
< / form >
< / p >
< p >
2012-04-28 16:42:32 +02:00
< a href = "https://twitter.com/share" class = "twitter-share-button" data-text = "ansible.github.com" > Share On Twitter< / a >
< script > ! function ( d , s , id ) { var js , fjs = d . getElementsByTagName ( s ) [ 0 ] ; if ( ! d . getElementById ( id ) ) { js = d . createElement ( s ) ; js . id = id ; js . src = "//platform.twitter.com/widgets.js" ; fjs . parentNode . insertBefore ( js , fjs ) ; } } ( document , "script" , "twitter-wjs" ) ; < / script >
< g:plusone annotation = "inline" > < / g:plusone >
< div class = "fb-like" data-href = "http://ansible.github.com" data-send = "true" data-width = "450" data-show-faces = "false" > < / div >
2012-04-28 16:50:20 +02:00
< / p >
< p >
2012-03-09 17:44:14 +01:00
© Copyright 2012 Michael DeHaan.< br / >
2012-08-15 04:07:00 +02:00
Last updated on Aug 14, 2012.< br / >
2012-03-09 17:44:14 +01:00
< / p >
< / div >
< / footer >
2012-03-07 17:35:18 +01:00
< / body >
2012-04-18 01:59:53 +02:00
< / html >