Difference between revisions of "Introduction to YAML"
(→Some examples: file written in YAML) |
|||
(3 intermediate revisions by the same user not shown) | |||
Line 17: | Line 17: | ||
::: name: 'Name B' | ::: name: 'Name B' | ||
::: id: '98765432' | ::: id: '98765432' | ||
+ | = Some examples: file written in YAML = | ||
+ | == YAML File and Python == | ||
+ | File name: students.yaml | ||
+ | <pre> | ||
+ | students: | ||
+ | - name: David | ||
+ | id_number: 012345678 | ||
+ | program: CTY | ||
+ | - name: Raymond | ||
+ | id_number: 112001987 | ||
+ | program: CNS | ||
+ | </pre> | ||
+ | The above file contains data equivalent to the following dictionary data object in Python: | ||
+ | <pre> | ||
+ | {'students': [{'id_number': '012345678', 'name': 'David', 'program': 'CTY'}, | ||
+ | {'id_number': 112001987, 'name': 'Raymond', 'program': 'CNS'}]} | ||
+ | </pre> | ||
+ | The contents of students.yaml can be created by the following python code: | ||
+ | <pre> | ||
+ | #!/usr/bin/env python3 | ||
+ | import yaml | ||
+ | |||
+ | students = {'students': [{'name': 'David', 'id_number': '012345678', 'program': 'CTY'}, | ||
+ | {'name': 'Raymond', 'id_number': 112001987, 'program': 'CNS'}]} | ||
+ | |||
+ | print(yaml.dump(students)) | ||
+ | </pre> | ||
+ | == Ansible playbooks == | ||
+ | File Name: get_os_version.yaml | ||
+ | <pre> | ||
+ | --- | ||
+ | - hosts: all | ||
+ | remote_user: rchan | ||
+ | tasks: | ||
+ | - name: Print Linux distribution and version | ||
+ | debug: | ||
+ | msg: '{{ ansible_distribution }} {{ ansible_distribution_version }}' | ||
+ | </pre> | ||
+ | The file above contains data equivalent to the following data object in Python: | ||
+ | <pre> | ||
+ | [{'hosts': 'all', 'remote_user': 'rchan', 'tasks': [{'debug': {'msg': '{{ ansible_distribution }} | ||
+ | {{ ansible_distribution_version }}'}, 'name': 'Print Linux distribution and version'}]}] | ||
+ | </pre> |
Latest revision as of 00:02, 25 November 2019
Contents
What is YAML?
- YAML stands for "YAML Ain’t markup language"
- It is a human-readable data serialization language that is used in Ansible to package output and its playbooks
What are the major components in YAML?
- declarations: name: 'yum'
- lists:
- - 'item_a'
- - 'item_b'
- - 'item_c'
- associative arrays:
- item_a:
- name: 'Name A'
- id: '12345678'
- item_b:
- name: 'Name B'
- id: '98765432'
- item_a:
Some examples: file written in YAML
YAML File and Python
File name: students.yaml
students: - name: David id_number: 012345678 program: CTY - name: Raymond id_number: 112001987 program: CNS
The above file contains data equivalent to the following dictionary data object in Python:
{'students': [{'id_number': '012345678', 'name': 'David', 'program': 'CTY'}, {'id_number': 112001987, 'name': 'Raymond', 'program': 'CNS'}]}
The contents of students.yaml can be created by the following python code:
#!/usr/bin/env python3 import yaml students = {'students': [{'name': 'David', 'id_number': '012345678', 'program': 'CTY'}, {'name': 'Raymond', 'id_number': 112001987, 'program': 'CNS'}]} print(yaml.dump(students))
Ansible playbooks
File Name: get_os_version.yaml
--- - hosts: all remote_user: rchan tasks: - name: Print Linux distribution and version debug: msg: '{{ ansible_distribution }} {{ ansible_distribution_version }}'
The file above contains data equivalent to the following data object in Python:
[{'hosts': 'all', 'remote_user': 'rchan', 'tasks': [{'debug': {'msg': '{{ ansible_distribution }} {{ ansible_distribution_version }}'}, 'name': 'Print Linux distribution and version'}]}]