Ansible

Ansible - 無法引用模組參數值

  • August 31, 2021

這是我的劇本

---
- hosts: mytestserver
 become: true
 become_method: sudo
 gather_facts: true

 vars:
 # To get vgname and lvname from ansible_mounts's device 
   mt: "{{ MT }}" #Extravariable
   disp_pth: "{{ item['device'] }}"
   disp_tmp: "{{ disp_pth | basename }}"
   DISP: "{{ disp_tmp.split('-') }}"
   VGname: "{{ DISP.0 }}"
   LVname1: "{{ DISP.1 }}"
   DISPP2: "{{ DISP.3|default('') }}"
   LVname2: "{{ DISP.1 }}-{{ DISPP2 }}"
   LVname: "{{ LVname2 if (disp_tmp | regex_search('--')) else LVname1 }}"


 tasks:
   - name: Get filesystem values
     lvol:
       vg: "{{ VGname }}"
       lv: "{{ LVname }}"
     with_items: "{{ ansible_mounts }}"
     when: item.mount == MT
     register: myfs

   - debug:
       var: myfs

當我執行它時:

$ ansible-playbook -i proyects/Inventory/awx_hosts -l mytestserver getlvmfacts.yml -e MT=/DATA

我得到了值:

PLAY [mytestserver] ******************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************************************************************
ok: [mytestserver]

TASK [Get filesystem values] ************************************************************************************************************************************************************************************************************
ok: [mytestserver] => (item={'block_used': 21620, 'uuid': '7fef92b8-8157-43d1-92b4-525a56fa33f8', 'size_total': 1056858112, 'block_total': 258022, 'mount': '/DATA', 'block_available': 236402, 'size_available': 968302592, 'fstype': 'ext4', 'inode_total': 65536, 'options': 'rw', 'device': '/dev/mapper/vgAPP-DATA', 'inode_used': 11, 'block_size': 4096, 'inode_available': 65525})

TASK [debug] ****************************************************************************************************************************************************************************************************************************
ok: [mytestserver] => {
   "myfs": {
       "changed": false,
       "msg": "All items completed",
       "results": [
           {
               "ansible_loop_var": "item",
               "changed": false,
               "item": {
                   "block_available": 414770,
                   "block_size": 4096,
                   "block_total": 516052,
                   "block_used": 101282,
                   "device": "/dev/mapper/vg00-lvol01",
                   "fstype": "ext3",
                   "inode_available": 124211,
                   "inode_total": 131072,
                   "inode_used": 6861,
                   "mount": "/",
                   "options": "rw",
                   "size_available": 1698897920,
                   "size_total": 2113748992,
                   "uuid": "7df9171b-c31f-434f-94c5-344d02775e89"
               },
               "skip_reason": "Conditional result was False",
               "skipped": true
           },
           {
               "ansible_loop_var": "item",
               "changed": false,
               "item": {
                   "block_available": 1037688,
                   "block_size": 4096,
                   "block_total": 1046016,
                   "block_used": 8328,
                   "device": "/dev/mapper/vgDATA-BASET--DATA1",
                   "fstype": "xfs",
                   "inode_available": 4194301,
                   "inode_total": 4194304,
                   "inode_used": 3,
                   "mount": "/BASET/DATA1",
                   "options": "rw",
                   "size_available": 4250370048,
                   "size_total": 4284481536,
                   "uuid": "91689e9b-552c-4f0e-b0ec-0d3d25154a72"
               },
               "skip_reason": "Conditional result was False",
               "skipped": true
           },
           {
               "ansible_loop_var": "item",
               "changed": false,
               "failed": false,
               "invocation": {
                   "module_args": {
                       "active": true,
                       "force": false,
                       "lv": "DATA",
                       "opts": null,
                       "pvs": null,
                       "resizefs": false,
                       "shrink": true,
                       "size": null,
                       "snapshot": null,
                       "state": "present",
                       "thinpool": null,
                       "vg": "vgAPP"
                   }
               },
               "item": {
                   "block_available": 236402,
                   "block_size": 4096,
                   "block_total": 258022,
                   "block_used": 21620,
                   "device": "/dev/mapper/vgAPP-DATA",
                   "fstype": "ext4",
                   "inode_available": 65525,
                   "inode_total": 65536,
                   "inode_used": 11,
                   "mount": "/DATA",
                   "options": "rw",
                   "size_available": 968302592,
                   "size_total": 1056858112,
                   "uuid": "7fef92b8-8157-43d1-92b4-525a56fa33f8"
               },
               "lv": "DATA",
               "size": 1024.0,
               "vg": "vgAPP"
           },
           {
               "ansible_loop_var": "item",
               "changed": false,
               "item": {
                   "block_available": 472667,
                   "block_size": 4096,
                   "block_total": 516052,
                   "block_used": 43385,
                   "device": "/dev/mapper/vg00-lvol07",
                   "fstype": "ext3",
                   "inode_available": 131061,
                   "inode_total": 131072,
                   "inode_used": 11,
                   "mount": "/tivoli",
                   "options": "rw",
                   "size_available": 1936044032,
                   "size_total": 2113748992,
                   "uuid": "2310d046-eaad-4419-a94b-197c2c56502c"
               },
               "skip_reason": "Conditional result was False",
               "skipped": true
           }
       ]
   }
}

在匹配的掛載點 (/DATA) 的末尾,有三個文件系統參數:

"lv": "DATA",
"size": 1024.0,
"vg": "vgAPP"

問題是:我怎樣才能引用這個參數?

我嘗試過使用myfs.lv但不起作用並且使用了更多方法。我認為這是因為那些不在“項目”內

期望:使用“myfs.lv”之類的東西並獲取“DATA”

希望你能幫忙。提前致謝!

如果您參考調試輸出的結構,您會發現這myfs.lv顯然是錯誤的,因為頂層的唯一鍵是msgchangedresults

當您在循環中執行任務時,結果將作為列表儲存在results已註冊變數的鍵下。要訪問該特定結果,您可以執行類似myfs.results.2.lv.

但是,像這樣對索引進行硬編碼通常不是您想要的,因此您應該做一些事情來選擇您想要的結果。(myfs.results | reject('skipped') | list | first).lv將檢索lv第一個未跳過的結果。

或者,當然,您可以通過不循環第一個任務來使自己更輕鬆:

 tasks:
   - name: Get filesystem values
     lvol:
       vg: "{{ mount_dev_split.0 }}"
       lv: "{{ mount_dev_split.1 ~ lv_name_suffix }}"
     vars:
       mount_dev: "{{ (ansible_facts.mounts | selectattr('mount', 'equalto', MT) | list).0.device }}"
       mount_dev_split: "{{ (mount_dev | basename).split('-') }}"
       lv_name_suffix: "{{ ('-' ~ mount_dev_split.3 | default('')) if '--' in mount_dev else '' }}"
     register: myfs

   - debug:
       msg: "{{ myfs.lv }}"

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