centos 检查Ansible是否存在服务

ac1kyiln  于 2022-11-07  发布在  其他
关注(0)|答案(9)|浏览(190)

我有一个Ansible剧本,用于将Java应用程序部署为init.d守护进程。
作为Ansible和Linux的初学者,我很难根据主机的状态在主机上有条件地执行任务。
也就是说,我有一些主机已经有了服务,我想在做其他事情之前停止它。然后可能有新的主机,它们还没有服务。所以我不能简单地使用service: name={{service_name}} state=stopped,因为这在新的主机上会失败。
我如何才能做到这一点呢?以下是我目前所掌握的:

- name: Check if Service Exists
    shell: "if chkconfig --list | grep -q my_service;   then echo true;   else echo false; fi;"
    register: service_exists

# This should only execute on hosts where the service is present

  - name: Stop Service
    service: name={{service_name}} state=stopped
    when: service_exists
    register: service_stopped

# This too

  - name: Remove Old App Folder
    command: rm -rf {{app_target_folder}}
    when: service_exists

# This should be executed on all hosts, but only after the service has stopped, if it was present

  - name: Unpack App Archive
    unarchive: src=../target/{{app_tar_name}} dest=/opt
rqdpfwrv

rqdpfwrv1#

请参阅Ansible 2.5中新增的service_facts模块。

- name: Populate service facts
  service_facts:
- debug:
    msg: Docker installed!
  when: "'docker' in services"
1u4esq0p

1u4esq0p2#

当然,我也可以检查 Package 器脚本是否存在于/etc/init. d中。

- name: Check if Service Exists
    stat: path=/etc/init.d/{{service_name}}
    register: service_status

  - name: Stop Service
    service: name={{service_name}} state=stopped
    when: service_status.stat.exists
    register: service_stopped
eblbsuwk

eblbsuwk3#

我修改了Florian's answer,只使用service命令的返回代码(这在Mint 18.2上有效)

- name: Check if Logstash service exist
  shell: service logstash status 
  register: logstash_status
  failed_when: not(logstash_status.rc == 3 or logstash_status.rc == 0)

- name: Check if Logstash service exist
  service:
    name: logstash
    state: stopped 
  when: logstash_status.rc == 0
kiayqfof

kiayqfof4#

如果“服务”模块能够处理“无法识别的服务”错误,那就太好了。
这是我的方法,使用service命令而不是检查init脚本:

- name: check for apache
  shell: "service apache2 status"
  register: _svc_apache
  failed_when: >
    _svc_apache.rc != 0 and ("unrecognized service" not in _svc_apache.stderr)

- name: disable apache
  service: name=apache2 state=stopped enabled=no
  when: "_svc_apache.rc == 0"
  • 检查“服务状态”的退出代码,并在输出包含“未识别的服务”时接受退出代码0
  • 如果退出代码为0,则该服务已安装(已停止或正在运行)
azpvetkf

azpvetkf5#

systemd(from Jakuje)的另一种方法:

- name: Check if cups-browsed service exists
  command: systemctl cat cups-browsed
  check_mode: no
  register: cups_browsed_exists
  changed_when: False
  failed_when: cups_browsed_exists.rc not in [0, 1]

- name: Stop cups-browsed service
  systemd:
    name: cups-browsed
    state: stopped 
  when: cups_browsed_exists.rc == 0
kiayqfof

kiayqfof6#

基于@Maciej对RedHat 8的回答,并将其与对它的评论相结合。
这就是我如何设法停止Celery只有当它已经安装:

- name: Populate service facts
  service_facts:
- debug:
    msg: httpd installed!
  when: ansible_facts.services['httpd.service'] is defined

- name: Stop celery.service
  service:
    name: celery.service
    state: stopped
    enabled: true
  when: ansible_facts.services['celery.service'] is defined

您可以删除调试语句--它只是用来确认ansible_factors是否正常工作。

fjaof16o

fjaof16o7#

这种仅使用服务模块的方式对我们很有效:

- name: Disable *service_name*
  service:
    name: *service_name*
    enabled: no
    state: stopped
  register: service_command_output
  failed_when: >
    service_command_output|failed
    and 'unrecognized service' not in service_command_output.msg
    and 'Could not find the requested service' not in service_command_output.msg
4bbkushb

4bbkushb8#

我的几分钱。与上面相同的方法,但对kubernetes

检查kublete服务是否正在运行

- name: "Obtain state of kublet service"
  command: systemctl status kubelet.service
  register: kubelet_status
  failed_when: kubelet_status.rc > 3

如果kublet服务未运行,则显示调试消息

- debug:
    msg: "{{ kubelet_status.stdout }}"
  when: "'running' not in kubelet_status.stdout"
mccptt67

mccptt679#

从Ansible 2.5开始,您可以使用service_facts模块。但是您需要知道输出是服务的真实名称,如docker.servicesomthing@.service。因此,您有不同的选项,如:

- name: Populate service facts
  service_facts:
- debug:
    msg: Docker installed!
  when: "'docker.service' in services"

或者,您可以搜索以服务名称开头的字符串;这要可靠得多,因为服务名称在不同的发行版之间是不同的:

- name: Populate service facts
  service_facts:
- debug:
    msg: Docker installed!
  when: "services.keys()|list|select('search', '^docker')|length >0"

相关问题