ansible - 在主机上运行时出现 Ansible playbook 错误:
问题描述
在 main.yml 中编写一个任务,以使用 ansible 中的服务模块停止和启动服务“ssh”中的服务。
---
- hosts: localhost
become: true
become_method: sudo
tasks:
- name: stop service
service:
name: ssh
state: stopped
- name: start service
service:
name: ssh
state: started
运行时出现以下错误
[WARNING]: Unable to parse /projects/challenge/localhost as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
ERROR! unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>
The error appears to be in '/projects/challenge/fresco_module/tasks/main.yml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be
---
- hosts: localhost
^ here
解决方案
使用命令模块能够停止和启动服务,使用sudo service ssh stop
并sudo service ssh start
服务于我的目的。无法使用服务模块这样做,仍然不知道