【问题标题】:ansible playbook error even for small space即使对于小空间,ansible playbook 也会出错
【发布时间】:2018-07-30 17:27:10
【问题描述】:

我刚刚说过在 Linux 环境下使用 ansible,我真的无法理解 playbook yml 中的语法

例如,我正在编写简单的剧本来安装/卸载软件包。

yum 模块第 5 行的空间很小

工作手册

---
- hosts: test,dev
  tasks:
  - name: install/uninstall package httpd
    yum: name=httpd state=removed

结果

[root@rhel7m100 krishnp]# ansible-playbook package-httpd-installation.yml --check

PLAY [test,dev] **********************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************
ok: [rhel7c1]
ok: [rhel6c2]
ok: [rhel6c1]

TASK [install/uninstall package httpd] ***********************************************************************************************
ok: [rhel6c2]
ok: [rhel6c1]
ok: [rhel7c1]

PLAY RECAP ***************************************************************************************************************************
rhel6c1                    : ok=2    changed=0    unreachable=0    failed=0
rhel6c2                    : ok=2    changed=0    unreachable=0    failed=0
rhel7c1                    : ok=2    changed=0    unreachable=0    failed=0

错误的剧本

---
- hosts: test,dev
  tasks:
  - name: install/uninstall package httpd
     yum: name=httpd state=removed        ---> the only difference is the small
                                               space in from of yum command

结果

[root@rhel7m100 krishnp]# ansible-playbook package-httpd-installation.yml --check
ERROR! Syntax Error while loading YAML.


The error appears to have been in '/krishnp/package-httpd-installation.yml': line 5, column 9, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  - name: install/uninstall package httpd
     yum: name=httpd state=removed
        ^ here

enter code hereexception type: <class 'yaml.scanner.ScannerError'>
exception: mapping values are not allowed in this context
  in "<unicode string>", line 5, column 9

小空间会给 playbook 运行带来麻烦真的很重要吗?

【问题讨论】:

  • YAML sintax 的主要功能之一是使其易于阅读,标识非常重要,如果您不尊重它,您的剧本就会出现问题......另一个重要建议是使用空格,而不是制表符。

标签: ansible


【解决方案1】:

小空间会给 playbook 运行带来麻烦真的很重要吗?

是的。剧本写在YAML。而且 YAML 对缩进非常严格,因为你通常会使用不同的缩进来“切换上下文”。

【讨论】:

    【解决方案2】:

    是的,YAML 中的空格很重要,因为它严格使用空格字符作为缩进

    这里还有一些 YAML 语法的基本规则。

    YAML 基础规则

    • 适用的 YAML 文件:所有带有 .yml 扩展名的文件。
    • 不允许使用制表符,只能使用空格。
    • 您的代码必须缩进 1 个或多个空格。
    • 所有键/属性都区分大小写。

    【讨论】:

      猜你喜欢
      • 2022-11-27
      • 2020-11-15
      • 2014-04-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      • 2018-02-10
      • 2016-03-29
      • 1970-01-01
      相关资源
      最近更新 更多