具有角色的Ansible剧本的语法错误

时间:2018-11-25 08:25:37

标签: 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

执行剧本时,出现以下错误:

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

1 个答案:

答案 0 :(得分:4)

模块属性的声明中存在语法错误。代替

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

正确的语法是

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