首页 > 解决方案 > Ansible:条件寄存器检查

问题描述

我正在创建一个剧本来执行一个动作,但有一个条件:只有在日期早于 x 天时才执行我的命令

这是我尝试使用的

with_items: "{{ (my_register | sort(attribute='ctime'))[30:] | list }}"

- name: some action
  shell: some command | grep command
  register: my_register
  ignore_errors: True

- name: conditional action
  shell: command
  become: yes
  when: my_register
  with_items: "{{ (my_register | sort(attribute='ctime'))[x days:] | list }}"

我希望检查“my_register”中的信息是否超过 x 天。

标签: pythonlinuxansibleredhat

解决方案


我已经用测试文件进行了测试。请检查这是否对您的方案有用。在什么情况下你可以给出如下

  - name: Test
    stat:
        path: /tmp/test.txt
    register: output
  - name: conditional action
    shell: command
    become: yes
    when: (ansible_date_time.epoch|int - output.stat.ctime|int) < 2592000

注意:2592000 是以秒为单位的 30 天


推荐阅读