ansible - 具有角色的 Ansible playbook 的语法错误

标签 ansible

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

/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

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

我在我的文件中检查了相同的内容,如果我在没有角色的情况下运行剧本,并且在同一剧本中定义了所有任务和处理程序,那么一切看起来都很好,并且也没有错误。

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

最佳答案

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

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

正确的语法是

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

关于ansible - 具有角色的 Ansible playbook 的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53465818/

相关文章:

ansible - “dict对象”没有属性 'stdout_lines'

ansible - 最后修改的文件只移动了ansible?

ubuntu - 用于 Vagrant 的 Ansible 配置器中的 $PATH 环境变量

python - 如何为 Ansible 剧本选择 Python 解释器?

regex - 如何使用ansible更改文件中的特定值

ansible - 将共享参数与 Ansible 中的环境特定参数合并

SSH 进入私有(private)主机 Ansible

amazon-web-services - 认证或权限失败,对远程目录没有权限

node.js - 解析 block 映射时未找到预期的键

linux - 如何从命令行重新加载配置 Jenkins?