Ansible 语法最佳实践,YAML 字典(键:值)还是等号(键=值)? [英] Ansible syntax best practice, YAML dictionary (key: value) or equal sign (key=value)?

查看:18
本文介绍了Ansible 语法最佳实践,YAML 字典(键:值)还是等号(键=值)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯将 Ansible 示例视为:

- 文件:path=/tmp/file state=touch

但工作中有人告诉我,我应该只使用像这样的 YAML 语法来保持一致:

- 文件:路径:/tmp/文件状态:触摸

或者,

- file: {path:/tmp/file, state:touch}

哪一个满足 Ansible 最佳实践?

解决方案

摘自 https://www.ansible.com/blog/ansible-best-practices-essentials

<块引用>

就其核心而言,Ansible playbook runner 是一个 YAML 解析器,具有附加的逻辑,例如命令行键=值对速记.虽然在制作快速手册或文档示例时很方便,但这种格式会降低可读性.我们建议您避免使用这种速记(即使使用 YAML 折叠样式)作为最佳做法.

以下是使用 key=value 简写的一些任务的示例:

- 名称:安装电报嗯:名称:telegraf-{{telegraf_version }} state=present update_cache=yes disable_gpg_check=yes enablerepo=telegraf通知:重新启动电报- 名称:配置电报模板:src=telegraf.conf.j2 dest=/etc/telegraf/telegraf.conf通知:重新启动电报- 名称:启动电报服务:名称=电报状态=启动启用=是

现在这里是使用原生 YAML 语法的相同任务:

- 名称:安装电报百胜:telegraf-{{telegraf_version }}状态:现在update_cache: 是disable_gpg_check: 是启用仓库:电报通知:重新启动电报- 名称:配置电报模板:源代码:telegraf.conf.j2目标:/etc/telegraf/telegraf.conf通知:重新启动电报- 名称:启动电报服务:名称:电报状态:开始启用:是

<块引用>

原生YAML有更多行;然而,这些行更短,减少了水平滚动和换行.它让眼睛直接向下扫描.任务参数堆叠在一起,很容易与下一个参数区分开来.本机 YAML 语法还具有在几乎所有现代文本编辑器中改进语法突出显示的好处.作为本机 YAML,vim 和 Atom 等编辑器将从其值中突出显示 YAML 键(模块名称、指令、参数名称),进一步提高内容的可读性.我们自己的许多文档都出于遗留原因使用这种速记,尽管我们正在逐步改变它.(接受文档拉取请求.)

I'm used to see Ansible examples as:

- file: path=/tmp/file state=touch

but someone at work told me that I should be consistent using only YAML syntax like this:

- file:
    path: /tmp/file
    state: touch

or,

- file: {path: /tmp/file, state:touch}

Which one satisfies Ansible best practices?

解决方案

Taken from https://www.ansible.com/blog/ansible-best-practices-essentials

At its core, the Ansible playbook runner is a YAML parser with added logic such as commandline key=value pairs shorthand. While convenient when cranking out a quick playbook or a docs example, that style of formatting reduces readability. We recommend you refrain from using that shorthand (even with YAML folded style) as a best practice.

Here is an example of some tasks using the key=value shorthand:

- name: install telegraf
  yum:
    name: telegraf-{{ telegraf_version }} state=present update_cache=yes disable_gpg_check=yes enablerepo=telegraf
  notify: restart telegraf

- name: configure telegraf
  template: src=telegraf.conf.j2 dest=/etc/telegraf/telegraf.conf
  notify: restart telegraf

- name: start telegraf
  service: name=telegraf state=started enabled=yes

Now here is the same tasks using native YAML syntax:

- name: install telegraf
  yum: telegraf-{{ telegraf_version }}
    state: present
    update_cache: yes
    disable_gpg_check: yes
    enablerepo: telegraf
  notify: restart telegraf

- name: configure telegraf
  template:
    src: telegraf.conf.j2
    dest: /etc/telegraf/telegraf.conf
  notify: restart telegraf

- name: start telegraf
  service:
    name: telegraf
    state: started
    enabled: yes

Native YAML has more lines; however, those lines are shorter, reducing horizontal scrolling and line wrapping. It lets the eyes scan straight down the play. The task parameters are stacked and easily distinguished from the next. Native YAML syntax also has the benefit of improved syntax highlighting in virtually any modern text editor out there. Being native YAML, editors such as vim and Atom will highlight YAML keys (module names, directives, parameter names) from their values further aiding the readability of your content. Many of our own docs use this shorthand for legacy reasons though we’re progressively changing that. (Documentation pull requests accepted.)

这篇关于Ansible 语法最佳实践,YAML 字典(键:值)还是等号(键=值)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆