使用分段输出中的属性

时间:2019-07-02 17:49:18

标签: ansible

我需要Ansible来安装服务器,并且必须为LVM进行分区。如何使用“ sda_info.partitions”的第二个分区中的属性“ size”?

hosts: node2
 become: yes
 become_method: sudo
 become_flags: -H -S
 tasks:
 - name: Some name
   parted:
     device: /dev/sda
     unit: s
   register: sda_info

OUTPUT

TASK [debug] ***************************************************************
ok: [node2] => {
   "sda_info.partitions": [
       {
           "begin": 4096.0, 
           "end": 51202047.0, 
           "flags": [
               "boot"
           ], 
           "fstype": "ext4", 
           "name": "", 
           "num": 1, 
           "size": 51197952.0, 
           "unit": "s"
       }, 
       {
           "begin": 51202048.0, 
           "end": 52248575.0, 
           "flags": [], 
           "fstype": "linux-swap(v1)", 
           "name": "", 
           "num": 2, 
           "size": 1046528.0, 
           "unit": "s"
       }
   ]
}

1 个答案:

答案 0 :(得分:0)

sda_info.partitions 是包含2个词典的列表。可以参考第二本字典

sda_info.partitions[1]

和属性 size 可以分配给变量

- set_fact:
    size_2nd_part: "{{ sda_info.partitions[1].size }}"

很有可能该列表将按分区号排序。如果不是,则上面的变量将不是第二个分区的大小。使用json_query确定

- set_fact:
    size_2nd_part: "{{ sda_info.partitions|json_query('[?num==`2`].size') }}"