Cannot understand error from ansible parted module

849 Views Asked by At

I am trying to create a partition on a device, check what was created and use that information for further steps, like setting a filesystem on the partition. But I am getting the error below and I don't understand what I am missing.

Here are the relevant tasks

  - name: Fetch partitions info "{{ item.0.device }}"
    parted: device={{ item.0.device }} unit=MiB
    register: nvme2n1_info
    loop:  "{{ local_volume_mount_disks|subelements('partitions') }}"
  - debug:
      var: nvme2n1_info.partitions

  - name: Create partitions "{{ item.0.device }}"
    parted:
      device: "{{ item.0.device }}"
      number: "{{ item.1.number }}"
      state: present
      label: gpt
      part_start: "{{ item.1.start }}"
      part_end: "{{ item.1.end }}"
    loop:  "{{ local_volume_mount_disks|subelements('partitions') }}"
  - parted: device= "{{ item.0.device }}" unit=GiB
    register: nvme2n1_info
    loop:  "{{ local_volume_mount_disks|subelements('partitions') }}"
  - debug:
      var: nvme2n1_info.partitions

The above tasks are using the following variables

# Create partition and mount filesystems for service storage
local_volume_mount_enabled: true
local_volume_mount_root_directory: /local-volumes

local_volume_mount_disks:
  - device: /dev/nvme2n1
    partitions:
      - number: 1
        start: 1GiB
        end: 800GiB
        storage_class: ssd-wkr-node-services

Output:

   TASK [local_volume_mount : Fetch partitions info "{{ item.0.device }}"] ********
   Friday 19 March 2021  18:45:41 +0000 (0:00:00.025)       0:00:22.876 ********** 
   ok: [node1] => (item=[{'device': '/dev/nvme2n1', 'partitions': [{'number': 1, 'start': '1GiB', 'end': '800GiB', 'storage_class': 'ssd-wkr-node-services'}]}, {'number': 1, 'start': '1GiB', 'end': '800GiB', 'storage_class': 'ssd-wkr-node-services'}])
   
   TASK [local_volume_mount : debug] **********************************************
   Friday 19 March 2021  18:45:41 +0000 (0:00:00.268)       0:00:23.144 ********** 
   ok: [node1] => {
       "nvme2n1_info.partitions": "VARIABLE IS NOT DEFINED!"
   }
   
   TASK [local_volume_mount : Create partitions "{{ item.0.device }}"] ************
   Friday 19 March 2021  18:45:41 +0000 (0:00:00.027)       0:00:23.171 ********** 
   ok: [node1] => (item=[{'device': '/dev/nvme2n1', 'partitions': [{'number': 1, 'start': '1GiB', 'end': '800GiB', 'storage_class': 'ssd-wkr-node-services'}]}, {'number': 1, 'start': '1GiB', 'end': '800GiB', 'storage_class': 'ssd-wkr-node-services'}])
   
   TASK [local_volume_mount : parted] *********************************************
   Friday 19 March 2021  18:45:41 +0000 (0:00:00.242)       0:00:23.414 ********** 
  failed: [node1] (item=[{'device': '/dev/nvme2n1', 'partitions': [{'number': 1, 'start': '1GiB', 'end': '800GiB', 'storage_class': 'ssd-wkr-node-services'}]}, {'number': 1, 'start': '1GiB', 'end': '800GiB', 'storage_class': 'ssd-wkr-node-services'}]) => {"ansible_loop_var": "item", "changed": false, "err": "Error: Could not stat device unit - No such file or directory.\n", "item": [{"device": "/dev/nvme2n1", "partitions": [{"end": "800GiB", "number": 1, "start": "1GiB", "storage_class": "ssd-wkr-node-services"}]}, {"end": "800GiB", "number": 1, "start": "1GiB", "storage_class": "ssd-wkr-node-services"}], "msg": "Error while getting device information with parted script: '/sbin/parted -s -m  -- unit 'GiB' print'", "out": "", "rc": 1}
 
1

There are 1 best solutions below

0
On

You are looping, thus it is not nvme2n1_info['partitions'] but nvme2n1_info.results list.

---

- name: test
  hosts: localhost
  become: true
  tasks:
    - parted:
        device: /dev/{{ item }}
        unit: MiB
      register: out
      loop:
        - sda
    - debug:
        msg: "{{ out.results | type_debug }}"