I'm looking to extract a list of users from /etc/passwd and then grep their crontab files for disabled (commented: ^#) jobs.
The high level steps would be:
- Grab an array of usernames from /etc/passwd ("my_users")
- Run grep against the files named in step 1 (/var/spool/cron/{{my_users}})
- Use 'Debug' to print out the results.
I'd appreciate any suggestions, here's what I have so far:
- 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: "^#"
Here's the output in question, which isn't quite what I expected:
TASK [Get disabled cron jobs]*********************************************************************************************************************************************************************************** failed: [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":
"item", "changed": false, "item": "cento", "msg": "line is required
with state=present"}
failed: [testvm] (item=admin) =>
{"ansible_loop_var": "item", "changed": false, "item": "admin", "msg":
"line is required with state=present"}
failed: [testvm] (item=nprobe)
=> {"ansible_loop_var": "item", "changed": false, "item": "nprobe", "msg": "line is required with state=present"}
failed: [testvm]
(item=root) => {"ansible_loop_var": "item", "changed": false, "item":
"root", "msg": "line is required with state=present"}
failed: [testvm]
(item=backup) => {"ansible_loop_var": "item", "changed": false,
"item": "backup", "msg": "line is required with state=present"}