Ansible-无法引用模块参数值

Ansible-无法引用模块参数值

这是我的剧本

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

希望您能帮忙。提前致谢!

答案1

如果您参考调试输出的结构,您会发现这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 }}"

相关内容