Have /usr/local/bin in cron PATH

This lets scripts in cron jobs find /usr/local/bin/simple_message_to_bus
without hard-coding the whole path.

Signed-off-by: Nils Philippsen <nils@redhat.com>
This commit is contained in:
Nils Philippsen 2021-09-07 13:11:21 +02:00
parent 0b518a7e88
commit 8e05661fc5
2 changed files with 47 additions and 0 deletions

View file

@ -0,0 +1,41 @@
#####################################################
# Ensure PATH in /etc/crontab contains /usr/local/bin
#####################################################
- name: check if PATH is set in crontab
lineinfile:
path: /etc/crontab
state: absent
regexp: '^PATH\s*='
check_mode: yes
changed_when: false
register: path_set_in_crontab
- name: add PATH if not set in crontab
lineinfile:
path: /etc/crontab
state: present
insertbefore: BOF
line: 'PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin'
validate: crontab -T %s
when: not path_set_in_crontab.found
- name: check if PATH in crontab contains /usr/local/bin
lineinfile:
path: /etc/crontab
state: absent
regexp: '^PATH\s*=\s*(.*:)?/usr/local/bin(:.*)?\s*'
check_mode: yes
changed_when: false
register: local_in_path_in_crontab
when: path_set_in_crontab.found
- name: append /usr/local/bin to PATH in crontab if missing
lineinfile:
path: /etc/crontab
state: present
backrefs: yes
regexp: '^(PATH\s*=\s*(?:.*\S)?)\s*$'
line: '\1:/usr/local/bin'
validate: crontab -T %s
when: path_set_in_crontab.found and not local_in_path_in_crontab.found

View file

@ -2,3 +2,9 @@
copy: src=simple_message_to_bus dest=/usr/local/bin/ mode=0755
tags:
- fedora-messaging
- name: ensure PATH in crontab contains /usr/local/bin
import_tasks: crontab_path.yml
tags:
- fedora-messaging
- cron