# Sample workflow - Automatically adding a host to an external VMWare VCenter This example shows how to create a custom workflow in order to add a host to a vmware vcenter system. We will be using the CLI for this and some familiarity with it is required. Please follow [Using the CLI guide](/content/overview/installation/using_the_cli) to setup the CLI. :::{Warning} Tested using VMWare vCenter 6.7.0 and might not work with future versions of the API ::: Documentation for vCenter REST API is available [here](https://vdc-download.vmware.com/vmwb-repository/dcr-public/423e512d-dda1-496f-9de3-851c28ca0814/0e3f6e0d-8d05-4f0c-887b-3d75d981bae5/VMware-vSphere-Automation-SDK-REST-6.7.0/docs/apidocs/index.html). We're calling [host create](https://vdc-download.vmware.com/vmwb-repository/dcr-public/423e512d-dda1-496f-9de3-851c28ca0814/0e3f6e0d-8d05-4f0c-887b-3d75d981bae5/VMware-vSphere-Automation-SDK-REST-6.7.0/docs/apidocs/operations/com/vmware/vcenter/host.create-operation.html). ## Creating a Task for enroll into vCenter 1. Create a variable for the host of the vcenter ```bash echo "https://172.17.108.75/" | metalcloud-cli variable create --name vcenter --pipe ``` 2. Create a secret for the credentials ```bash echo "Basic YWRtaW5pc3RyYXRvckB2c3BoZXJlLmxvY2FsOkRlbGxSMGNrcyE=" | metalcloud-cli secret create --name vcenter-creds --pipe ``` 3. Create a stage definition to login ```bash metalcloud-cli stage-definition create --label "vcenter-login" --title "vcenter-login" --type "HTTPRequest" --http-request-method GET --http_request_url='{{vcenter}}/rest/com/vmware/cis/session' --vars "vcenter" ``` 4. Create a stage definition to add the host to the vmware. Notice the IP obtained from the context `{{vmware_ipv4_address_0_0}}` ```bash echo '{{vmware_ipv4_address_0_0}}' | ./metalcloud-cli stage-definition create --label "vcenter-host-add" --title "vcenter-host-add" --type "HTTPRequest" --http-request-method GET --http-request-url='{{vcenter}}/rest/vcenter/host' --http-request-header-authorization '{{vcenter_creds}}' --vars "vcenter,vcenter_creds,vmware_ipv4_0" --http-request-body-from-pipe ``` ## Create a workflow ```bash $ metalcloud-cli workflow create --label "vmware" --title "Add host to vCenter" --usage infrastructure ``` ## Add stages to workflow ```bash $ metalcloud-cli stage-definition add-to-workflow --id vcenter-login --workflow vmware --runlevel 1 $ metalcloud-cli stage-definition add-to-workflow --id 'vcenter-host-add' --workflow vmware --runlevel 2 ``` ## Verify the stages ```bash metalcloud-cli workflow list --id wmware Stage Definitions: +-------+---------------+------------------+-------------+----------+---------------------+ | ID | INFRASTRUCTRE | STAGE | TYPE | RUNLEVEL | OUTPUT | +-------+---------------+------------------+-------------+----------+---------------------+ | 816 | vmware | vcenter-login | post_deploy | 1 | | | 818 | vmware | vcenter-host-add | post_deploy | 2 | | +-------+---------------+------------------+-------------+----------+---------------------+ Total: 2 ``` ## Add workflow to infrastructure We add the workflow to the infrastructure using a reference stage definition which instructs the system to call a workflow. ```bash metalcloud-cli stage-definition create --title "ref VMWARE" --label refvmware --type WorkflowReference --workflow add-host-to-vcenter ``` ## Deploy infrastructure ```bash metalcloud-cli infrastructure deploy --id vmware --autoconfirm ```