在我的剧本中,我有一个任务来更新audit.rules,然后通知一个应该重启auditd服务的处理程序。
task:
- name: 6.6.7 - audit rules configuration
template: src=X/ansible/templates/auditd_rules.j2
dest=/etc/audit/rules.d/audit.rules
backup=yes
owner=root group=root mode=0640
notify:
- restart auditd
handlers:
- name: restart auditd
service: name=auditd state=restarted
当playbook运行时,审计规则会更新,并且会请求重新启动auditd,但这会失败,如下所示。
RUNNING HANDLER [restart auditd] ***********************************************
fatal: [ipX-southeast-2.compute.internal]: FAILED! => {"changed": false, "failed": true, "msg": "Unable to restart service auditd: Failed to restart auditd.service: Operation refused, unit auditd.service may be requested by dependency only.\n"}
当我查看auditd的单位定义时,我可以看到refuseManualStop = yes。这是为什么我不能重新启动服务?如何通过这个来获取新的审计规则?
systemctl cat auditd.service
# /usr/lib/systemd/system/auditd.service
[Unit]
Description=Security Auditing Service
DefaultDependencies=no
After=local-fs.target systemd-tmpfiles-setup.service
Conflicts=shutdown.target
Before=sysinit.target shutdown.target
RefuseManualStop=yes
ConditionKernelCommandLine=!audit=0
Documentation=man:auditd(8) https://people.redhat.com/sgrubb/audit/
[Service]
ExecStart=/sbin/auditd -n
## To not use augenrules, copy this file to /etc/systemd/system/auditd.service
## and comment/delete the next line and uncomment the auditctl line.
## NOTE: augenrules expect any rules to be added to /etc/audit/rules.d/
ExecStartPost=-/sbin/augenrules --load
#ExecStartPost=-/sbin/auditctl -R /etc/audit/audit.rules
ExecReload=/bin/kill -HUP $MAINPID
# By default we don't clear the rules on exit. To enable this, uncomment
# the next line after copying the file to /etc/systemd/system/auditd.service
#ExecStopPost=/sbin/auditctl -R /etc/audit/audit-stop.rules
[Install]
WantedBy=multi-user.target
将手动停止更改为NO并尝试
sudo服务auditd restart
如果这样可行,那么代码也可以工作
systemctl启动审计
和
systemctl启用auditd
适用于CentOS版本7。请点击链接获取进一步的帮助。
这已在Red Hat Bugzilla #1026648和Anisble Issue # 22171 (github)报告中进行了探索,讨论和解决(主要)。
解析度
service
模块参数use=service
强制执行/sbin/service
实用程序而不是systemd
(调用/sbin/systemctl
)的collect-fact值,如下所示:
- service: name=auditd state=restarted use=service
解决方法:
command
模块显式运行服务可执行文件,如下所示:
- command: /sbin/service auditd restart
分析 - 根本原因:
- 这是auditd.service unit的上游包装创建的问题。当
systemctl
采取行动时,它不会启动/停止/重新启动,显然是by design。- Ansible服务控制功能进一步复杂化,它使用收集系统事实时识别的首选方法,并且“ansible_service_mgr”返回“systemd”。这与用于管理service.unit的实际模块无关。
- 如果在即将发布的更新中被视为问题,RHEL开发团队可能会修复(ERRATA)
- Ansible开发团队提供了一种解决方法,并且(从2.2开始)使用
service
参数更新了use
模块。
我会正确验证auditd服务重新加载,因为即使使用命令模块,您指定的命令也不会按照您期望的方式工作或运行;
通过确认
service auditd status
试试吧
service auditd condrestart
:)
也许是因为答案迟了,但如果其他人遇到同样的问题,你可以用这个命令导入auditd
的新规则:
auditctl -R /path/to_your_rules_file
因此,无需重新启动auditd.service
来导入新规则
您不应该更改参数refuseManualStop,它是为了保证您的系统安全。在创建新规则后,您可以做什么,重新启动主机,等待它,然后继续使用Playbook。
手册示例:
- name: Create new rules file
copy:
src: 01-personalized.rules
dest: /etc/audit/rules.d/01-personalized.rules
owner: root
group: root
mode: 0600
register: result
- name: Reboot server
shell: "sleep 5 && reboot"
async: 1
poll: 0
when: result is changed
- name: Wait for server to become available
wait_for_connection:
delay: 60
sleep: 5
timeout: 300
when: result is changed