{"id":28619,"date":"2022-03-12T13:10:08","date_gmt":"2022-03-12T13:10:08","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=28619"},"modified":"2022-12-23T06:47:51","modified_gmt":"2022-12-23T06:47:51","slug":"what-is-yaml-and-how-it-works-an-overview-and-its-use-cases","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/what-is-yaml-and-how-it-works-an-overview-and-its-use-cases\/","title":{"rendered":"What is YAML and How it works? An Overview and Its Use Cases"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/03\/yaml.png\" alt=\"\" class=\"wp-image-28621\" width=\"548\" height=\"344\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">What is YAML?<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>YAML means\u00a0<strong> another markup language<\/strong>. It is a data serialization language that works well with the modern programming language, and it is human-friendly.<\/li><li>It is used to define the\u00a0data<strong> structures<\/strong>\u00a0that are very easy to understand. These data structures are very easy to manage and maintain by the users.<\/li><li>YAML uses\u00a0<strong>space<\/strong>\u00a0to define something. If we use a single space or double space, it has different meanings in YAML. Spaces change the meaning of data structure.<\/li><li>YAML is\u00a0<strong>case sensitive.<\/strong><\/li><li>YAML is\u00a0<strong>language independent.<\/strong>\u00a0If we define the YAML once, the same YAML file can be invoked in Python file, Ruby, etc.<\/li><li>In the starting, YAML was very useful and friendly to people who work with data. YAML uses\u00a0<strong>Unicode printable characters<\/strong>. In Unicode, some provide the information of structure, and others contain the data itself. Using the minimization of structural characters, a unique cleanness can be achieved by YAML. YAML is used to show the data itself in a meaningful and natural way. For example, bullet lists are created by dashes, key: value pairs are separated by a colon, and structure may be used by indentation.<\/li><li>To work well with the\u00a0<strong>common use cases,<\/strong>\u00a0YAML language was specialty designed. Common-use cases are cross-language data sharing, log files, debugging of complex data structure, interprocess messaging, object persistence, and configuration files. The programming becomes an easy task when data is easy to understand and view.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How To Write YAML<\/h2>\n\n\n\n<p>The basic structure of a YAML file is a map. You might call this a dictionary, hash or object, depending on your programming language or mood.<\/p>\n\n\n\n<p>Very generally, it\u2019s keys and values all the way down:<a><\/a><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTTP\" data-shcb-language-slug=\"http\"><span><code class=\"hljs language-http\"><span class=\"hljs-attribute\">key<\/span>: value\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTTP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">http<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">YAML Example: Scalar Types<\/h3>\n\n\n\n<p>You can use all sorts of scalar types as values: numbers, booleans, and strings (quoted or not). The first line of a&nbsp;<code>config.yml<\/code>, for example, is usually:<a><\/a><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTTP\" data-shcb-language-slug=\"http\"><span><code class=\"hljs language-http\"><span class=\"hljs-attribute\">version<\/span>: 2\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTTP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">http<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Words in keys can be separated by underscores, dashes, or spaces. At CircleCI, we&nbsp;<a href=\"https:\/\/circleci.com\/docs\/2.0\/configuration-reference\/#example-full-configuration\" target=\"_blank\" rel=\"noopener\">use underscores<\/a>.<\/p>\n\n\n\n<p>If the value of a key is a multi-line string, you can use either the \u2018literal block\u2019 style using the \u2018|\u2019 character. This is particularly helpful when defining shell commands:<a><\/a><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">command<\/span>: \n    <span class=\"hljs-selector-tag\">if<\/span> <span class=\"hljs-selector-attr\">&#91; <span class=\"hljs-string\">\"${CIRCLE_BRANCH}\"<\/span> == <span class=\"hljs-string\">\"master\"<\/span> ]<\/span>;\n      <span class=\"hljs-selector-tag\">then<\/span> <span class=\"hljs-selector-tag\">ansible-playbook<\/span> <span class=\"hljs-selector-tag\">site<\/span><span class=\"hljs-selector-class\">.yml<\/span> <span class=\"hljs-selector-tag\">-i<\/span> <span class=\"hljs-selector-tag\">production<\/span>;\n    <span class=\"hljs-selector-tag\">fi<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that the leading indentation for the multi-line string will be stripped.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">YAML Example: Collection Types<\/h3>\n\n\n\n<p>All you have to do to create collections is Use Indentation:<a><\/a><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">environment:\n    TEST_REPORTS: <span class=\"hljs-regexp\">\/tmp\/<\/span>test-reports\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you have a list of things (like images), you can denote that sequence using dashes:<a><\/a><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">docker<\/span>:\n    <span class=\"hljs-selector-tag\">-<\/span> <span class=\"hljs-selector-tag\">image<\/span>: <span class=\"hljs-selector-tag\">ubuntu<\/span><span class=\"hljs-selector-pseudo\">:14.04<\/span>\n    <span class=\"hljs-selector-tag\">-<\/span> <span class=\"hljs-selector-tag\">image<\/span>: <span class=\"hljs-selector-tag\">mongo<\/span><span class=\"hljs-selector-pseudo\">:2.6.8<\/span>\n      <span class=\"hljs-selector-tag\">command<\/span>: <span class=\"hljs-selector-attr\">&#91;mongod, --smallfiles]<\/span>\n    <span class=\"hljs-selector-tag\">-<\/span> <span class=\"hljs-selector-tag\">image<\/span>: <span class=\"hljs-selector-tag\">postgres<\/span><span class=\"hljs-selector-pseudo\">:9.4.1<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that the second item in the sequence has two keys:&nbsp;<code>image<\/code>&nbsp;and&nbsp;<code>command<\/code>. The&nbsp;<code>command<\/code>&nbsp;key uses a JSON-style sequence because (remember!) YAML is a superset of JSON.<\/p>\n\n\n\n<p>Finally, YAML doesn\u2019t allow tab characters, so if you\u2019re using those to indent, have your text editor convert those to spaces. Syntax errors in your YAML can sometimes cause CircleCI builds to hang, but they\u2019re also easily preventable by running your\u00a0<code>circle.yml\/config.yml<\/code>\u00a0through an\u00a0online validator.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-file-extension-for-yaml\">What is the file extension for YAML<\/h2>\n\n\n\n<p><code>yaml<\/code>&nbsp;files are created with extension&nbsp;<code>.yaml<\/code>&nbsp;or&nbsp;<code>.yml<\/code>. You can use any IDE or text editor to open this file.<\/p>\n\n\n\n<p>All the popular IDE supports the\u00a0<code>yaml<\/code>\u00a0file extension and also provides plugins to validate the\u00a0<code>yaml<\/code>\u00a0file content.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\"  id=\"_ytid_72749\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/GOk4IoYhM9U?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  epyt-is-override  no-lazyload\" title=\"YouTube player\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of YAML<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Files are\u00a0easy to work in a text editor.<\/li><li>Files are portable between programming languages.<\/li><li>Files are expressive\u00a0and also extensible.<\/li><li>Files support the Unicode character set.<\/li><li>Support for major programming languages.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"disadvantages-of-yaml\">Disadvantages of YAML<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>It is a new format introduced recently, a learning curve exists.<\/li><li>Not much popular other than XML and JSON.<\/li><li>It is very complex to represent configuration in the hierarchy of data.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this guide, we covered the steps of preparing the development environment in both Windows as well as Linux to get started with YAML. We nearly discussed all the concepts of YAML\u2019s basic data types, YAML editor, and YAML Parser.<\/p>\n\n\n\n<p>We have also highlighted the benefits of using YAML vis-a-vis other markup languages and provided code examples with the help of a supporting sample project. We hope that now the learners can use YAML to abstract data from application logic to write efficient and maintainable code.<\/p>\n\n\n\n<p><em><strong>Happy Learning!!<\/strong><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is YAML? YAML means\u00a0 another markup language. It is a data serialization language that works well with the modern programming language, and it is human-friendly. It is used to&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[2],"tags":[95,5449,1122,545,766,177,637,213],"class_list":["post-28619","post","type-post","status-publish","format-standard","hentry","category-uncategorised","tag-devops","tag-devopsschool","tag-guide","tag-how","tag-interview","tag-php","tag-top","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/28619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=28619"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/28619\/revisions"}],"predecessor-version":[{"id":28624,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/28619\/revisions\/28624"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=28619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=28619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=28619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}