获取逻辑卷当前 space 的函数?

Function to get the current space of a logical volume?

我正在为 1 个 ansible 角色而苦苦挣扎。 我正在尝试使用 2 个变量、额外的 space 来添加到卷中,以及挂载点,例如“/disco1”和“1.5g”

使用 lvextend 扩展我的逻辑卷是可行的,但是当您需要添加更多 space 时,您需要获得正确的逻辑卷总大小并添加额外的 space 以添加。 角色任务:

---
- name: Set device name fact
  set_fact:
   device_path : "{{ item.device}}"
  with_items: "{{ansible_mounts}}"
  when: "fs_name in item.mount"
    
- name: Set device name fact
  set_fact:
   device_name : "{{ item.device.split('/')[-1]}}"
  with_items: "{{ansible_mounts}}"
  when: "fs_name in item.mount"
    
     
- name: Set Volume group fact
  set_fact:
   vg_name : "{{ device_name.split('-')[0]}}"
    
    
- name: Set Logical volume fact
  set_fact:
   lv_name : "{{ device_name.split('-')[1]}}"
    
- name: Get current lv space
  shell:  lvs --noheadings -o lv_size '{{ vg_name }}/{{lv_name}}'| sed 's/g//'
  register: lv_size
  no_log: true
- name: debug lv size
  debug:
    msg: "{{lv_size.stdout_lines}}"
    
- name: extend fs
  lvol:
    vg: "{{vg_name}}"
    lv: "{{lv_name}}"
    size: "{{lv_size.stdout_lines  | int  + fs_size_add |int  }}"
    resizefs: yes

默认文件:main.yml

---
fs_name: /disco1
fs_size_add: '1.4G'

问题是我无法将当前磁盘大小作为整数正确获取,以添加所需大小和当前大小。

经过所有的努力和用户 toydarian 的巨大帮助,我的角色是:

tasks/main.yml

    ---
    - name: Set device name fact
      set_fact:
       device_path : "{{ item.device}}"
      with_items: "{{ansible_mounts}}"
      when: "fs_name in item.mount"
    
    
    - set_fact:
       device_name : "{{ item.device.split('/')[-1]}}"
      with_items: "{{ansible_mounts}}"
      when: "fs_name in item.mount"
    
    - name: Set Volume group fact
      set_fact:
       vg_name : "{{ device_name.split('-')[0]}}"
    
    
    - name: Set Logical volume fact
      set_fact:
       lv_name : "{{ device_name.split('-')[1]}}"
    - shell: |
        lvs --noheadings -o lv_size '{{ vg_name }}/{{lv_name}}' |  grep -oPi '\d+.\d+(?=G)'
      register: vsize
    
      #- fail:
      #   msg: "Current volume sie could not be parsed. Not in correct GB format?"
      #when: vsize_stdout | length == 0
    
    - set_fact:
        nsize: "{{ (vsize.stdout | float) + (fs_size_add  | float) }}"
    - debug:
        msg: |
          current size: "{{vsize.stdout}}"
          new size: "{{nsize}}"
    
    
    
    - name: extend fs
      lvol:
        vg: "{{vg_name}}"
        lv: "{{lv_name}}"
        size: "{{ nsize}}G"
        resizefs: yes

defaults/main.yml

    ---
    fs_name: /disco1
    fs_size_add: '0.4'

你的问题在这一行:size: "{{lv_size.stdout_lines | int + fs_size_add | int }}"

  • lv_size.stdout_lines 是一个 list。当您尝试将其转换为 int 时,结果会变成 0。检查 documentation.
  • '1.4G' 是无法转换为 intstring,因为它包含一个字符 (G)。所以那个也是0
  • size 选项实际上需要 string,而不是 int。如果它得到 int,它会将其转换为 string 并将其视为兆字节。

根据 documentation,应该可以使用百分比来增加这样的逻辑卷的大小 size: "+100%FREE" 以扩展卷以使用所有空闲 space。
您需要使用 ansible 版本 2.1 或更高版本才能工作。
有关详细信息,请参阅 lvcreate(8) man-page and the examples in the documentation

如果您不能使用百分比,可以通过以下方式计算逻辑卷的新大小:

- shell: |
    lvs --noheadings -o lv_size '{{ vg_name }}/{{lv_name}}' | grep -oP '\d+,\d+(?=g)' | sed "s/,/./"
  register: vsize

- fail:
    msg: "Current volume size could not be parsed. Not in GB?"
  when: vsize.stdout | length == 0

- set_fact:
    nsize: "{{ (vsize.stdout | float) + (fs_size_add | float) }}"

- debug:
    msg: |
      current size: {{ vsize.stdout }}
      new size: {{ nsize }}
  • 如果无法解析当前大小,它将失败。例如。如果它以 MB 而不是 GB 为单位。
  • fs_size_add 需要是可解析的 float。例如。 1.4,但 不是 1.4G