Directory

僅當相關目錄足夠舊(例如 +30 天)時,如何觸發 Ansible 任務?

  • February 7, 2022

僅當相關目錄足夠舊(例如 +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無論它安裝在/mnt1or後面2此處為 3)。3

👨francois@💻zaphod🐙:~/GITLAB/dev/dev_ansible_serviceatonce$ ANSIBLE_NOCOWS=1 ansible-playbook -i inventory/hosts roles/filebackup/filebackup.yaml 

PLAY [localhost] *****************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************
ok: [localhost]

TASK [Check the last backup date] ************************************************************************************************************************************************************
changed: [localhost]

TASK [Get current date for arithmetics] ******************************************************************************************************************************************************
changed: [localhost]

TASK [set_fact] ******************************************************************************************************************************************************************************
ok: [localhost]

TASK [find ideal path to create new backup if last one is too old] ***************************************************************************************************************************
changed: [localhost]

TASK [create path] ***************************************************************************************************************************************************************************
changed: [localhost]

PLAY RECAP ***********************************************************************************************************************************************************************************
localhost                  : ok=6    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

👨francois@💻zaphod🐙:~/GITLAB/dev/dev_ansible_serviceatonce$ ls -d /mnt3/backup.202106*
/mnt3/backup.20210604  /mnt3/backup.20210610  /mnt3/backup.20210615  /mnt3/backup.20210621  /mnt3/backup.20210629  /mnt3/backup.20210630
👨francois@💻zaphod🐙:~/GITLAB/dev/dev_ansible_serviceatonce$ 

關於你的主要問題

僅當相關目錄足夠老時如何觸發 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'
   ...

您可以根據Conditionals使用塊對任務進行分組

引用自:https://unix.stackexchange.com/questions/656391