首页 > 解决方案 > 在主机上运行时出现 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

标签: ansible

解决方案


使用命令模块能够停止和启动服务,使用sudo service ssh stopsudo service ssh start服务于我的目的。无法使用服务模块这样做,仍然不知道


推荐阅读