在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
首先,您应该能够对本地主机进行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
使用命令模块能够停止和启动服务,使用sudo service ssh stop
和sudo service ssh start
达到了我的目的。 无法使用服务模块执行此操作,仍然不知道
通过使用 .yml 文件的完整路径在我的最后解决
Ansible-playbook -i/etc/ansible/hosts myfirstplaybook.yml