在以下主机上运行时,Ansible剧本错误:主机:

时间:2020-06-24 15:21:59

标签: ansible

在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

2 个答案:

答案 0 :(得分:0)

首先,您应该能够对本地主机执行SSH。 您可以尝试

ssh user@localhost date

您可以创建一个主机文件并将其命名为hosts并向其中添加以下内容。

[localhost]
localhost

[localhost:vars]
ansible_ssh_user=user
ansible_ssh_pass=pass
ansible_sudo_pass=sudopass

并以以下方式运行剧本

ansible-playbook -i hosts main.yml

答案 1 :(得分:0)

使用命令模块能够停止和启动服务,使用sudo service ssh stopsudo service ssh start可以达到我的目的。 服务模块无法做到这一点,仍然对此一无所知