具有角色的Ansible剧本的语法错误
下面是我的playbook目录结构具有角色的Ansible剧本的语法错误,ansible,Ansible,下面是我的playbook目录结构 /home/ansible/playbooks/ ├── first.yml --> simple playbook with no roles works fine. ├── playbook.yml --> main playbook with roles └── roles └── webservers ├── default │ └── main.yml ├── handler
/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'
- name: installed_httpd
service:
- name: httpd
state: restarted
- name: installed_apache
service:
- name: apache2
state: restarted
My/roles/webserver/handlers/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'
- 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
错误!作用中的意外参数类型:
错误似乎出现在“/home/ansible/playbooks/roles/webserver/tasks/main.yml”中:第1行第3列,但可能是
根据确切的语法问题,在文件中的其他位置。
令人不快的一行似乎是:
-名称:在redhat上安装httpd
^这里
我检查了所有与此错误相关的问题,但似乎都与不正确的语法或不正确的缩进有关
我已经在我的文件中检查了相同的内容,如果我在没有角色的情况下运行playbook,并且在同一playbook中定义了所有任务和处理程序,那么所有内容看起来都很好,并且工作正常
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
所有这些YAML文件都包含有效的YAML,但是从它们加载的数据当然可能不是Ansible所期望的(也就是说,除非文件中有制表符)。不过,我确实建议在YAML的格式设置上更加一致,以便能够更容易地发现任何错误。现在序列有了不同的缩进(元素前面有2个位置,散列没有缩进,散列有4个位置,偏移量为2:选择一个并坚持它)。