'Playbook creating filesystem fails in checkmode and is not idempotent

I'm attempting to create a playbook to create and mount a filesystem.

However, it consistently fails to run in check mode:

TASK [Create partition on device] **********************************************
changed: [node1]
changed: [node2]

TASK [Create volume group] *****************************************************
skipping: [node1]
skipping: [node2]

TASK [Create logical volume] ***************************************************
fatal: [node1]: FAILED! => {"changed": false, "err": "  Volume group \"vg_data\" not found\n  Cannot process volume group vg_data\n", "msg": "Volume group vg_data does not exist.", "rc": 5}

In addition, it seems to have to be run multiple times to function as a playbook:

TASK [Create partition on device] ********************************************************************************************
changed: [node1]
changed: [node2]
TASK [Create volume group] ***************************************************************************************************
skipping: [node1]
...
TASK [Create logical volume] *************************************************************************************************
fatal: [node2]: FAILED! => {"changed": false, "err": "  Volume group \"vg_data\" not found\n  Cannot process volume group vg_data\n", "msg": "Volume group vg_data does not exist.", "rc": 5}

If I then run it again and again it works fine however.

Question:

  • Should I retrieve facts manually using some module to make this playbook work, that is, make it run on one-go?
  • Is there any way I can make check-mode not fail.

Following is the play I use:

---
- name: see if device exists
  parted:
    unit: GiB
    device: /dev/sdb
  register: sdb_device
- name: set fact
  set_fact:
    sdb_disk_size: "{{ sdb_device['disk']['size']}}"
- name: Create partition on device
  parted:
    device: /dev/sdb
    number: 1
    state: present
    part_end: 100%
  when: 
    - ('sdb1' not in (ansible_devices|string))
    - ( sdb_disk_size |float) >= 2
- name: Create volume group
  lvg:
    vg: vg_data
    pvs: /dev/sdb1
  when: ('sdb1' in (ansible_devices |string))
- name: Create logical volume
  lvol:
    vg: vg_data
    lv: lv_data
    size: 100%PVS
- name: Create filesystem
  filesystem:
    fstype: xfs
    dev: /dev/mapper/vg_data-lv_data
- name: Create data directory to mount filesystem
  file:
    state: directory
    path: /src/data
    mode: '0755'
- name: Mount directory
  mount:
    path: /srv/data
    src: /dev/mapper/vg_data-lv_data
    state: mounted
    fstype: xfs



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source