Ansible 搜尋檔案的父目錄名稱

Ansible 搜尋檔案的父目錄名稱

我正在嘗試獲取由模式標識的文件的父目錄名稱。

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"
    ]
}

相關內容