Skip to content

Request time comparisons that are straightforward and the intent easy to read #86525

@vontrapp

Description

@vontrapp

Summary

This is similar to 79465 but with more general application. This feature, if implemented, would offer another more succinct way to accomplish the results asked for in 79465.

It would be helpful for ansible to have a syntactic way to refer to and compare timestamps, and to have timestamps made available on a wide variety of entities.

For example I got this result when searching how to warn if a config file is newer than the service start time

- name: Warn if config is newer
  ansible.builtin.debug:
    msg: "WARNING: Config file /etc/service.conf is newer than service start"
  when: >
    config_file.stat.exists and
    (config_file.stat.mtime > (service_time.stdout | regex_search('\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [A-Z]{3,4}') | to_datetime('%Y-%m-%d %H:%M:%S %Z')).strftime('%s') | float)

service_time.stdout was of course an output from a shell command.

This is unacceptably obtuse and error prone.

If the ansible stat.mtime can be treated natively as a timestamp and compared directly to other timestamps (maybe this is the case already) and if the ansible service primitive could also be asked for timestamp attributes (start time, restart time, stop time if stopped etc) that can also be directly compared to other timestamps then this would offer a clear and intuitive way to do a lot of checks.

Other timestamp checks that could be done:

  • generate file C if file B is newer than file A (similar to 'make' semantics)
  • run a script if the script itself is newer than a last-run file-marker
  • run a task if the system boot time is newer than some other time
  • run a task or issue a warning if a file is newer than a service start time (above use case)
    • note that in my particular case I may not want to automatically restart the service as the impact could be too great and should be taken intentionally at some more opportune time, but I want to be warned every run that this item still needs addressed.

In summary

  • a timestamp primitive to exist in ansible, if not already
  • comparisons and other operations done on timestamps behave as expected, if not already
  • existing objects that have timestamp-like attributes use the timestamp primitive, if they do not already (stat.mtime?)
  • additional objects whose representative entities have timestamps get those timestamps exposed as ansible timestamp primitive attributes, like services as one example.

Issue Type

Feature Idea

Component Name

ansible_facts.services and others

Additional Information

- name: check config file for time
  ansible.builtin.stat:
    path: /etc/service.conf
  register: config_file
- name: populate services to check for start time
  ansible.builtin.service_facts:
  
- name: Warn if config is newer
  ansible.builtin.debug:
    msg: "WARNING: Config file /etc/service.conf is newer than service start"
  when:
    - ansible_facts.services['myservice.service']['state'] == 'running'
    - config_file.stat.exists
    - config_file.stat.mtime > ansible_facts.services['myservice.service']['start_time']

Code of Conduct

  • I agree to follow the Ansible Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureThis issue/PR relates to a feature request.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions