简体   繁体   English

ansible - 通过模式作为额外的变量

[英]ansible - pass pattern as extra var

How do I fix following playbook to accept file pattern as an extra var and delete files with matching file pattern older than given no.如何修复以下剧本以接受文件模式作为额外的 var 并删除匹配文件模式早于给定编号的文件。 of days?天?

---
- hosts: destination_servers
  vars:
   file_pattern: "{{ file_name }}"

  tasks:
  - name: Find all files older than {{ age }} days
    find:
     path: '{{ dir }}'
     file_type: file
     pattern: '{{ file_pattern }}'
     age: '{{ age }}'
     recurse: no
    register: files_to_delete
  - name: Remove files
    file:
     path: '{{ dir }}'
     file_type: file
     state: absent
    with_items: "{{ files_to_delete.files }}"

command to execute要执行的命令

ansible-playbook -i inventories/servers.ini playbook/deleteFiles.yml -e "dir=/home/test/folder/ age=30d file_name=*.txt"

file_name can have any pattern like *.* or *.txt or file*.txt or file*.* file_name 可以有任何模式,如*.* or *.txt or file*.txt or file*.*

Got the solution and following is the updated playbook得到了解决方案,以下是更新的剧本

---
- hosts: destination_servers
  vars:
   file_pattern: '{{ file_name }}'

  tasks:
  - name: Find all files older than {{ age }} days
    find:
     paths: '{{ dir }}'
     file_type: file
     pattern: '{{ file_pattern }}'
     age: '{{ age }}'
     recurse: no
    register: files_to_delete
  - name: Remove files
    file:
     path: '{{ item.path }}'
     state: absent
    with_items: "{{ files_to_delete.files }}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM