Install new VM with Foreman 2.1 on VMware

Problem:
I want to use Foreman 2.1 to create new VMs in a VMware site by installing from scratch (not using a template). What is the process?

Foreman and Proxy versions:
Foreman 2.1

Foreman and Proxy plugin versions:
theforeman.foreman 1.3.0

Other relevant data:
This is my ansible to create the host:

---
- name: Create a VM
  hosts: localhost
  gather_facts: false

  tasks:
    - theforeman.foreman.host:
        username: "admin"
        password: "password"
        server_url: "https://192.168.33.183"
        validate_certs: false
        name: 'new-host1.demo.com'
        hostgroup: 'CentOS 8 VMs'
        organization: 'Default Organization'
        location: 'Default Location'
        compute_attributes:
          cpus: 2
          memory_mb: 4096
          start: "1"
        managed: false
        state: present
...

But the host new-host1.demo.com does not appear in vSphere.

1 Like

Does the same work when done from the UI? You’re not providing a compute resource or compute profile, so I guess those are supposed to be inherited from the host group?
What happens if you explicitly pass build:true?

1 Like

A) Doesn’t work from the UI either - pointers?
B) setting build: true makes it require the MAC address etc. (see below)
How do I get Foreman to build a new VM without providing a MAC address?

[WARNING]: when 'build'=True, 'managed' is ignored and forced to True
fatal: [localhost]: FAILED! => {
"ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"error": {
    "errors": {
        "interfaces.mac": [
            "can't be blank"
        ]
    },
    "full_messages": [
        "Mac can't be blank"
    ],
    "id": null
},
"invocation": {
    "module_args": {
        "architecture": null,
        "build": true,
        "comment": null,
        "compute_attributes": {
            "cpus": 2,
            "memory_mb": 4096,
            "start": "1"
        },
        "compute_profile": null,
        "compute_resource": null,
        "config_groups": null,
        "content_source": null,
        "content_view": null,
        "domain": null,
        "enabled": null,
        "environment": null,
        "hostgroup": "CentOS 8 VMs",
        "image": null,
        "ip": null,
        "kickstart_repository": null,
        "lifecycle_environment": null,
        "location": "Default Location",
        "mac": null,
        "managed": false,
        "medium": null,
        "name": "new-host2.demo.com",
        "openscap_proxy": null,
        "operatingsystem": null,
        "organization": "Default Organization",
        "owner": null,
        "owner_group": null,
        "parameters": null,
        "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
        "provision_method": null,
        "ptable": null,
        "puppet_ca_proxy": null,
        "puppet_proxy": null,
        "puppetclasses": null,
        "pxe_loader": null,
        "realm": null,
        "root_pass": null,
        "server_url": "https://192.168.33.183",
        "state": "present",
        "subnet": null,
        "subnet6": null,
        "username": "admin",
        "validate_certs": false
    }
},
"msg": "Error while performing create on hosts: 422 Client Error: Unprocessable Entity"
}

this should be resolved by Bug #30822: MAC address field is active when creating host with set by hostgroup compute resource - Foreman and Feature #30627: Add error message when mac address is provided by CR and it's set - Foreman in 2.3, in the meantime i think you can just provide a fake mac address which will be overriden by the compute resource.

Not really, I am not too familiar with the compute resource integration, but this at least indicates an error outside the Ansible modules.

All solved:

  • Defined VMware as a compute resource
  • Created compute profile
  • Was able to create VM from Foreman GUI
  • Ansible now works fine
1 Like

That’s great @eddyr

There are some docs here for future reference that might help:

https://docs.theforeman.org/master/Provisioning_Guide/index-foreman.html#Provisioning_Virtual_Machines_in_VMware_vSphere

Yes, that’s what I looked at.

1 Like

Hello Eddyr,

Please provided me that ansible playbook for me.

Thanks,
Mahesh

---
- name: Create a VM
  hosts: localhost
  gather_facts: false

  tasks:
    - theforeman.foreman.host:
        username: "admin"
        password: "password"
        server_url: "https://192.168.33.183"
        validate_certs: false
        name: "{{ vm_name }}"
        hostgroup: 'CentOS 8 VMs'
        organization: 'Default Organization'
        location: 'Default Location'
        compute_attributes:
          cpus: 2
          memory_mb: 4096
          start: "1"
        mac: 00:50:56:ef:3e:55
        managed: true
        build: true
        state: present
...

Thank you for provided the playbook.