大网数据广告活动
服务器运维问答

Ansible - can't refer to module parameter value

· 52CE 我爱测

This is my playbook --- - 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_t…

问题原文

原帖链接 · azk · CC BY-SA 4.0

This is my playbook

---
- 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

When I excecute it:

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

I get the values:

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

To the end of the matched mount point (/DATA) there are three filesystems parameters:

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

The question is: How can I refer to this parameters?

I have tried using myfs.lv but don't work and varius more ways. I think this is because those aren't inside 'item'

Desired: using somthing like "myfs.lv" and get "DATA"

hope you can help. Thanks in advance!

已采纳解答

原帖链接 · flowerysong · CC BY-SA 4.0

If you refer to the structure that debug outputs you can see that myfs.lv is clearly wrong, because the only keys at the top level are msg, changed, and results.

When you run a task in a loop, the results are stored as a list under the results key of the registered variable. To access that particular result, you would do something like myfs.results.2.lv.

Hardcoding an index like this is generally not what you want, though, so you should instead do something to select the result you want. (myfs.results | reject('skipped') | list | first).lv would retrieve lv for the first non-skipped result.

Or, of course, you could make this easier on yourself by not looping in the first task:

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-08-30)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。