【发布时间】:2020-06-19 09:51:22
【问题描述】:
问题:
此场景用于解释 Ansible 中模块的使用。
为此,您必须停止并启动名为
ssh的服务。要完成的任务:- 在 fresco_module\tasks 文件夹中的 main.yml 文件中编写一个任务。
任务是使用 Ansible 中的服务模块停止和启动名为 ssh 的服务。
注意:
- 运行 project install 安装 ansible.mainplaybook.yml 文件提供给 ansible-playbook。
- 使用本地主机作为 ansible-playbook 的清单。
我的代码:
- hosts: localhost
become: yes
tasks:
- name: Stop and Start ssh
service:
name: ssh
state: "{{ item }}"
with_items:
- stopped
- started
输出:
PLAY [localhost] *******************************************************************************
TASK [Gathering Facts] *************************************************************************
[DEPRECATION WARNING]: Distribution Ubuntu 16.04 on host localhost should use /usr/bin/python3,
but is using /usr/bin/python for backward compatibility with prior Ansible releases. A future
Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more
information. This feature will be removed in version 2.12. Deprecation warnings can be disabled
by setting deprecation_warnings=False in ansible.cfg.
ok: [localhost]
TASK [Stop and Start ssh] **********************************************************************
changed: [localhost] => (item=stopped)
ok: [localhost] => (item=started)
PLAY RECAP *************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
问题:服务在 Ansible 停止后已经在运行,看起来 sshd 从一开始就没有停止过。
用于检查状态的命令:service ssh status。我也将此命令与state:stopped 一起使用,但sshd 仍在运行。我已经面对这个问题很久了。我也试过state:restarted。
【问题讨论】:
-
好吧,现在你失去了我。你最终的目标是什么?因为您在问题中所做的以及我在回答中的建议将导致重新启动并因此运行 ssh 守护进程。这不就是你需要的结果吗?
-
获取此代码:-主机:localhost 变为:是任务:-名称:停止并启动 ssh 服务:名称:ssh 状态:已停止并使用“服务 ssh 状态”检查状态。它将显示 sshd 正在运行而不是停止。同样在舞台上重新启动它不会停止启动服务。
-
嗨 Vivek Gaur,如果您已经清除了停止和启动 ssh 服务的权限。请协助我清除handson。因为我也面临您指定的相同问题。
-
参考我的 YouTube 视频youtu.be/dlwSit9K1lk