【问题标题】:Ansible module to stop and start `ssh` serviceAnsible 模块停止和启动 `ssh` 服务
【发布时间】: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

标签: ansible ansible-inventory


【解决方案1】:

您好 Vivek,欢迎加入社区!

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

以下代码应该可以工作:

- hosts: localhost
  become: yes
  tasks:
  - name: Stop and Start ssh
    service:
      name: ssh
      state: restarted

这样 Ansible 可以确保 ssh 服务停止并启动 - 简而言之:重新启动。你甚至不需要with_items 循环。

【讨论】:

  • 我试过这个,但是服务没有停止,后来又启动了。它一直在运行。
  • 请不要误会我的意思:但这就是你想要的,不是吗?如果您希望服务停止,您只需执行类似state: stopped 的停止。还是我在这里遗漏了什么?
  • 如果我使用 state:stopped 那么服务也不会停止。它继续运行。我只是希望服务先停止,然后再启动。
  • 如果我用命令替换服务模块: service ssh restart 然后,它停止,然后,开始。这件事不能用 service:name:ssh state:restarted.
  • 好的,所以我们同意重启按预期工作,但是停止然后启动似乎不能正常工作,对吗?你能检查一下 sshd 日志,在 Ansible 运行期间服务到底发生了什么?帮我一个忙并配置您的清单,以便您的 localhost 使用本地连接而不是 ssh 并再次检查。
【解决方案2】:

我尝试了以下方法并获得了相同的“sshd running”输出,但我认为这里的问题是他们希望我们在一项任务下停止和启动。我们也不允许使用重启状态:/

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


-

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

【讨论】:

    【解决方案3】:
    ---
    - 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 作为停止和启动服务。

    【讨论】:

      【解决方案4】:

      即使我尝试使用许多其他模块(如 systemd)来解决这个问题,我仍然无法停止服务。但是使用命令模块并通过'sudo service ssh stop',我能够停止服务。但仍然没有通过问题。 甚至之前尝试过felix的答案,仍然无法通过。

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

      【讨论】:

      • 我也有同样的问题。我尝试使用命令然后它停止,否则不会。我尝试了很多解决方案,但没有一个有效。
      • 兄弟你能帮我解决这个问题吗,Ansible Choral | 5 | Ansible 角色问题。如果你已经这样做了。
      【解决方案5】:

      只需运行 playbook 即可停止启动 ssh 服务而无需重新启动。 或者使用这个

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

      成功运行 playbook 后。只需通过以下方式停止服务 sudo serivce ssh stop 然后启动服务sudo service ssh start。 然后只需提交测试。你会通过动手操作

      【讨论】:

      • 嗨 Carlito_15,根据您的指示,我已在 main.yml 中粘贴了以下代码 - 名称:“停止 ssh”服务:名称:ssh 状态:停止 - 名称:“启动 ssh”服务:名称: ssh state: 启动之后我已经成功运行了剧本,然后在终端中执行了 sudo service ssh stop 命令,然后我执行了 sudo service ssh start 命令,然后提交了测试。但是我还没有通过handson。如果我遗漏了什么或做错了什么,请建议我,并帮助我清理handson。
      【解决方案6】:

      只需在您的主 yaml 文件中写入以下命令。这将首先停止 ssh 服务,然后重新启动它。

      - name: Stop service ssh, if started
        ansible.builtin.service:
          name: ssh
          state: stopped
      
      - name: Start service ssh, if not started
        ansible.builtin.service:
          name: ssh
          state: started
      

      【讨论】:

        【解决方案7】:

        在 AWS EC2 实例上,ssh 服务是 sshd

        - name: restart ssh daemon
          hosts: all
          remote_user: ec2-user
          become: yes
          become_method: sudo
          tasks:
          - name: Stop and Start ssh
            service:
              name: sshd
              state: restarted
        

        在上述 YAML 中,将 sshd 替换为 ssh 将失败为 "msg": "Could not find the requested service ssh

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-22
          • 1970-01-01
          • 1970-01-01
          • 2023-04-02
          相关资源
          最近更新 更多