疑惑:如何禁用但运行服务? [英] Ansible: How to get disabled but running services?
问题描述
我想遍历许多主机,并获取未先启用(Systemctl Enable Xxxx)而手动启动(Systemctl Start Xxxx)的所有服务,反之亦然。所有已禁用(systemctl禁用xxxx)但仍在运行(因为尚未停止)的服务。
我只希望将它们输出到我的shell中,而不是自动"修复"上面发现的所有问题。
在systemctl list-unit-files --state=disabled
上执行For Each循环并通过systemctl is-active
运行每一行可能会起作用,但我需要向每个主机传输一个小SH脚本.
仅靠Ansible能做到这一点吗?(将RHEL7 Update 7与内核3.10.x配合使用)
编辑:到目前为止,这是我的剧本,但我无法使循环工作:
---
- hosts: all
gather_facts: no
tasks:
- name: get service facts
service_facts:
- name: show report
when:
- ansible_facts.services[item + '.service'].state == 'running'
- ansible_facts.services[item + '.service'].status == 'disabled'
debug:
msg: "{{ ansible_facts.services[item + '.service'].status == 'disabled' }}"
loop:
ansible_facts.services
推荐答案
关于您的问题
这是否可以仅由Ansible完成?
简短的答案是:是的,当然。这就是Ansible应运而生的原因。
关于您的标题
如何禁用但正在运行服务?
和评论
您是否尝试过ansible.builtin.service_facts
我准备了一个简短的测试,它将显示ansible_facts.services
的结构。
---
- hosts: localhost
become: no
gather_facts: no
tasks:
- name: Gather Service Facts
service_facts:
- name: Show Service Facts
debug:
msg: "{{ ansible_facts.services }}"
例如,对于其中一项服务,该值大于
auditd.service:
name: auditd.service
source: systemd
state: running
status: enabled
因此您需要循环结果集并查找ansible_facts.services[YOURSERVICE].state
和ansible_facts.services[YOURSERVICE].status
。
关于您的描述
..。已禁用(systemctl禁用xxxx)但仍在运行(因为尚未停止)的服务.
您可以查看how to declare a variable for service_facts
的答案。
关于您的评论
..。如何为所有服务执行WHEN条件.
和前面提到的结果循环,我为测试设置添加了
- name: Loop over all services and print name
debug:
msg: "{{ item }}"
when:
- ansible_facts.services[item].state == 'running'
- ansible_facts.services[item].status != 'enabled'
with_items: "{{ ansible_facts.services }}"
并且发现它工作正常。
您可能需要调整条件,因为该查询也将报告状态为static
LIKEdbus
或systemd-journald
的服务。另请注意,服务也可以具有unknow
状态。在示例中
splunk.service:
name: splunk.service
source: systemd
state: running
status: unknown
或
systemctl status splunk
● splunk.service - SYSV: Splunk indexer service
Loaded: loaded (/etc/rc.d/init.d/splunk; bad; vendor preset: disabled)
Active: active (running) ...
这篇关于疑惑:如何禁用但运行服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!