Score:0

Ansible Parted, only run if no partitions exist

cn flag
hat

I want to only run the parted operation if no partitions currently exist on that device. Said parts could have been made previously or manually outside of ansible in the past and in that case should not be changed and the task skip.

name: "Add Disk" ## this needs amending to skip if any partition is already present on that device
    become: true
    parted:
      device: /dev/xvdf
      number: 1
      state: present
      flags: [ lvm ]
      label: gpt
      name: primary

However currently when running this even if a partition already exists then changes are being made and an error similar to below is printed by tower suggesting it is making changes (after a reboot the system became unbootable)

"_ansible_no_log": false,
    "err": "Error: Partition(s) 1 on /dev/xvdf have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use.  As a result, the old partition(s) will remain in use.  You should reboot now before making further changes.\n",

I was hoping it can be achieved with a when condition getting an ansible_fact similar to below?

when: ansible_facts.device.xvdf is not defined

but I can't find the correct fact or syntax for this idea?

from documentation I was hoping the parted module was idempotent enough to recognize if a partition already exists and not change, but I think if the specified config is in anyway different it tries to apply it

The target system is a AWS EC2 running RHEL7 and the attached disk is a secondary (non-root) encrypted EBS Volume

Score:0
pe flag

Try to play with this to see it helps you:

  tasks:
  - name: Debug
    debug:
      msg: "No xvdf1"
    when: ansible_devices.xvdf.partitions.xvdf1 is not defined
cn flag
hat
Thankyou! I have been using when: "'/dev/xvdf1' not in ansible_facts.lvm.pvs" but this one might be cleaner!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.