【问题标题】:Ansible: Check disk size and create volume group according its sizeAnsible:检查磁盘大小并根据其大小创建卷组
【发布时间】:2020-08-20 14:22:46
【问题描述】:

在第一个节点 sdb 大小为 1GB 第二个节点 sdb 大小为 600M 只是我无法检查磁盘的大小。执行以下剧本时出现错误(显示大小为 0 值)。

---
- hosts: all
  become: yes
  tasks:

    - debug:
        msg: "{{ ansible_devices.sdb.size }} is less than 800"
      when:  ansible_devices.sdb.size|int  < 800


$ ansible-playbook lvm-new.yml

ansible-playbook lvm-new.yml

PLAY [all] ***********************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************
ok: [servera]
ok: [serverb]

TASK [debug] *********************************************************************************************************************************************************************
ok: [serverb] => {
    "msg": "600.00 MB is less than 800"
}
ok: [servera] => {
    "msg": "1.00 GB is less than 800"
}

PLAY RECAP ***********************************************************************************************************************************************************************
servera                    : ok=2    changed=0    unreachable=0    failed=0   
serverb                    : ok=2    changed=0    unreachable=0    failed=0 

你能帮忙解决这个任务吗?

【问题讨论】:

  • 出现错误 ...您应该 edit your question 与我们其他人分享
  • 已编辑,问候
  • 首先,您的代码与输出不匹配,因此请发布您正在运行的实际代码。其次,您正在读取变量ansible_devices,该变量未在您显示的代码中设置,而您将parted 结果存储在sdb_info 中,所以我对您没有获得值并不感到惊讶.
  • 现在我又编辑了。谢谢提醒。这里我无法将大小值转换为整数来检查它是否小于 800

标签: python-2.7 ansible


【解决方案1】:

我已经通过以下方式解决了。

---
- hosts: all
  become: yes
  tasks:
    - block:
        - parted: device=/dev/sdb unit=MiB
          register: sdb_info
        - debug:
            msg: '{{ sdb_info.disk.size }}'
        - debug:
            msg: "{{ sdb_info.disk.size }} greater than 800"
          when: " sdb_info.disk.size  > 800"
        - debug:
            msg: "{{ sdb_info.disk.size }} less than 800"
          when: " sdb_info.disk.size  < 800"

ansible-playbook lvm-new.yml

PLAY [all] *************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [servera]
ok: [serverb]

TASK [parted] **********************************************************************************************************************************************************
fatal: [serverb]: FAILED! => {"changed": false, "err": "Error: Could not stat device /dev/sdb - No such file or directory.\n", "msg": "Error while getting device information with parted script: '/sbin/parted -s -m /dev/sdb -- unit 'MiB' print'", "out": "", "rc": 1}
ok: [servera]

TASK [debug] ***********************************************************************************************************************************************************
ok: [servera] => {
    "msg": "1024.0"
}

TASK [debug] ***********************************************************************************************************************************************************
ok: [servera] => {
    "msg": "1024.0 greater than 800"
}

TASK [debug] ***********************************************************************************************************************************************************
skipping: [servera]

TASK [debug] ***********************************************************************************************************************************************************
ok: [serverb] => {
    "msg": "No disk detected"
}

PLAY RECAP *************************************************************************************************************************************************************
servera                    : ok=4    changed=0    unreachable=0    failed=0   
serverb                    : ok=2    changed=0    unreachable=0    failed=1   

【讨论】:

    猜你喜欢
    • 2019-03-27
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    相关资源
    最近更新 更多