ansible yaml basics

h‮w//:sptt‬ww.theitroad.com

Ansible uses YAML (Yet Another Markup Language) for defining tasks and playbooks. YAML is a human-readable data serialization language that is easy to understand and write.

Here are some YAML basics that you need to know to work with Ansible:

  1. YAML syntax: YAML uses a simple syntax of key-value pairs and lists. Key-value pairs are represented as key: value and are separated by a colon followed by a space. Lists are represented using hyphens (-) followed by a space.

  2. Indentation: YAML uses indentation to define the hierarchy of data. All items with the same indentation level are considered to be at the same level in the hierarchy.

  3. Comments: Comments in YAML start with the # character and continue until the end of the line.

  4. Quoting: Strings in YAML can be single- or double-quoted. If a string contains a special character, it should be enclosed in quotes.

Here's an example of YAML syntax in Ansible:

- name: Install nginx
  apt:
    name: nginx
    state: present
  become: yes

In this example, we define a task to install the nginx package using the apt module. The name and state parameters are passed as key-value pairs. The become parameter is set to yes to run the task with sudo privileges.

Note the use of indentation to define the hierarchy of data. The task name is at the top level, followed by the apt module and its parameters. The become parameter is defined at the same level as the apt module, indicating that it applies to the entire task.