Ansible模块停止和启动`ssh`服务

时间:2020-06-19 09:51:22

标签: ansible ansible-inventory

问题:

此方案用于解释Ansible中模块的用法。

为此,您必须停止并启动名为ssh的服务。

要完成的任务:-在fresco_module \ tasks文件夹中的main.yml文件中编写任务。

任务是使用Ansible中的服务模块停止和启动名为ssh的服务。

注意:

  • 运行项目安装以将ansible.mainplaybook.yml文件提供给ansible-playbook。
  • 将localhost用于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

6 个答案:

答案 0 :(得分:0)

您好Vivek,欢迎来到社区!

这应该很容易。您可以告诉Ansible直接重新启动服务,而无需通过两个单独的步骤来停止和启动它。

以下代码应该起作用:

deltaxSize, deltaySize, c = 0, 0, 0
        self.buttons = []
        for k, obj_class in enumerate(all_classes):
            self.buttons.append(wx.Button(self.panel, label=f'{obj_class}', pos=(50 + deltaxSize, 20 + deltaySize),
                                       size=(100, 20)))
            self.Bind(wx.EVT_BUTTON, lambda event: print(f'{obj_class}\n'), self.buttons[k])
            deltaySize += 20
            c += 1
            if c == 30:
                deltaxSize += 100
                deltaySize, c = 0, 0

Ansible通过这种方式确保ssh服务已停止启动-简而言之:重新启动。您甚至不需要- hosts: localhost become: yes tasks: - name: Stop and Start ssh service: name: ssh state: restarted 循环。

答案 1 :(得分:0)

我有相同的查询。我做了同样的事情,但是没有这样的输出。它总是显示sshd正在运行。这就是为什么这个不完整的原因。

答案 2 :(得分:0)

我已经尝试了以下内容,并获得相同的“ sshd running”输出,但是我认为这里的问题是他们希望我们在一项任务下既停止又开始。另外,我们也不允许使用重新启动状态:/

-
  name: "Stop ssh"
  service:
    name: ssh
    state: stopped


-

  name: "start ssh"
  service:
    name: ssh
    state: started

答案 3 :(得分:0)

即使我尝试使用许多其他模块(例如systemd)来解决此问题,我仍然无法停止该服务。但是使用命令模块,并传递“ sudo service ssh stop”,我能够停止该服务。但仍然没有解决问题。 即使以前尝试过菲利克斯的答案,仍然无法通过。

如果有人可以帮助我解决“ Ansible Choral | 5 | Ansible Roles问题” 会很好。即使在获得100%fs分数后仍无法通过该问题。

答案 4 :(得分:0)

只需运行该剧本即可停止启动ssh服务而无需重新启动。 或使用

-
  name: "Stop ssh"
  service:
    name: ssh
    state: stopped


-

  name: "start ssh"
  service:
    name: ssh
    state: started

成功运行剧本后。只要停止服务 sudo serivce ssh stop,然后启动服务sudo service ssh start。 然后只需提交测试即可。您将通过动手操作

答案 5 :(得分:0)

---
- hosts: localhost
  connection: local
  become: true
  become_method: sudo
  tasks:
    - name: stop a service
      service:
        name: ssh
        state: stopped
    - name: start a service
      service:
        name: ssh
        state: started

添加 become-method sudo 和 task 作为停止和启动服务。