下面是我的剧本目录结构。
/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/