패턴으로 식별되는 파일의 상위 디렉터리 이름을 얻으려고 합니다.
base/tool1/sub/test.log
base/tool2/abc/values.log
base/tool3/sub/test.log
test.log가 찾을 모든 디렉토리 절대 경로를 얻고 싶습니다. 그래서 결과로 얻고 싶은 일치가 될 것입니다 base/tool1/sub/
.base/tool3/sub/
- name: "Loop-Playbook"
hosts: all
tasks:
- name: "Filter File Matches"
find:
paths: "/base"
file_type: "file"
recurse: "yes"
patterns: "*test.log"
register: files_matched
- name: "Debug files_matched full"
debug:
var: files_matched.files
- name: "Debug files_matched items"
debug:
var: item.path | dirname
loop: "{{ files_matched.files| flatten(levels=1) }}"
loop_control:
label: "{{ item.path }}"
내 생각에는 다음과 같은 것을 사용해야 할 것 같지만 {{ item.path | dirname }}
솔직히 말해서 이것을 어디서 정의해야 하는지 전혀 모르겠습니다.
누군가 나를 도와줄 수 있나요?
답변1
진자의 map()속성을 추출하거나 시퀀스에 필터를 적용할 수 있습니다. 두 가지를 모두 사용하여 하나의 필터 체인에서 찾기 결과를 변환합니다.
---
- name: "basename of find results"
hosts: localhost
gather_facts: False
vars:
testfiles:
- base/tool1/sub/test.log
- base/tool2/abc/values.log
- base/tool3/sub/test.log
tasks:
- name: "Set up test case"
block:
- tempfile:
state: directory
register: mktemp
- file:
path: "{{ ( mktemp.path ~ '/' ~ item ) | dirname }}"
state: directory
loop: "{{ testfiles }}"
- file:
path: "{{ mktemp.path ~ '/' ~ item }}"
state: touch
loop: "{{ testfiles }}"
- name: "Filter File Matches"
find:
paths: "{{ mktemp.path }}"
file_type: "file"
recurse: "yes"
# patterns is already matched against basename
# Do not use a wildcard in front, so only "test.log" matches
patterns: "test.log"
register: files_matched
- name: "Debug files_matched full"
debug:
var: files_matched.files
verbosity: 1
- name: "Directories found"
debug:
msg: "{{ testdirnames }}"
vars:
# map to extract an attribute
# map to apply a filter
# (it can do either)
# list filter to consume any generator object returned by Jinja
testdirnames: "{{ files_matched.files | map(attribute='path') | map('dirname') | list }}"
산출:
$ ansible-playbook sf1073762.yml
PLAY [basename of find results] **************************************************************************************
TASK [tempfile] ******************************************************************************************************
changed: [localhost]
TASK [file] **********************************************************************************************************
changed: [localhost] => (item=base/tool1/sub/test.log)
changed: [localhost] => (item=base/tool2/abc/values.log)
changed: [localhost] => (item=base/tool3/sub/test.log)
TASK [file] **********************************************************************************************************
changed: [localhost] => (item=base/tool1/sub/test.log)
changed: [localhost] => (item=base/tool2/abc/values.log)
changed: [localhost] => (item=base/tool3/sub/test.log)
TASK [Filter File Matches] *******************************************************************************************
ok: [localhost]
TASK [Debug files_matched full] **************************************************************************************
skipping: [localhost]
TASK [Directories found] *****************************************************************************************************
ok: [localhost] => {
"msg": [
"/tmp/ansible.Nc0b6i/base/tool1/sub",
"/tmp/ansible.Nc0b6i/base/tool3/sub"
]
}