Vm creation with ansible playbook

People your support, i’m trying to create a VM with a playbook. I can create the VM all good there the issue is when I try to add a volume using LVM it fails all the time.

The playbook is:

- name: Create a physical volume on /dev/sdb
  lvg:
    vg: vg_data
    pvs: /dev/sdb
    state: present

and is returns:

fatal: [localhost]: FAILED! => {
“changed”: false,
“err”: " Device /dev/sdb excluded by a filter.\n",
“invocation”: {
“module_args”: {
“force”: false,
“pesize”: “4”,
“pv_options”: “”,
“pvresize”: false,
“pvs”: [
“/dev/sdb”
],
“state”: “present”,
“vg”: “vg_data”,
“vg_options”: “”
}
},
“msg”: “Creating physical volume ‘/dev/sdb’ failed”,
“rc”: 5
}

I tried two flavors (CentOS and RockyLinux) and get the same error.

did someone face this?

/dev/sdb probably has partitions on it. Use wipefs to clear it out:

wipefs -a /dev/sdb

I already tried that and nothing.

If I run this playbook directly it create the partition if I try to run all in the same playbook it fails…

What do you mean “create the partition”? And can you explain “run all in the same playbook”?

this is my playbook:


  • name: RF vm creation test
    hosts: localhost
    gather_facts: no
    collections:

    • community.vmware

    vars_prompt:

    • name: “ansible_user”
      prompt: “Enter your vCenter username”
      private: no

    • name: “ansible_password”
      prompt: “Enter your vCenter password”
      private: yes

    tasks:

    • name: Create a new VM
      vmware_guest:
      hostname: “xxxx-xxx-xxxx-p01”
      username: “{{ ansible_user }}”
      password: “{{ ansible_password }}”
      validate_certs: no
      name: “RFtest”
      cluster: “ALL”
      datacenter: “xxxxxxx-PRD”
      folder: “/ALL”
      template: “Template_Rocky9-xxxxxxxxx-PRD”
      state: poweredon
      guest_id: “otherGuest64”
      disk:
      - size_gb: 20
      type: thin
      datastore: “xxxxxxxxxxx”
      - size_gb: 40
      type: thin
      datastore: “xxxxxxx”
      hardware:
      memory_mb: 1024
      num_cpus: 1
      scsi: paravirtual
      networks:
      - name: “VM Network”
      device_type: “vmxnet3”
      vlan: “xxxx_DC”
      ip: “10.xx.xx.52”
      netmask: “255.255.252.0”
      gateway: “10.xx.xx.253”
      dns_servers:
      - xxxxxxxx
      - xxxxxxxx
      wait_for_ip_address: yes
      customization:
      hostname: “RFtest”
      domain: “xxxxxxx.com

    • name: Wait for the VM to be ready
      wait_for:
      timeout: 30

    • name: Create a physical volume on /dev/sdb
      command: pvcreate /dev/sdb
      become: yes

    • name: Create a volume group named vg_data
      command: vgcreate vg_data /dev/sdb
      become: yes

    • name: Create a logical volume named lv_data
      command: lvcreate -l 100%FREE -n lv_data vg_data
      become: yes

    • name: Format the logical volume with ext4 filesystem
      filesystem:
      fstype: ext4
      dev: /dev/vg_data/lv_data
      become: yes

    • name: Create mount point
      file:
      path: /opt/timwe
      state: directory
      owner: root
      group: root
      mode: ‘0755’
      become: yes

    • name: Mount the logical volume to /opt/timwe
      mount:
      path: /opt/timwe
      src: /dev/vg_data/lv_data
      fstype: ext4
      state: mounted
      become: yes

    • name: Ensure the logical volume is mounted on reboot
      mount:
      path: /opt/timwe
      src: /dev/vg_data/lv_data
      fstype: ext4
      opts: defaults
      state: present
      become: yes