我正在使用 ansible 將檔案從 docker 容器復制到檔案夾。我想根據我正在下載的容器版本洗掉不需要的那些。我根本不是腳本撰寫者,但我做到了:
for i in `ls`; do if [[ $i < '6.1.0' ]]; then echo $i this_one; fi; done
在我的 ansible 代碼中,它看起來像這樣:
- name: List files in folder
ansible.builtin.shell: ls /tmp/sql/v{{ tversion }}/upgrade
register: file_out
如何獲取輸出并洗掉早于呼叫版本的檔案(在上面的示例中,任何早于 6.1.0 的檔案)?
檔案結構實際上是6.0.1_to_6.0.2.sql等等。
uj5u.com熱心網友回復:
例如,給定檔案
shell> tree test-482
test-482
├── 5.0.1_to_5.0.4.sql
├── 6.0.1_to_6.0.2.sql
└── 7.0.1_to_7.0.9.sql
查找檔案
- find:
path: test-482
recurse: true
register: result
給出結果
result.files|map(attribute='path')|list:
- test-482/5.0.1_to_5.0.4.sql
- test-482/6.0.1_to_6.0.2.sql
- test-482/7.0.1_to_7.0.9.sql
決議屬性并創建字典
- set_fact:
files_from_to: "{{ dict(_files|zip(_from_to)|list) }}"
vars:
_files: "{{ result.files|map(attribute='path')|list }}"
_names: "{{ _files|map('basename')|map('splitext')|map('first')|list }}"
_from_to: "{{ _names|map('regex_replace', _regex, _replace)|map('from_yaml')|list }}"
_regex: "^(.*)_to_(.*)$"
_replace: '{"from": "\1", "to": "\2"}'
給
files_from_to:
test-482/5.0.1_to_5.0.4.sql:
from: 5.0.1
to: 5.0.4
test-482/6.0.1_to_6.0.2.sql:
from: 6.0.1
to: 6.0.2
test-482/7.0.1_to_7.0.9.sql:
from: 7.0.1
to: 7.0.9
使用測驗版本選擇檔案,例如
- name: Anything higher then 6.1.0
set_fact:
files_del: "{{ files_from_to|dict2items|
selectattr('value.to', 'version', _version, 'gt')|
map(attribute='key')|list }}"
vars:
_version: '6.1.0'
給
files_del:
- test-482/7.0.1_to_7.0.9.sql
或者,
- name: Anything lower then 6.0.2
set_fact:
files_del: "{{ files_from_to|dict2items|
selectattr('value.from', 'version', _version, 'lt')|
map(attribute='key')|list }}"
vars:
_version: '6.0.2'
給
files_del:
- test-482/5.0.1_to_5.0.4.sql
- test-482/6.0.1_to_6.0.2.sql
逐步除錯字典
- debug:
var: _files
vars:
_files: "{{ result.files|map(attribute='path')|list }}"
- debug:
var: _names
vars:
_files: "{{ result.files|map(attribute='path')|list }}"
_names: "{{ _files|map('basename')|map('splitext')|map('first')|list }}"
- debug:
var: _from_to
vars:
_files: "{{ result.files|map(attribute='path')|list }}"
_names: "{{ _files|map('basename')|map('splitext')|map('first')|list }}"
_from_to: "{{ _names|map('regex_replace', _regex, _replace)|map('from_yaml')|list }}"
_regex: "^(.*)_to_(.*)$"
_replace: '{"from": "\1", "to": "\2"}'
- debug:
var: _files|zip(_from_to)|list
vars:
_files: "{{ result.files|map(attribute='path')|list }}"
_names: "{{ _files|map('basename')|map('splitext')|map('first')|list }}"
_from_to: "{{ _names|map('regex_replace', _regex, _replace)|map('from_yaml')|list }}"
_regex: "^(.*)_to_(.*)$"
_replace: '{"from": "\1", "to": "\2"}'
給
_files:
- test-482/5.0.1_to_5.0.4.sql
- test-482/6.0.1_to_6.0.2.sql
- test-482/7.0.1_to_7.0.9.sql
_names:
- 5.0.1_to_5.0.4
- 6.0.1_to_6.0.2
- 7.0.1_to_7.0.9
_from_to:
- from: 5.0.1
to: 5.0.4
- from: 6.0.1
to: 6.0.2
- from: 7.0.1
to: 7.0.9
_files|zip(_from_to)|list:
- - test-482/5.0.1_to_5.0.4.sql
- from: 5.0.1
to: 5.0.4
- - test-482/6.0.1_to_6.0.2.sql
- from: 6.0.1
to: 6.0.2
- - test-482/7.0.1_to_7.0.9.sql
- from: 7.0.1
to: 7.0.9
一步步除錯選擇
- debug:
msg: "{{ files_from_to|dict2items }}"
- debug:
msg: "{{ files_from_to|dict2items|
selectattr('value.to', 'version', _version, 'gt') }}"
vars:
_version: '6.1.0'
- debug:
msg: "{{ files_from_to|dict2items|
selectattr('value.to', 'version', _version, 'gt')|
map(attribute='key')|list }}"
vars:
_version: '6.1.0'
給
msg:
- key: test-482/5.0.1_to_5.0.4.sql
value:
from: 5.0.1
to: 5.0.4
- key: test-482/6.0.1_to_6.0.2.sql
value:
from: 6.0.1
to: 6.0.2
- key: test-482/7.0.1_to_7.0.9.sql
value:
from: 7.0.1
to: 7.0.9
msg:
- key: test-482/7.0.1_to_7.0.9.sql
value:
from: 7.0.1
to: 7.0.9
msg:
- test-482/7.0.1_to_7.0.9.sql
uj5u.com熱心網友回復:
既然您標記bash了 ,這是一種方法。
Linux find 命令支持-regex匹配檔案名,也支持洗掉檔案。因此,我們可以使用它們來查找和洗掉檔案,而無需迭代。
# Find all files up to 6.0.0, and delete them
find . -type f -regex '\.\/[1-5]\.[0-9]\.[0-9]_to_[1-5]\.[0-9]\.[0-9].sql' -delete
# Find files of 6.0 series (excluding anything above 6.1)
find . -type f -regex '\.\/6\.0\.[0-9]_to_6\.0\.[0-9].sql' -delete
可能有一種更有效的方式來一次性查找和洗掉檔案,但我們可以在 Ansible 任務中使用它:
- shell:
cmd: |
find . -type f -regex '\.\/[1-5]\.[0-9]\.[0-9]_to_[1-5]\.[0-9]\.[0-9].sql' -delete
find . -type f -regex '\.\/6\.0\.[0-9]_to_6\.0\.[0-9].sql' -delete
chdir: path/to/sql
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/414673.html
標籤:
