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

Not able to get swap partition UUID after creating it with Parted ansible's module

· 52CE 我爱测

I have to create a new partition in a fresh disk on a VM. I use the parted module in order to create the partition. In a future task (in the same playbook), I need to use the UUID of the device partition (not the partition UUID, …

问题原文

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

I have to create a new partition in a fresh disk on a VM. I use the parted module in order to create the partition. In a future task (in the same playbook), I need to use the UUID of the device partition (not the partition UUID, but the device UUID of the partition, present for example in /dev/disks/by-uuid/)

The problem is that : I don't find a way to get the UUID of the partition in a task after the parted module archieved the job. I'm sure that the parted module works fine because the partition exists after the run.

Let's take a look of what I tried :

Creating the partition with the parted module
Re-launch facts with the setup module (with and without filtering on ansible_devices) in order to use the ansible_devices.sdX.partitions.sdXY.uuid --> return void
Use the blkid -s UUID -o value /dev/sdXY throught the shell module --> return void
Use the ls -l /dev/disk/by-uuid/ | grep sdXY | awk '{print $9}' throught the shell module --> still return void

The terrible fact is that : if I launch the same task (like 2, 3 or 4) in another playbook, I'm able to get results. But in the same playbook of the parted module : I can't.

Here's my full playbook :

---
- hosts: all
  become: yes
  gather_facts: "yes"
  tasks:

   - name: Check actual swap quantity
     debug:
       var: ansible_swaptotal_mb
     failed_when: ansible_swaptotal_mb > 1999


   - name: Add a 2Gb disk
     shell: pwsh ./add_disk_vm.ps1 -vm {{ ansible_hostname }} -diskspace 2
     delegate_to: localhost


   - name: Scan for new disks
     shell: for i in $(ls /sys/class/scsi_host/); do echo "- - -" > /sys/class/scsi_host/$i/scan ; done

   - name: Get letter of the new disk
     shell: 'dmesg | grep "sd\w.*2.00 GiB)$" | grep -o "sd\w" | tail -1'
     register: diskletter


   - name: Create new partition
     community.general.parted:
       device: /dev/{{ diskletter.stdout }}
       fs_type: linux-swap
       label: gpt
       name: swap
       number: 1
       state: present
     register: parted


   #here the task who should work, but returning nothing
   - name: Get UUID
     shell: blkid -s UUID -o value /dev/{{ diskletter.stdout }}1
     register: uuidinfo

I don't mentionned it before, but the last task works fine on an other playbook OR even directly in SSH on the VM. The command doesn't return anything only during the playbook execution context.

Here's some other informations about my setup:

ansible-playbook 2.9.27 python version = 2.7.17 Ubuntu 18.04 (where ansible is installed AND the target server) community.general collection (version 3.7.0)

If someone have any ideas about this ; thanks for all !

已采纳解答

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

Ok finally I found the solution.

The problem is that when the partition is not mounted, it have no UUID. When working with data partitions like xfs, extX, etc... The partition is directly mounted by the system, so the partition is getting direclty an UUID.

It's not the case for a swap partition, you must first enable it with mkswap for the system can mount the Swap partition.

That's why I was able to get the UUID value in a second playbook execution : I reached the UUID value before enabling the swap in the playbook. So the partition wasn't mount and haven't UUID.

If it can help someone

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

资料来源与适用范围

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

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

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