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

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

问题描述

我习惯将Ansible示例视为:

I'm used to see Ansible examples as:

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

但是有人在工作时告诉我,我应该仅使用YAML语法保持一致:

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

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

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

哪个满足Ansible最佳做法?

Which one satisfies Ansible best practices?

推荐答案

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

Ansible剧本运行程序的核心是一个YAML解析器,具有添加的逻辑(例如,命令行键=值对速记).虽然编制快速剧本或文档示例很方便,但是这种格式设置会降低可读性.我们建议您最好不要使用该速记(即使使用YAML折叠样式)也是一种最佳做法.

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.

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

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

现在这是使用本机YAML语法的相同任务:

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

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

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天全站免登陆