YAMLSyntax.html 9.79 KB
Newer Older
Tim Bielawa committed
1 2 3 4 5 6 7 8

<!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" />
    
9
    <title>YAML Syntax &mdash; Ansible - SSH-Based Configuration Management &amp; Deployment</title>
Tim Bielawa committed
10 11
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
12 13
    <link rel="stylesheet" href="_static/bootstrap.css" type="text/css" />
    <link rel="stylesheet" href="_static/bootstrap-sphinx.css" type="text/css" />
Tim Bielawa committed
14 15 16
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
17
        VERSION:     '0.01',
Tim Bielawa committed
18 19
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
Tim Bielawa committed
20
        HAS_SOURCE:  false
Tim Bielawa committed
21 22 23 24 25
      };
    </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>
26 27
    <script type="text/javascript" src="_static/bootstrap-dropdown.js"></script>
    <script type="text/javascript" src="_static/bootstrap-scrollspy.js"></script>
28
    <link rel="top" title="Ansible - SSH-Based Configuration Management &amp; Deployment" href="index.html" />
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    <link rel="next" title="Playbooks" href="playbooks.html" />
    <link rel="prev" title="Ansible Modules" href="modules.html" />
<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();
  });
}());
</script>
Michael DeHaan committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
<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);
 })();

</script>
101

Tim Bielawa committed
102 103
  </head>
  <body>
104 105 106 107 108 109 110 111
  <div class="topbar" data-scrollspy="scrollspy" >
    <div class="topbar-inner">
      <div class="container">
        <a class="brand" href="index.html">Ansible</a>
        <ul class="nav">
          
            <li class="dropdown" data-dropdown="dropdown">
  <a href="index.html"
112
     class="dropdown-toggle">Chapter</a>
113
  <span class="globaltoc"><ul class="current">
114
<li class="toctree-l1"><a class="reference internal" href="gettingstarted.html">Downloads &amp; Getting Started</a></li>
115 116 117
<li class="toctree-l1"><a class="reference internal" href="patterns.html">The Inventory File, Patterns, and Groups</a></li>
<li class="toctree-l1"><a class="reference internal" href="examples.html">Command Line Examples</a></li>
<li class="toctree-l1"><a class="reference internal" href="modules.html">Ansible Modules</a></li>
118
<li class="toctree-l1 current"><a class="current reference internal" href="">YAML Syntax</a></li>
119
<li class="toctree-l1"><a class="reference internal" href="playbooks.html">Playbooks</a></li>
120
<li class="toctree-l1"><a class="reference internal" href="api.html">API &amp; Integrations</a></li>
121
<li class="toctree-l1"><a class="reference internal" href="moduledev.html">Module Development Guide</a></li>
122
<li class="toctree-l1"><a class="reference internal" href="faq.html">Frequently Asked Questions</a></li>
123 124 125 126
<li class="toctree-l1"><a class="reference internal" href="man.html">Man Pages</a></li>
</ul>
</span>
</li>
127 128 129 130 131 132 133 134 135 136 137
            <li class="dropdown" data-dropdown="dropdown">
  <a href="#"
     class="dropdown-toggle">Page</a>
  <span class="localtoc"><ul>
<li><a class="reference internal" href="#">YAML Syntax</a><ul>
<li><a class="reference internal" href="#yaml-basics">YAML Basics</a></li>
</ul>
</li>
</ul>
</span>
</li>
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
          
        </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>

154

155 156
<div class="container">
   
157 158
  <div class="section" id="yaml-syntax">
<h1>YAML Syntax<a class="headerlink" href="#yaml-syntax" title="Permalink to this headline"></a></h1>
Michael DeHaan committed
159
<p>This page provides a basic overview of correct YAML syntax, which is how Ansible
160 161 162 163
playbooks (our configuration management language) are expressed.</p>
<p>We use YAML because it is easier to read and write for humans than other common
data formats like XML or JSON.  Further, there are libraries available for reading
and writing YAML in most programming languages.</p>
Michael DeHaan committed
164
<p>You may also wish to read <a class="reference internal" href="playbooks.html"><em>Playbooks</em></a> at the same time to see how this
165
is used in practice.</p>
Tim Bielawa committed
166 167
<div class="section" id="yaml-basics">
<h2>YAML Basics<a class="headerlink" href="#yaml-basics" title="Permalink to this headline"></a></h2>
168 169
<p>For <cite>ansible</cite>, nearly every YAML file starts with a list.
Each item in the list is a list of key/value pairs, commonly
170 171 172 173 174 175 176
called a &#8220;hash&#8221; or a &#8220;dictionary&#8221;.  So, we need to know how
to write lists and dictionaries in YAML.</p>
<p>There&#8217;s another small quirk to YAML.  All YAML files (regardless of their association with
<cite>ansible</cite> or not) should start with <tt class="docutils literal"><span class="pre">---</span></tt>.  This is just a YAML
format thing that means &#8220;this is the start of a document&#8221;.</p>
<p>All members of a list are lines beginning at the same indentation level starting
with a <tt class="docutils literal"><span class="pre">-</span></tt> (dash) character:</p>
Tim Bielawa committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190
<div class="highlight-python"><pre>---
# A list of tasty fruits
- Apple
- Orange
- Strawberry
- Mango</pre>
</div>
<p>A dictionary is represented in a simple <tt class="docutils literal"><span class="pre">key:</span></tt> and <tt class="docutils literal"><span class="pre">value</span></tt> form:</p>
<div class="highlight-python"><pre>---
# An employee record
name: John Eckersberg
job: Developer
skill: Elite</pre>
</div>
191
<p>Dictionaries can also be represented in an abbreviated form if you really want to:</p>
Tim Bielawa committed
192 193 194 195
<div class="highlight-python"><pre>---
# An employee record
{name: John Eckersberg, job: Developer, skill: Elite}</pre>
</div>
196 197
<p id="truthiness">Ansible doesn&#8217;t really use these too much, but you can also specify a
boolean value (true/false) in several forms:</p>
Tim Bielawa committed
198 199 200 201 202
<div class="highlight-python"><pre>---
knows_oop: True
likes_emacs: TRUE
uses_cvs: false</pre>
</div>
203 204
<p>Let&#8217;s combine what we learned so far in an arbitary YAML example.  This really
has nothing to do with Ansible, but will give you a feel for the format:</p>
Tim Bielawa committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
<div class="highlight-python"><pre>---
# An employee record
name: John Eckersberg
job: Developer
skill: Elite
employed: True
foods:
    - Apple
    - Orange
    - Strawberry
    - Mango
languages:
    ruby: Elite
    python: Elite
    dotnet: Lame</pre>
</div>
<p>That&#8217;s all you really need to know about YAML to get started writing
Michael DeHaan committed
222
<cite>Ansible</cite> playbooks.</p>
Tim Bielawa committed
223 224 225
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
226 227
<dt><a class="reference internal" href="playbooks.html"><em>Playbooks</em></a></dt>
<dd>Learn what playbooks can do and how to write/run them.</dd>
Tim Bielawa committed
228
<dt><a class="reference external" href="http://yamllint.com/">YAMLLint</a></dt>
229
<dd>YAML Lint (online) helps you debug YAML syntax if you are having problems</dd>
230 231
<dt><a class="reference external" href="https://github.com/ansible/ansible/tree/master/examples/playbooks">Github examples directory</a></dt>
<dd>Complete playbook files from the github project source</dd>
232 233 234 235
<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>
Tim Bielawa committed
236 237 238 239 240 241 242
</dl>
</div>
</div>
</div>


</div>
243 244 245 246 247
<footer class="footer">
  <div class="container">
    <p class="pull-right"><a href="#">Back to top</a></p>
    <p>
        &copy; Copyright 2012 Michael DeHaan.<br/>
Michael DeHaan committed
248
      Last updated on Mar 30, 2012.<br/>
249
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.8.<br/>
250 251 252
    </p>
  </div>
</footer>
Tim Bielawa committed
253 254
  </body>
</html>