YAMLSyntax.html 12.1 KB
Newer Older
Tim Bielawa committed
1 2 3 4

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

5

Tim Bielawa committed
6 7 8 9
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
10
    <title>YAML Syntax &mdash; Ansible - SSH-Based Configuration Management &amp; Deployment</title>
11
    
Tim Bielawa committed
12 13
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
14 15
    <link rel="stylesheet" href="_static/bootstrap.css" type="text/css" />
    <link rel="stylesheet" href="_static/bootstrap-sphinx.css" type="text/css" />
16
    
Tim Bielawa committed
17 18 19
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
20
        VERSION:     '0.01',
Tim Bielawa committed
21 22
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
Tim Bielawa committed
23
        HAS_SOURCE:  false
Tim Bielawa committed
24 25 26 27 28
      };
    </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>
29 30
    <script type="text/javascript" src="_static/bootstrap-dropdown.js"></script>
    <script type="text/javascript" src="_static/bootstrap-scrollspy.js"></script>
Michael DeHaan committed
31
    <link rel="shortcut icon" href="_static/favicon.ico"/>
32
    <link rel="top" title="Ansible - SSH-Based Configuration Management &amp; Deployment" href="index.html" />
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 85 86 87 88
    <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>
89

Michael DeHaan committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
<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>
106 107 108 109 110 111 112 113 114

<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>

115 116 117 118 119 120 121
<script>(function(d, s, id) {
  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>
122

Tim Bielawa committed
123 124
  </head>
  <body>
125 126 127
  <div class="topbar" data-scrollspy="scrollspy" >
    <div class="topbar-inner">
      <div class="container">
Michael DeHaan committed
128
        <!-- <a class="brand" href="index.html">Ansible</a> -->
129 130 131 132
        <ul class="nav">
          
            <li class="dropdown" data-dropdown="dropdown">
  <a href="index.html"
133
     class="dropdown-toggle">Chapter</a>
134
  <span class="globaltoc"><ul class="current">
135 136
<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 &amp; Patterns</a></li>
Michael DeHaan committed
137
<li class="toctree-l1"><a class="reference internal" href="examples.html">Command Line Examples And Next Steps</a></li>
138
<li class="toctree-l1"><a class="reference internal" href="modules.html">Ansible Modules</a></li>
139
<li class="toctree-l1 current"><a class="current reference internal" href="">YAML Syntax</a></li>
140
<li class="toctree-l1"><a class="reference internal" href="playbooks.html">Playbooks</a></li>
141
<li class="toctree-l1"><a class="reference internal" href="playbooks2.html">Advanced Playbooks</a></li>
142
<li class="toctree-l1"><a class="reference internal" href="bestpractices.html">Best Practices</a></li>
143
<li class="toctree-l1"><a class="reference internal" href="api.html">API &amp; Integrations</a></li>
144
<li class="toctree-l1"><a class="reference internal" href="moduledev.html">Module Development</a></li>
145
<li class="toctree-l1"><a class="reference internal" href="faq.html">FAQ</a></li>
146
<li class="toctree-l1"><a class="reference internal" href="who_uses_ansible.html">Who Uses Ansible</a></li>
147 148 149
</ul>
</span>
</li>
150 151 152 153 154 155 156 157 158 159 160
            <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>
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
          
        </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>
176
<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>
177

178

179
<div class="container">
180
   <a href="http://ansible.github.com"><img src="http://ansible.github.com/ansible-logo.png" alt="Ansible"/></a><br/>
181
<br/>
182
   
183 184
  <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
185
<p>This page provides a basic overview of correct YAML syntax, which is how Ansible
186 187 188 189
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
190
<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
191
is used in practice.</p>
Tim Bielawa committed
192 193
<div class="section" id="yaml-basics">
<h2>YAML Basics<a class="headerlink" href="#yaml-basics" title="Permalink to this headline"></a></h2>
194 195
<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
196 197 198 199 200 201 202
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
203 204 205 206 207 208 209 210 211 212 213 214 215 216
<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>
217
<p>Dictionaries can also be represented in an abbreviated form if you really want to:</p>
Tim Bielawa committed
218 219 220 221
<div class="highlight-python"><pre>---
# An employee record
{name: John Eckersberg, job: Developer, skill: Elite}</pre>
</div>
222 223
<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
224 225 226 227 228
<div class="highlight-python"><pre>---
knows_oop: True
likes_emacs: TRUE
uses_cvs: false</pre>
</div>
229 230
<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
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
<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
248
<cite>Ansible</cite> playbooks.</p>
Tim Bielawa committed
249 250 251
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
252 253
<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
254
<dt><a class="reference external" href="http://yamllint.com/">YAMLLint</a></dt>
255
<dd>YAML Lint (online) helps you debug YAML syntax if you are having problems</dd>
256
<dt><a class="reference external" href="https://github.com/ansible/ansible/tree/devel/examples/playbooks">Github examples directory</a></dt>
257
<dd>Complete playbook files from the github project source</dd>
258 259 260 261
<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
262 263 264 265 266 267
</dl>
</div>
</div>
</div>


268
<br/>
Tim Bielawa committed
269
</div>
270 271 272 273
<footer class="footer">

  <div class="container">
  <div id="fb-root"></div>
Michael DeHaan committed
274
<p>
Michael DeHaan committed
275 276 277 278 279 280 281 282 283 284 285 286 287
<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>
288 289 290 291
<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>
Michael DeHaan committed
292 293
</p>
<p>
294
        &copy; Copyright 2012 Michael DeHaan.<br/>
295
      Last updated on Aug 30, 2012.<br/>
296 297 298
    </p>
  </div>
</footer>
Tim Bielawa committed
299 300
  </body>
</html>