mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-23 21:30:22 -07:00
Updating various doc items with 0.6 features, releasing soon, and removing references to things new in 0.4, which
has been out long enough to no longer be new.
This commit is contained in:
parent
dac0db9ca2
commit
1aa3c1522c
21 changed files with 360 additions and 152 deletions
101
playbooks.html
101
playbooks.html
|
@ -149,7 +149,7 @@ s.parentNode.insertBefore(ga, s);
|
|||
class="dropdown-toggle">Page</a>
|
||||
<span class="localtoc"><ul>
|
||||
<li><a class="reference internal" href="#">Playbooks</a><ul>
|
||||
<li><a class="reference internal" href="#playbook-example">Playbook Example</a></li>
|
||||
<li><a class="reference internal" href="#playbook-language-example">Playbook Language Example</a></li>
|
||||
<li><a class="reference internal" href="#basics">Basics</a><ul>
|
||||
<li><a class="reference internal" href="#hosts-and-users">Hosts and Users</a></li>
|
||||
<li><a class="reference internal" href="#vars-section">Vars section</a></li>
|
||||
|
@ -157,7 +157,7 @@ s.parentNode.insertBefore(ga, s);
|
|||
</ul>
|
||||
</li>
|
||||
<li><a class="reference internal" href="#running-operations-on-change">Running Operations On Change</a></li>
|
||||
<li><a class="reference internal" href="#include-files-and-reuse">Include Files And Reuse</a></li>
|
||||
<li><a class="reference internal" href="#include-files-and-encouraging-reuse">Include Files And Encouraging Reuse</a></li>
|
||||
<li><a class="reference internal" href="#executing-a-playbook">Executing A Playbook</a></li>
|
||||
<li><a class="reference internal" href="#tips-and-tricks">Tips and Tricks</a></li>
|
||||
</ul>
|
||||
|
@ -189,11 +189,10 @@ s.parentNode.insertBefore(ga, s);
|
|||
|
||||
<div class="section" id="playbooks">
|
||||
<h1>Playbooks<a class="headerlink" href="#playbooks" title="Permalink to this headline">¶</a></h1>
|
||||
<p>Playbooks are a completely different way to use ansible and are
|
||||
particularly awesome. They are the basis for a really simple
|
||||
<p>Playbooks are a completely different way to use ansible than in task execution mode, and are
|
||||
particularly awesome. Simply put, playbooks are the basis for a really simple
|
||||
configuration management and multi-machine deployment system,
|
||||
unlike any that already exist, and
|
||||
one that is very well suited to deploying complex applications.</p>
|
||||
unlike any that already exist, and one that is very well suited to deploying complex applications.</p>
|
||||
<p>Playbooks can declare configurations, but they can also orchestrate steps of
|
||||
any manual ordered process, even as different steps must bounce back and forth
|
||||
between sets of machines in particular orders. They can launch tasks
|
||||
|
@ -205,10 +204,13 @@ remote systems are in spec.</p>
|
|||
<p>Let’s dive in and see how they work. As you go, you may wish to open
|
||||
the <a class="reference external" href="https://github.com/ansible/ansible/tree/master/examples/playbooks">github examples directory</a> in
|
||||
another tab, so you can apply the theory to what things look like in practice.</p>
|
||||
<div class="section" id="playbook-example">
|
||||
<h2>Playbook Example<a class="headerlink" href="#playbook-example" title="Permalink to this headline">¶</a></h2>
|
||||
<div class="section" id="playbook-language-example">
|
||||
<h2>Playbook Language Example<a class="headerlink" href="#playbook-language-example" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Playbooks are expressed in YAML format and have a minimum of syntax.
|
||||
Each playbook is composed of one or more ‘plays’ in a list.</p>
|
||||
<p>The goal of a play is map a group of hosts to some well defined roles, represented by
|
||||
things ansible called tasks. At the basic level, a task is nothing more than a call
|
||||
to an ansible module, which you should have learned about in earlier chapters.</p>
|
||||
<p>By composing a playbook of multiple ‘plays’, it is possible to
|
||||
orchestrate multi-machine deployments, running certain steps on all
|
||||
machines in the webservers group, then certain steps on the database
|
||||
|
@ -285,21 +287,18 @@ this does not come into play. Ansible also takes care to not log password parame
|
|||
<p>These variables can be used later in the playbook like this:</p>
|
||||
<div class="highlight-python"><pre>$varname or ${varname}</pre>
|
||||
</div>
|
||||
<p>The later is useful in the event you need to do something like ${other}_concatenated_value.</p>
|
||||
<p>The full power of the Jinja2 templating language is also available (note: in 0.4, this is only true inside of templates), which looks like this:</p>
|
||||
<p>The later is useful in the event you need to do something like ${other}_some_string.</p>
|
||||
<p>The full power of the <a class="reference external" href="http://jinja.pocoo.org/docs/">Jinja2</a> templating language is also available, which looks like this:</p>
|
||||
<div class="highlight-python"><pre>{{ varname }}</pre>
|
||||
</div>
|
||||
<p>The Jinja2 documentation provides information about how to construct loops and conditionals for those
|
||||
who which to use more advanced templating. This is optional and the $varname format still works in template
|
||||
files.</p>
|
||||
<p>If there are discovered variables about the system (ansible provides some of these,
|
||||
plus we include ones taken from facter or ohai if installed) these variables bubble up back into the
|
||||
playbook, and can be used on each system just like explicitly set
|
||||
variables.</p>
|
||||
<p>Facter variables are prefixed with <tt class="docutils literal"><span class="pre">facter_</span></tt> and Ohai
|
||||
variables are prefixed with <tt class="docutils literal"><span class="pre">ohai_</span></tt>. Ansible variables (0.3 and later)
|
||||
are not surprisingly prefixed with <tt class="docutils literal"><span class="pre">ansible_</span></tt> (See the <a class="reference internal" href="modules.html#setup"><em>setup</em></a> module
|
||||
documentation for a list of Ansible variables).</p>
|
||||
<p>If there are discovered variables about the system, called ‘facts’, these variables bubble up back into the
|
||||
playbook, and can be used on each system just like explicitly set variables. Ansible provides several
|
||||
of these, prefixed with ‘ansible’, and are documented under <a class="reference internal" href="modules.html#setup"><em>setup</em></a> in the module documentation. Additionally,
|
||||
facts can be gathered by ohai and facter if they are installed. Facter variables are prefixed with <tt class="docutils literal"><span class="pre">facter_</span></tt> and Ohai
|
||||
variables are prefixed with <tt class="docutils literal"><span class="pre">ohai_</span></tt>.</p>
|
||||
<p>So for instance, if I wanted
|
||||
to write the hostname into the /etc/motd file, I could say:</p>
|
||||
<div class="highlight-python"><pre>- name: write the motd
|
||||
|
@ -314,19 +313,22 @@ to write the hostname into the /etc/motd file, I could say:</p>
|
|||
<h3>Tasks list<a class="headerlink" href="#tasks-list" title="Permalink to this headline">¶</a></h3>
|
||||
<p>Each play contains a list of tasks. Tasks are executed in order, one
|
||||
at a time, against all machines matched by the host pattern,
|
||||
before moving on to the next task.</p>
|
||||
<p>Hosts with failed tasks are taken out of the rotation for the entire
|
||||
playbook. If things fail, simply correct the playbook file and rerun.</p>
|
||||
before moving on to the next task. It is important to understand that, within a play,
|
||||
all hosts are going to get the same task directives. It is the purpose of a play to map
|
||||
a selection of hosts to tasks.</p>
|
||||
<p>When running the playbook, which runs top to bottom, hosts with failed tasks are
|
||||
taken out of the rotation for the entire playbook. If things fail, simply correct the playbook file and rerun.</p>
|
||||
<p>The goal of each task is to execute a module, with very specific arguments.
|
||||
Variables, as mentioned above, can be used in arguments to modules.</p>
|
||||
<p>Modules other than <cite>command</cite> and <cite>shell</cite> are ‘idempotent’, meaning if you run them
|
||||
<p>Modules are ‘idempotent’, meaning if you run them
|
||||
again, they will make the changes they are told to make to bring the
|
||||
system to the desired state. This makes it very safe to rerun
|
||||
the same playbook multiple times. They won’t change things
|
||||
unless they have to change things.</p>
|
||||
<p>The <cite>command</cite> and <cite>shell</cite> modules will actually rerun the same command again,
|
||||
<p>The <cite>command</cite> and <cite>shell</cite> modules will typically rerun the same command again,
|
||||
which is totally ok if the command is something like
|
||||
‘chmod’ or ‘setsebool’, etc.</p>
|
||||
‘chmod’ or ‘setsebool’, etc. Though there is a ‘creates’ flag available which can
|
||||
be used to make these modules also idempotent.</p>
|
||||
<p>Every task should have a <cite>name</cite>, which is included in the output from
|
||||
running the playbook. This is output for humans, so it is
|
||||
nice to have reasonably good descriptions of each task step. If the name
|
||||
|
@ -345,6 +347,18 @@ them work just like you would expect. Simple:</p>
|
|||
- name: disable selinux
|
||||
action: command /sbin/setenforce 0</pre>
|
||||
</div>
|
||||
<p>The command and shell module care about return codes, so if you have a command
|
||||
who’s successful exit code is not zero, you may wish to do this:</p>
|
||||
<blockquote>
|
||||
<div><dl class="docutils">
|
||||
<dt>tasks:</dt>
|
||||
<dd><ul class="first last simple">
|
||||
<li>name: run this command and ignore the result
|
||||
action: shell /usr/bin/somecommand & /bin/true</li>
|
||||
</ul>
|
||||
</dd>
|
||||
</dl>
|
||||
</div></blockquote>
|
||||
<p>Variables can be used in action lines. Suppose you defined
|
||||
a variable called ‘vhost’ in the ‘vars’ section, you could do this:</p>
|
||||
<div class="highlight-python"><pre>tasks:
|
||||
|
@ -352,11 +366,13 @@ a variable called ‘vhost’ in the ‘vars’ section, you cou
|
|||
action: template src=somefile.j2 dest=/etc/httpd/conf.d/$vhost</pre>
|
||||
</div>
|
||||
<p>Those same variables are usable in templates, which we’ll get to later.</p>
|
||||
<p>Now in a very basic playbook all the tasks will be listed directly in that play, though it will usually
|
||||
make more sense to break up tasks using the ‘include:’ directive. We’ll show that a bit later.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="running-operations-on-change">
|
||||
<h2>Running Operations On Change<a class="headerlink" href="#running-operations-on-change" title="Permalink to this headline">¶</a></h2>
|
||||
<p>As we’ve mentioned, nearly all modules are written to be ‘idempotent’ and can relay when
|
||||
<p>As we’ve mentioned, modules are written to be ‘idempotent’ and can relay when
|
||||
they have made a change on the remote system. Playbooks recognize this and
|
||||
have a basic event system that can be used to respond to change.</p>
|
||||
<p>These ‘notify’ actions are triggered at the end of each ‘play’ in a playbook, and
|
||||
|
@ -391,11 +407,13 @@ won’t need them for much else.</p>
|
|||
<p class="last">Notify handlers are always run in the order written.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="include-files-and-reuse">
|
||||
<h2>Include Files And Reuse<a class="headerlink" href="#include-files-and-reuse" title="Permalink to this headline">¶</a></h2>
|
||||
<div class="section" id="include-files-and-encouraging-reuse">
|
||||
<h2>Include Files And Encouraging Reuse<a class="headerlink" href="#include-files-and-encouraging-reuse" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Suppose you want to reuse lists of tasks between plays or playbooks. You can use
|
||||
include files to do this.</p>
|
||||
<p>An include file simply contains a flat list of tasks, like so:</p>
|
||||
include files to do this. Use of included task lists is a great way to define a role
|
||||
that system is going to fulfill. Remember, the goal of a play in a playbook is to map
|
||||
a group of systems into multiple roles. Let’s see what this looks like...</p>
|
||||
<p>A task include file simply contains a flat list of tasks, like so:</p>
|
||||
<div class="highlight-python"><pre>---
|
||||
# possibly saved as tasks/foo.yml
|
||||
- name: placeholder foo
|
||||
|
@ -403,11 +421,11 @@ include files to do this.</p>
|
|||
- name: placeholder bar
|
||||
action: command /bin/bar</pre>
|
||||
</div>
|
||||
<p>Include directives look like this:</p>
|
||||
<p>Include directives look like this, and can be mixed in with regular tasks in a playbook:</p>
|
||||
<div class="highlight-python"><pre>- tasks:
|
||||
- include: tasks/foo.yml</pre>
|
||||
</div>
|
||||
<p>You can also pass variables into includes directly. We might call this a ‘parameterized include’.</p>
|
||||
<p>You can also pass variables into includes. We call this a ‘parameterized include’.</p>
|
||||
<p>For instance, if deploying multiple wordpress instances, I could
|
||||
contain all of my wordpress tasks in a single wordpress.yml file, and use it like so:</p>
|
||||
<div class="highlight-python"><pre>- tasks:
|
||||
|
@ -415,16 +433,16 @@ contain all of my wordpress tasks in a single wordpress.yml file, and use it lik
|
|||
- include: wordpress.yml user=alice
|
||||
- include: wordpress.yml user=bob</pre>
|
||||
</div>
|
||||
<p>Variables passed in can be used in the included files. You can reference them like this:</p>
|
||||
<p>Variables passed in can then be used in the included files. You can reference them like this:</p>
|
||||
<div class="highlight-python"><pre>$user</pre>
|
||||
</div>
|
||||
<p>In addition to the explicitly passed in parameters, all variables from
|
||||
the vars section are also available for use here as well.</p>
|
||||
<p>(In addition to the explicitly passed in parameters, all variables from
|
||||
the vars section are also available for use here as well.)</p>
|
||||
<div class="admonition note">
|
||||
<p class="first admonition-title">Note</p>
|
||||
<p class="last">Include statements are only usable from the top level
|
||||
playbook file. This means includes can not include other
|
||||
includes. This may be implemented in a later release.</p>
|
||||
<p class="last">Task include statements are only usable one-level deep.
|
||||
This means task includes can not include other
|
||||
task includes. This may change in a later release.</p>
|
||||
</div>
|
||||
<p>Includes can also be used in the ‘handlers’ section, for instance, if you
|
||||
want to define how to restart apache, you only have to do that once for all
|
||||
|
@ -440,9 +458,12 @@ of a play:</p>
|
|||
- include: handlers/handlers.yml</pre>
|
||||
</div>
|
||||
<p>You can mix in includes along with your regular non-included tasks and handlers.</p>
|
||||
<p>Note that you can not conditionally path the location to an include file, like you can
|
||||
<p>NOTE:: you can not conditionally path the location to an include file, like you can
|
||||
with ‘vars_files’. If you find yourself needing to do this, consider how you can
|
||||
restructure your playbook to be more class/role oriented.</p>
|
||||
restructure your playbook to be more class/role oriented. This is to say you cannot
|
||||
use a ‘fact’ to decide what include file to use. All hosts contained within the play
|
||||
are going to get the same tasks. (‘only_if’ provides some ability for hosts to conditionally
|
||||
skip tasks).</p>
|
||||
</div>
|
||||
<div class="section" id="executing-a-playbook">
|
||||
<h2>Executing A Playbook<a class="headerlink" href="#executing-a-playbook" title="Permalink to this headline">¶</a></h2>
|
||||
|
@ -514,7 +535,7 @@ package is installed. Try it!</p>
|
|||
</p>
|
||||
<p>
|
||||
© Copyright 2012 Michael DeHaan.<br/>
|
||||
Last updated on Jul 30, 2012.<br/>
|
||||
Last updated on Jul 31, 2012.<br/>
|
||||
</p>
|
||||
</div>
|
||||
</footer>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue