Skip to content

Commit 1362dbb

Browse files
authored
facts.crontab: match full crontab(5) env var syntax (#1678)
1 parent 071b2f7 commit 1362dbb

3 files changed

Lines changed: 82 additions & 1 deletion

File tree

src/pyinfra/facts/crontab.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,21 @@ def to_json(self):
102102
return self.commands
103103

104104

105-
_crontab_env_re = re.compile(r"^\s*([A-Z_]+)=(.*)$")
105+
# crontab(5) env line: ``name = value`` with optional spaces around ``=``. The
106+
# name may be a bare identifier or placed in matching single/double quotes; the
107+
# value runs to end-of-line (the caller handles any quoting in the value).
108+
_crontab_env_re = re.compile(
109+
r"""
110+
^\s*
111+
(?:
112+
"[^"]*" # "quoted name"
113+
| '[^']*' # 'quoted name'
114+
| [A-Za-z_][A-Za-z0-9_]* # bare identifier
115+
)
116+
\s*=
117+
""",
118+
re.VERBOSE,
119+
)
106120

107121

108122
class Crontab(FactBase[CrontabFile]):
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"command": "crontab -l || true",
3+
"requires_command": "crontab",
4+
"output": [
5+
"MAILTO=me@example.com",
6+
"CRON_TZ=Europe/Amsterdam",
7+
"@reboot echo rebooted",
8+
"0 0 * * * apt update"
9+
],
10+
"fact": [
11+
{
12+
"env": "MAILTO=me@example.com",
13+
"comments": []
14+
},
15+
{
16+
"env": "CRON_TZ=Europe/Amsterdam",
17+
"comments": []
18+
},
19+
{
20+
"command": "echo rebooted",
21+
"special_time": "@reboot",
22+
"comments": []
23+
},
24+
{
25+
"command": "apt update",
26+
"minute": 0,
27+
"hour": 0,
28+
"month": "*",
29+
"day_of_month": "*",
30+
"day_of_week": "*",
31+
"comments": []
32+
}
33+
]
34+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"command": "crontab -l || true",
3+
"requires_command": "crontab",
4+
"output": [
5+
"MAILTO = me@example.com",
6+
"cron_tz = Europe/Amsterdam",
7+
"SHELL=/bin/bash",
8+
"0 * * * * echo hi"
9+
],
10+
"fact": [
11+
{
12+
"env": "MAILTO = me@example.com",
13+
"comments": []
14+
},
15+
{
16+
"env": "cron_tz = Europe/Amsterdam",
17+
"comments": []
18+
},
19+
{
20+
"env": "SHELL=/bin/bash",
21+
"comments": []
22+
},
23+
{
24+
"command": "echo hi",
25+
"minute": 0,
26+
"hour": "*",
27+
"month": "*",
28+
"day_of_month": "*",
29+
"day_of_week": "*",
30+
"comments": []
31+
}
32+
]
33+
}

0 commit comments

Comments
 (0)