Ansible

在 Ansible 中訪問調試值

  • August 9, 2022

我的 ansible 程式碼如下所示,

 ¦ - name: Copying Archived File
 ¦ ¦ unarchive:
 ¦ ¦ ¦ src: "/home/user/{{ item }}"
 ¦ ¦ ¦ dest: "/home/user/someLocation"
 ¦ ¦ ¦ list_files: yes
 ¦ ¦ register: archive_contents
 ¦ ¦ loop: "{{ files_to_unarchive }}"

 ¦ - name: Print Directory Name
 ¦ ¦ debug:
 ¦ ¦ ¦ msg: "{{ archive_contents }}"

我得到的輸出是,

ok: [serverName] => {
   "msg": {
       "changed": false, 
       "msg": "All items completed", 
       "results": [
           {
               "ansible_loop_var": "item", 
               "changed": false, 
               "dest": "/home/user/someLocation", 
               "failed": false, 
               "files": [
                   "6880880/", 
                   "6880880/README.txt", 
                   "6880880/opatch_generic.jar", 
                   "6880880/version.txt"
               ], 
               "gid": 110, 
               "group": "oracle", 
               "handler": "ZipArchive", 
               "invocation": {
                   "module_args": {
                       "attributes": null, 
                       "backup": null, 
                       "content": null, 
                       "creates": null, 
                       "delimiter": null, 
                       "dest": "/home/user/someLocation", 
                       "directory_mode": null, 
                       "exclude": [], 
                       "extra_opts": [], 
                       "follow": false, 
                       "force": null, 
                       "group": null, 
                       "keep_newer": false, 
                       "list_files": true, 
                       "mode": null, 
                       "owner": null, 
                       "regexp": null, 
                       "remote_src": false, 
                       "selevel": null, 
                       "serole": null, 
                       "setype": null, 
                       "seuser": null, 
                       "src": "/var/tmp/ansible-tmp-1660003769.3-20299-226224112946024/source", 
                       "unsafe_writes": false, 
                       "validate_certs": true
                   }
               }, 
               "item": "XYZ.zip", 
               "mode": "0755", 
               "owner": "user", 
               "size": 21, 
               "src": "/var/tmp/ansible-tmp-1660003769.3-20299-226224112946024/source", 
               "state": "directory", 
               "uid": 110
           }
       ]
   }
}

我需要在另一個任務中使用 files:6880880 中的第一個值(目錄名)。我試過味精:“{{ archive_contents

$$ ‘results’ $$}}" 和 “{{ archive_contents$$ ‘results’ $$$$ ‘files’ $$$$ 0 $$}}” 但出現錯誤。我對 ansible 還很陌生。我想我錯過了一些東西。 使用archive_contents時出錯$$ ‘results’ $$$$ ‘files’ $$$$ 0 $$==> 致命的:$$ serverName $$: 失敗的!=> {“msg”: “該任務包含一個帶有未定義變數的選項。錯誤是:’list object’ has no attribute ‘files’\n\n錯誤似乎在 ‘/home/user/ansibleScript.yml ‘:第 46 行,第 7 列,但可能\n位於文件中的其他位置,具體取決於確切的語法問題。\n\n違規行似乎是:\n\n\n - name: Print Directory Name\n ^ here\ n”}

您能否建議我如何獲得所需的價值?

屬性結果是一個列表。您必須引用該項目以獲取其屬性。最簡單的選項是第一項的路徑

dir0: "{{ archive_contents.results.0.files.0 }}"

dir0: 6880880/

通常,列表結果中可能有更多項目。全部獲取

dirs: "{{ archive_contents.results|map(attribute='files.0')|list }}"

給出一個列表

dirs:
 - 6880880/

獲取第一個項目

dirs.0: 6880880/

引用自:https://serverfault.com/questions/1107716