How-Tos

How-To: Make Every Server Log in ISO-8601 UTC With Ansible (rsyslog and journald)

Leave a comment

Jim Clausing’s ISC diary on syslog timestamps explained something I’d noticed on my Debian boxes: since Debian 12 and Ubuntu 23.04, rsyslog isn’t installed by default and the old RSYSLOG_TraditionalFileFormat directive was dropped, so logs switched from the classic “Nov 22 13:05:01” style to RFC 3339 timestamps. Meanwhile Fedora and Alma keep the old format. I run a mix of Arch and Debian, so I made it consistent with Ansible. Mixed formats and mixed timezones make correlating logs miserable.

1. Put servers on UTC

- name: Set timezone to UTC
  community.general.timezone:
    name: UTC

2. rsyslog (where installed): force high-precision ISO timestamps

- name: Use RFC 3339 timestamps in rsyslog
  ansible.builtin.copy:
    dest: /etc/rsyslog.d/00-iso-timestamps.conf
    content: |
      $ActionFileDefaultTemplate RSYSLOG_FileFormat
    mode: "0644"
  notify: restart rsyslog
  when: "'rsyslog' in ansible_facts.packages"

(Gather package facts first with ansible.builtin.package_facts, and add a handler that restarts rsyslog.)

3. journald: it already stores precise times, so just read them in ISO

journalctl -o short-iso-precise --since today
journalctl -o short-iso-precise --utc -u sshd

I drop a shell alias on every host so I never forget:

- name: Handy ISO journalctl alias
  ansible.builtin.lineinfile:
    path: /etc/profile.d/jlog.sh
    line: "alias jlog='journalctl -o short-iso-precise --utc'"
    create: true
    mode: "0644"

4. Check

Run the playbook with --check --diff first, then for real, then logger "iso test" and look at the newest line in your log. If you forward to a central collector, make sure it’s parsing RFC 5424-style timestamps too.

God Bless, and May the Source Be with You!

References & Further Reading

Subscribe (RSS) Follow How-Tos via RSS

Leave a note

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.