仅当相关目录足够旧(例如超过 30 天)时,如何触发 Ansible 任务?

仅当相关目录足够旧(例如超过 30 天)时,如何触发 Ansible 任务?

仅当相关目录足够旧(例如超过 30 天)时,如何触发 Ansible 任务?

想要做类似的事情

- name: backup biggest files 
  #get difference between currentdate & last backup
  register: age 

我知道我可以得到一个字符串或一个定义的when子句,但我不知道如何在这里。

我的目标是,/mnt/backup.YYYYMMDD例如,如果超过 30 天,则创建一个任务列表来创建新的日期目录并自行进行备份(同步方法可能很好?)。

我怎样才能得到这个?

获取步骤:

  • 查找上次备份日期
  • 查找当前日期
  • 他们之间的算术差异
  • 将该差异应用为 yaml 文件中任务中的 when 语句
 - name: Check the last backup date                                                                                                                                                          
    shell: |                                                                                                                                                                                  
      #or find module                                                                                
    register: lastone
                                                                                                                                                                        
  - name: Get current date for arithmetics                                                                                                                                                    
    shell: |                                                                                                                                                                                  
      echo $(date +%s)                                                                                                                                                                        
    register: currentdate                                                                                                                                                                     

  - name: Find ideal path to create new backup if last one is too old                                                                                                                         
    # define & create new directory if currentdate - lastone is over a numeric value (suffisant difference)                                                                                                                                                                            
    when: "{{ currentdate | int - lastone | int }}" > 40000  

在这里,我已经完成了用于测试的解决方案猪风格的虚拟开始:

---                                                                                                                                                                                           
- hosts: localhost                                                                                                                                                                            
  become: true                                                                                                                                                                                
  become_method: sudo                                                                                                                                                                         
  become_user: francois                                                                                                                                                                       

  tasks:                                                                                                                                                                                      

  - name: Check the last backup date                                                                                                                                                          
    shell: |                                                                                                                                                                                  
      date +%s -r $(find /mnt{1,2,3}/ -type d -name "backup.*[0-9]" 2> /dev/null | sort | tail -1)                                                                                            
    args:                                                                                                                                                                                     
      executable: /bin/bash                                                                                                                                                                   
    register: lastone                                                                                                                                                                         

  - name: Get current date for arithmetics                                                                                                                                                    
    shell: |                                                                                                                                                                                  
      date +%s                                                                                                                                                                                
    register: currentdate                                                                                                                                                                     

  - set_fact:                                                                                                                                                                                 
      difference: "{{ currentdate.stdout | int - lastone.stdout | int }}"                                                                                                                   

  - name: Find ideal path to create new backup if last one is too old                                                                                                                         
    shell: |                                                                                                                                                                                  
      find /mnt{1,2,3}/ -type d -name "backup.*[0-9]" 2> /dev/null | sort -n | tail -1 | sed "s/\.[0-9].*/\.$(date +%Y%m%d)/"                                                                 
    args:                                                                                                                                                                                     
      executable: /bin/bash                                                                                                                                                                   
    register: rep                                                                                                                                                                             
    when:                                                                                                                                                                                     
      - difference | int > 4000                                                                                                                                                               

  - name: Create path                                                                                                                                                                         
    file:                                                                                                                                                                                     
      path: "{{ rep.stdout }}"                                                                                                                                                                
      state: directory                                                                                                                                                                        
      mode: "0755"                                                                                                                                                                            
    when:                                                                                                                                                                                     
      - rep is defined                                                                                                                                                                        
      - difference | int > 4000   

这会很好地创建backup.20210630目录,无论它安装在/mnt1or23(这里 3)。

答案1

关于你的主要问题

仅当相关目录足够旧时,如何触发 Ansible 任务? ...我的目标是如果backup.YYYYMMDD超过 30 天...

我根据给定的命名约定设置了一个小目录测试

$ ls -l
drwxr-xr-x. 2 user group     4096 Jan  2 00:00 backup.20220102
drwxr-xr-x. 2 user group     4096 Jan  9 00:00 backup.20220109
drwxr-xr-x. 2 user group     4096 Jan 16 00:00 backup.20220116
drwxr-xr-x. 2 user group     4096 Jan 23 00:00 backup.20220123
drwxr-xr-x. 2 user group     4096 Jan 30 00:00 backup.20220130
drwxr-xr-x. 2 user group     4096 Feb  6 00:00 backup.20220206

以及更改mtime目录的通过touch -t YYMMDDHHMM.SS backup.YYYYMMDD

$ stat backup.20220102/
  File: ‘backup.20220102/’
  Size: 4096            Blocks: 8          IO Block: 4096   directory
...
Access: (0755/drwxr-xr-x)  Uid: (1234/user)   Gid: (1234/group)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2022-01-02 00:00:00.000000000 +0100
Modify: 2022-01-02 00:00:00.000000000 +0100
Change: 2022-02-06 09:00:00.000000000 +0100
 Birth: -

可以根据[am]time属性或名称后缀进行比较YYYYMMDD。你可以从find模块。

---
- hosts: test
  become: false
  gather_facts: false

  tasks:

  - name: Find directories older than 30 days
    find:
      paths: "/home/{{ ansible_user }}"
      file_type: directory
      age: 30d
    register: result

  - name: Show result
    debug:
      msg: "{{ item[0] }}"
    loop:
      - "{{ result.files | flatten(levels=1) }}"
    loop_control:
      extended: yes
      label: "{{ ansible_loop.index0 }}"

然后loop超过结果变量过滤数据。您还可以查看该stat模块检索文件或文件系统状态

要“查找”您当前的日期,您可以查看Ansible 事实

---
- hosts: test
  become: false
  gather_facts: true

  tasks:

  - name: Show Gathered Facts
    debug:
      msg: "{{ ansible_facts }}"

因为它通常已经收集好了。

TASK [Show Gathered Facts] ******
ok: [test.example.com] =>
  msg:
    ...
    date_time:
      date: '2022-02-06'
      day: '06'
      epoch: '1644142041'
      hour: '11'
      iso8601: '2022-02-06T10:07:21Z'
      iso8601_basic: 20220206T110721823347
      iso8601_basic_short: 20220206T110721
      iso8601_micro: '2022-02-06T10:07:21.823347Z'
      minute: '07'
      month: '02'
      second: '21'
      time: '11:07:21'
      tz: CET
      tz_offset: '+0100'
      weekday: Sunday
      weekday_number: '0'
      weeknumber: '05'
      year: '2022'
    ...

你可以用块对任务进行分组基于条件句

相关内容