【问题标题】:Syntax error for Ansible playbook with roles带有角色的 Ansible 剧本的语法错误
【发布时间】:2018-11-25 08:25:36
【问题描述】:

下面是我的剧本目录结构。

/home/ansible/playbooks/
├── first.yml --> simple playbook with no roles works fine.
├── playbook.yml  --> main playbook with roles
└── roles
    └── webservers
        ├── default
        │   └── main.yml
        ├── handler
        │   └── main.yml
        ├── tasks
        │   └── main.yml
        └── vars
            └── main.yml

我的主要剧本代码如下:

--- #playbook with roles

- hosts: webserver
  user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: yes
  roles:
    - webservers

我的 /roles/webserver/tasks/main.yml 代码:

- name: Install httpd on redhat
  yum:
    - pkg: httpd
      state: latest
  notify: installed_httpd
  when: ansible_os_family == 'RedHat'

- name: Install apache2 on debian
  apt:
    - pkg: apache2
      state: latest
  notify: installed_apache
  when: ansible_os_family == 'Debian'

我的 /roles/webserver/handlers/main.yml 代码:

- name: installed_httpd
  service:
  - name: httpd
    state: restarted

- name: installed_apache
  service:
  - name: apache2
    state: restarted

当我执行 playbook 时,出现以下错误:

ERROR! unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>

The error appears to have been in '/home/ansible/playbooks/roles/webservers/tasks/main.yml': line 1, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- name: Install httpd on redhat
  ^ here

我检查了与此错误相关的所有问题,但似乎都与不正确的语法或不正确的缩进有关。

我已经在我的文件中检查了相同的内容,如果我在没有角色的情况下运行 playbook,并且所有任务和处理程序都在同一个 playbook 中定义,那么一切似乎都很好,并且也可以正常工作。

Ansible 版本:2.7.2 python2版本:2.7.5 python3版本:3.7.1

【问题讨论】:

  • 所有这些 YAML 文件都包含有效的 YAML,但是从它们加载的数据当然可能不是 Ansible 所期望的(也就是说,除非您的文件中有 TAB 字符)。我确实建议您在 YAML 格式中更加一致,以便能够更轻松地发现任何错误。您现在对序列有不同的缩进(元素前有 2 个位置,哈希没有缩进,哈希有 4 个偏移量为 2:选择一个并坚持下去)。

标签: ansible


【解决方案1】:

模块属性的声明中存在语法错误。而不是

- name: Install httpd on redhat
  yum:
    - pkg: httpd

正确的语法是

- name: Install httpd on redhat
  yum:
    pkg: httpd

【讨论】:

    猜你喜欢
    • 2016-01-20
    • 2015-11-13
    • 1970-01-01
    • 2019-08-22
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 2016-04-13
    • 1970-01-01
    相关资源
    最近更新 更多