我希望从 /etc/passwd 中提取用户列表,然后 grep 他们的 crontab 文件以查找禁用的(注释:^#)作业。
高级步骤是:
- 从 /etc/passwd ("my_users") 中获取一组用户名
- 针对步骤 1 中命名的文件运行 grep (/var/spool/cron/{{my_users}})
- 使用“调试”打印出结果。
我将不胜感激任何建议,这是我到目前为止所拥有的:
- name: ANSIBLE PLAYBOOK - disabled cronjob check
hosts: "{{ variable_host | default('testvm') }}"
remote_user: admin
gather_facts: no
become: yes
tasks:
- getent:
database: passwd
- name: set_fact
debugger: on_failed
ansible.builtin.set_fact:
my_users: "{{ getent_passwd|dict2items|json_query('[?contains(value,`/bin/bash`)].key') }}"
cacheable: yes
- name: set_fact_2
ansible.builtin.set_fact:
array_length: "{{ my_users|length }}"
- debug:
msg: "Debugging 2: {{ my_users|length }}"
- name: Get disabled cron jobs
debugger: always
loop: "{{ my_users }}"
ansible.builtin.lineinfile:
path: "/var/spool/cron/{{ my_users }}"
regexp: "^#"
这是有问题的输出,这不是我所期望的:
任务 [获取禁用的 cron 作业]********************************************* ****************************************************** ****************************************************** *************************************** 失败:[testvm] (item=n2disk) => {" ansible_loop_var": "item", "changed": false, "item": "n2disk", "msg": "line is required with state=present"} failed: [testvm] (item=cento) => {"ansible_loop_var “:“项目”,“已更改”:false,“项目”:“cento”,“msg”:“state = present需要行”}失败:[testvm](item = admin)=> {“ansible_loop_var” : "item", "changed": false, "item": "admin", "msg": "line is required with state=present"} 失败:[testvm] (item=nprobe) => {"ansible_loop_var": "item", "changed": false, "item": "nprobe", "msg": "line is required with state=present"}失败:[testvm] (item=root) => {"ansible_loop_var": "item", "changed": false, "item": "root", "msg": "line is required with state=present"} 失败: [testvm] (item=backup) => {"ansible_loop_var": "item", "changed": false, "item": "backup", "msg": "line is required with state=present"}state=present 需要行”} failed: [testvm] (item=root) => {"ansible_loop_var": "item", "changed": false, "item": "root", "msg": "line需要 state=present"} failed: [testvm] (item=backup) => {"ansible_loop_var": "item", "changed": false, "item": "backup", "msg": "line is需要 state=present"}state=present 需要行”} failed: [testvm] (item=root) => {"ansible_loop_var": "item", "changed": false, "item": "root", "msg": "line需要 state=present"} failed: [testvm] (item=backup) => {"ansible_loop_var": "item", "changed": false, "item": "backup", "msg": "line is需要 state=present"}msg": "line is required with state=present"}msg": "line is required with state=present"}
作为提示,请参阅下面如何创建包含所有 crontab 信息的字典
例如,给定远程主机 test_11 和 test_13 上的 crontab
剧本
给
使用getent读取/etc/passwd