Use filter community.general.jc. It's a wrapper of the Python jc converter. For example, given the crontab
shell> ssh admin@test_11 cat /etc/crontab
# /etc/crontab - root's crontab for FreeBSD
#
# $FreeBSD$
#
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
#
#minute hour mday month wday who command
#
# Save some entropy so that /dev/random can re-seed on boot.
*/11 * * * * operator /usr/libexec/save-entropy
#
# Rotate log files every hour, if necessary.
0 * * * * root newsyslog
#
# Perform daily/weekly/monthly maintenance.
1 3 * * * root periodic daily
15 4 * * 6 root periodic weekly
30 5 1 * * root periodic monthly
#
# Adjust the time zone if the CMOS clock keeps local time, as opposed to
# UTC time. See adjkerntz(8) for details.
1,31 0-5 * * * root adjkerntz -a
The playbook below
shell> cat pb.yml
- hosts: test_11,test_12
tasks:
- command:
cmd: cat /etc/crontab
register: result
- set_fact:
crontab: "{{ result.stdout|community.general.jc('crontab_u') }}"
- copy:
dest: /tmp/crontabs.json
content: |
{{ crontabs_dict|to_json }}
vars:
crontabs_list: "{{ ansible_play_hosts|map('extract', hostvars, 'crontab')|list }}"
crontabs_dict: "{{ dict(ansible_play_hosts|zip(crontabs_list)) }}"
delegate_to: localhost
run_once: true
creates the file at the controller
shell> cat /tmp/crontabs.json
{"test_11": {"variables": [{"name": "PATH", "value": "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"}, {"name": "SHELL", "value": "/bin/sh"}], "schedule": [{"minute": ["/11"], "hour": [""], "day_of_month": [""], "month": [""], "day_of_week": [""], "user": "operator", "command": "/usr/libexec/save-entropy"}, {"minute": ["0"], "hour": [""], "day_of_month": [""], "month": [""], "day_of_week": [""], "user": "root", "command": "newsyslog"}, {"minute": ["1"], "hour": ["3"], "day_of_month": [""], "month": [""], "day_of_week": [""], "user": "root", "command": "periodic daily"}, {"minute": ["15"], "hour": ["4"], "day_of_month": [""], "month": [""], "day_of_week": ["6"], "user": "root", "command": "periodic weekly"}, {"minute": ["30"], "hour": ["5"], "day_of_month": ["1"], "month": [""], "day_of_week": [""], "user": "root", "command": "periodic monthly"}, {"minute": ["1", "31"], "hour": ["0-5"], "day_of_month": [""], "month": [""], "day_of_week": [""], "user": "root", "command": "adjkerntz -a"}]}, "test_12": {"variables": [{"name": "PATH", "value": "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"}, {"name": "SHELL", "value": "/bin/sh"}], "schedule": [{"minute": ["/11"], "hour": [""], "day_of_month": [""], "month": [""], "day_of_week": [""], "user": "operator", "command": "/usr/libexec/save-entropy"}, {"minute": ["0"], "hour": [""], "day_of_month": [""], "month": [""], "day_of_week": [""], "user": "root", "command": "newsyslog"}, {"minute": ["1"], "hour": ["3"], "day_of_month": [""], "month": [""], "day_of_week": [""], "user": "root", "command": "periodic daily"}, {"minute": ["15"], "hour": ["4"], "day_of_month": [""], "month": [""], "day_of_week": ["6"], "user": "root", "command": "periodic weekly"}, {"minute": ["30"], "hour": ["5"], "day_of_month": ["1"], "month": [""], "day_of_week": [""], "user": "root", "command": "periodic monthly"}, {"minute": ["1", "31"], "hour": ["0-5"], "day_of_month": [""], "month": [""], "day_of_week": [""], "user": "root", "command": "adjkerntz -a"}]}}
The variable crontabs_dict in YAML
crontabs_dict:
test_11:
schedule:
- command: /usr/libexec/save-entropy
day_of_month: ['*']
day_of_week: ['*']
hour: ['*']
minute: ['*/11']
month: ['*']
user: operator
- command: newsyslog
day_of_month: ['*']
day_of_week: ['*']
hour: ['*']
minute: ['0']
month: ['*']
user: root
- command: periodic daily
day_of_month: ['*']
day_of_week: ['*']
hour: ['3']
minute: ['1']
month: ['*']
user: root
- command: periodic weekly
day_of_month: ['*']
day_of_week: ['6']
hour: ['4']
minute: ['15']
month: ['*']
user: root
- command: periodic monthly
day_of_month: ['1']
day_of_week: ['*']
hour: ['5']
minute: ['30']
month: ['*']
user: root
- command: adjkerntz -a
day_of_month: ['*']
day_of_week: ['*']
hour: [0-5]
minute: ['1', '31']
month: ['*']
user: root
variables:
- {name: PATH, value: '/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin'}
- {name: SHELL, value: /bin/sh}
test_12:
schedule:
- command: /usr/libexec/save-entropy
day_of_month: ['*']
day_of_week: ['*']
hour: ['*']
minute: ['*/11']
month: ['*']
user: operator
- command: newsyslog
day_of_month: ['*']
day_of_week: ['*']
hour: ['*']
minute: ['0']
month: ['*']
user: root
- command: periodic daily
day_of_month: ['*']
day_of_week: ['*']
hour: ['3']
minute: ['1']
month: ['*']
user: root
- command: periodic weekly
day_of_month: ['*']
day_of_week: ['6']
hour: ['4']
minute: ['15']
month: ['*']
user: root
- command: periodic monthly
day_of_month: ['1']
day_of_week: ['*']
hour: ['5']
minute: ['30']
month: ['*']
user: root
- command: adjkerntz -a
day_of_month: ['*']
day_of_week: ['*']
hour: [0-5]
minute: ['1', '31']
month: ['*']
user: root
variables:
- {name: PATH, value: '/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin'}
- {name: SHELL, value: /bin/sh}