Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i need example on "postcustomization ansible vmware example" for collection vmware.vmware_rest.vcenter_vm_guest_customization #472

Closed
mani611 opened this issue Feb 9, 2024 · 3 comments
Assignees
Labels
documentation Improvements or additions to documentation has_pr

Comments

@mani611
Copy link

mani611 commented Feb 9, 2024

This is really a cool functionality, i would appreciate if you can provide the example of post customization script execution.
My current script look like this

- name: Customize the VM
  vmware.vmware_rest.vcenter_vm_guest_customization:
    vcenter_hostname: "{{ vcenter_hostname }}"
    vcenter_username: "{{ vcenter_username }}"
    vcenter_password: "{{ vcenter_password }}"
    vcenter_validate_certs : false
    vm: '{{ vm_info.id }}'
    configuration_spec:
      linux_config:
        domain: test
        hostname:
          fixed_name: test
          type: FIXED
        #script_text: |
        #  #!/bin/bash
        # yum update -y
    interfaces:
    - adapter:
        ipv4:
          type: STATIC
          gateways:
          - 1.1.11,1
          ip_address: 1.1.14.65
          prefix: 24
    global_DNS_settings:

need example how do i use post customization script. example is used in below code but not working. I believe example is missing in redhat documentation.

- name: Customize the VM
  vmware.vmware_rest.vcenter_vm_guest_customization:
    vcenter_hostname: "{{ vcenter_hostname }}"
    vcenter_username: "{{ vcenter_username }}"
    vcenter_password: "{{ vcenter_password }}"
    vcenter_validate_certs : false
    vm: '{{ vm_info.id }}'
    configuration_spec:
      linux_config:
        script_text: postcustomization
        #!/bin/sh
        if [ x$1 = x"precustomization" ]; then
            echo "Do Precustomization tasks"
        elif [ x$1 = x"postcustomization" ]; then
            echo "Do Postcustomization tasks"
            yum update -y
        fi
    interfaces:
    - adapter:
        ipv4:
          type: STATIC
          gateways:
          - 10,10,10,1
          ip_address: 10.10.10.13
          prefix: 24
    global_DNS_settings:
      dns_suffix_list: []
      dns_servers:
      - 10.10.10.10
  register: post_customization_info
- name: Print customization  status
  debug:
    var: post_customization_info
@machacekondra machacekondra added the documentation Improvements or additions to documentation label Mar 12, 2024
@oldguardmd
Copy link

oldguardmd commented Apr 10, 2024

I have a similar problem. No combination I have tried works. Seems like this might be a bug?

  - name: Test Native Ansible module
    vmware.vmware_rest.vcenter_vm_guest_customization:
      vcenter_hostname: "{{ vcenter_hostname }}"
      vcenter_password: "{{ lookup('ansible.builtin.env', 'ANSIBLE_PASSWORD') }}"
      vcenter_username: "{{ vcenter_user }}"
      vcenter_validate_certs: false  
      vm: "{{ cloud_init_vm_id.vm }}"
      configuration_spec:
        linux_config:
          domain: "something.com"
          hostname:
            fixed_name: "{{ current_vm.name }}"
            type: FIXED
      interfaces:
      - adapter:
          ipv4:
            type: "DHCP"
      global_DNS_settings:
        dns_suffix_list: [] 
        dns_servers:
        - 1.1.1.1

@mikemorency
Copy link
Collaborator

Hello, Ive opened a PR to update the documentation examples for this module. Heres the current version, please let me know your thoughts here or in the PR

##########
#
# VM customization can be difficult to troubleshoot, since each environment is different. Here are some general tips:
#
# 1. Make sure perl is installed on the Linux systems. Make sure cloud-init is installed if using cloud-init
# 2. Custom script execution is disabled by default. To enable it, you can run as root: vmware-toolbox-cmd config set deployPkg enable-custom-scripts  true
# 3. VMware tools must be installed and recognized by vCenter before you can apply customization. See the example below for one approach to this.
# 4. On Linux (RHEL specifically), customization script logs can be found at /var/log/vmware-imc/toolsDeployPkg.log
# 5. Once the VM is started, the pending customization is applied. Even if that fails, the customization is then cleared. Meaning, you need to re-apply
#    the customization spec in order to try again. Simply rebooting the VM will not change anything.
#
##########

# Heres the basic workflow for creating a new VM and then customizing it
- name: Deploy a new VM based on a template
  vmware.vmware_rest.vcenter_vmtemplate_libraryitems:
    name: vm-from-template
    library: '{{ library_id }}'
    template_library_item: '{{ template_id }}'
    placement:
      cluster: "{{ lookup('vmware.vmware_rest.cluster_moid', '/my_dc/host/my_cluster') }}"
    state: deploy
  register: my_new_vm

- name: Power on the VM to register VMware tools
  vmware.vmware_rest.vcenter_vm_power:
    state: start
    vm: "{{ my_new_vm.id }}"

- name: Wait until my VMware tools are recognized
  vmware.vmware_rest.vcenter_vm_tools_info:
    vm: "{{ my_new_vm.id }}"
  register: vm_tools_info
  until:
    - vm_tools_info is not failed
    - vm_tools_info.value.run_state == "RUNNING"
  retries: 60
  delay: 5

- name: Power Off VM
  vmware.vmware_rest.vcenter_vm_power:
    state: stop
    vm: "{{ my_new_vm.id }}"

- name: Customize the VM
    vmware.vmware_rest.vcenter_vm_guest_customization:
    vm: "{{ my_new_vm.id }}"
    global_DNS_settings:
      dns_suffix_list:
        - lan
        - foo.internal
      dns_servers:
        - "8.8.8.8"
    interfaces:
      - adapter:
          ipv4:
            type: DHCP
    configuration_spec:
      linux_config:
        domain: test
        hostname:
          fixed_name: myhost
          type: FIXED

# Heres an example using the Linux script text. The script shebang can be anything (bash, perl, python), so long as the script will actually run
# Theres also size and length limitation on the script text, as described in the module documentation.
# Finally, note the script is run twice. Once before all of the other customization and once after.
- name: Customize the VM
    vmware.vmware_rest.vcenter_vm_guest_customization:
    vm: "{{ my_new_vm.id }}"
    global_DNS_settings:
      dns_suffix_list:
        - lan
        - foo.internal
      dns_servers:
        - "8.8.8.8"
    interfaces:
      - adapter:
          ipv4:
            type: DHCP
    configuration_spec:
      linux_config:
        domain: test
        hostname:
          fixed_name: myhost
          type: FIXED
        script_text: |
          #!/bin/sh
          if [ x$1 == x"precustomization" ]; then
            echo "PRE" >> /tmp/vmware_rest_init_script.log
            # add any other pre-customization tasks here
          fi

          if [ x$1 == x"postcustomization" ]; then
            echo "POST" >> /tmp/vmware_rest_init_script.log
            # add any other post-customization tasks here
          fi


# Heres an example using cloud-init
# See also:
#   https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/data-structures/Guest_CloudinitConfiguration/
#   https://knowledge.broadcom.com/external/article/311895/how-to-customize-virtual-machine-using-c.html
#   https://cloudinit.readthedocs.io/en/latest/reference/examples.html

- name: Customize the VM
    vmware.vmware_rest.vcenter_vm_guest_customization:
    vm: "{{ my_new_vm.id }}"
    global_DNS_settings:
      dns_suffix_list: []
      dns_servers:
        - "8.8.8.8"
    interfaces:
      - adapter:
          ipv4:
          type: DHCP
    configuration_spec:
      cloud_config:
        type: CLOUDINIT
        cloudinit:
          metadata: |
            instance-id: cloud-vm-example-1
            local-hostname: cloud-vm
            network:
              config: disabled
          userdata: |
            #cloud-config
            disable_root: 0
            write_files:
              - content: |
                  This is a test
                path: /root/cloud-init-example

@sean-freeman
Copy link

FYI @mani611 @oldguardmd @machacekondra

@mikemorency This PR takes my provided code in #471 for cloud-init, but does not fix #471 and therefore is documenting a procedure that cannot possibly work for any end-user. It should be removed from the Ansible Collection documentation until the procedure is confirmed working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation has_pr
Projects
None yet
Development

No branches or pull requests

5 participants