OpenShift 4.x HOL教程汇总
本文在 Ansible 2.9 和 VMware vSphere 7 环境中进行验证。

创建虚拟机

---
- name: create vm
  hosts: localhost
  tasks: 
    - name: create vm from template
      vmware_guest:
        hostname: 192.168.201.31 # 指定vSphere
        username: AAAAA # 指定vSphere 具备创建vm的用户
        password: BBBBB # 指定密码
        validate_certs: no
        folder: /

        cluster: IM-VMLAB # 指定cluster
        datacenter: Datacenter  # datacenter 名称

        name: my-vm # 指定vm名称
        state: poweredon
        guest_id: rhel6Guest
        disk:
        - size_gb: 200
          type: thin
          autoselect_datastore: yes #根据模板自动选择datastore
        hardware:
          memory_mb: 512
          num_cpus: 1
          scsi: paravirtual 
        template: Ansible-Client01 # 指定vm模板名称
        wait_for_ip_address: True
        
      register: newvm

    - name: IP address info
      debug: 
        msg: "{{newvm}} ansible_test_name"

删除虚拟机

- name: delete vm
  hosts: localhost
  tasks:
    - name: get fact by vm name
      vmware_guest:
        hostname: 192.168.201.31 # 指定vSphere
        username: AAAAA # 指定vSphere 具备创建vm的用户
        password: BBBBB # 指定密码
        validate_certs: no
        datacenter: Datacenter #指定datacenter
        folder: /
        name: my-vm
      register: facts
      
    - name: Remove a virtual machine by uuid
      vmware_guest:
        hostname: 192.168.201.31 # 指定vSphere
        username: AAAAA # 指定vSphere 具备创建vm的用户
        password: BBBBB # 指定密码
        validate_certs: no
        uuid: "{{ facts.instance.hw_product_uuid }}"
        state: absent

启动虚拟机

- name: poweron vm
  hosts: localhost
  tasks:
    - name: poweron vm
      vmware_guest:
        hostname: 192.168.201.31 # 指定vSphere
        username: AAAAA # 指定vSphere 具备创建vm的用户
        password: BBBBB # 指定密码
        validate_certs: no
        datacenter: Datacenter #指定datacenter
        folder: /
        name: my-vm
        state: poweredon
      register: facts

关闭虚拟机

- name: poweroff vm
  hosts: localhost
  tasks:
    - name: poweroff vm
      vmware_guest:
        hostname: 192.168.201.31 # 指定vSphere
        username: AAAAA # 指定vSphere 具备创建vm的用户
        password: BBBBB # 指定密码
        validate_certs: no
        datacenter: Datacenter #指定datacenter
        folder: /
        name: my-vm
        state: poweredoff
        force: yes
      register: facts

获得虚拟机配置

---
- name: get specific facts from vm
  hosts: localhost
  tasks:
    - name: get facts from vm
      vmware_guest:
        hostname: 192.168.201.31 # 指定vSphere
        username: AAAAA # 指定vSphere 具备创建vm的用户
        password: BBBBB # 指定密码
        validate_certs: no
        datacenter: Datacenter #指定datacenter
        folder: /
        name: my-vm
      register: facts

    - name: show msg
      debug: msg="{{facts}}"

修改虚拟机配置

- name: change vm config
  hosts: localhost
  tasks:
    - name: change vm config
      vmware_guest:
        hostname: 192.168.201.31 # 指定vSphere
        username: AAAAA # 指定vSphere 具备创建vm的用户
        password: BBBBB # 指定密码
        validate_certs: no
        datacenter: Datacenter #指定datacenter
        folder: /
        name: my-vm
        state: present
        hardware:
          memory_mb: 1024
      register: facts

    - name: show msg
      debug: msg="{{facts}}"

修改宿主机主机名

- name: set vm hostname
  hosts: localhost
  tasks:
    - name: set vm hostname
      vmware_vm_shell:
        hostname: 192.168.201.31 # 指定vSphere
        username: AAAAA # 指定vSphere 具备创建vm的用户
        password: BBBBB # 指定密码
        validate_certs: no
        datacenter: Datacenter #指定datacenter
        folder: /
        vm_id: my-vm
        vm_username: root
        vm_password: password
        vm_shell: "/usr/bin/hostnamectl"
        vm_shell_args: "set-hostname new_hostname > /tmp/$$.txt 2>&1"

创建虚拟机快照

- name: create a snapshot for vm
  hosts: localhost
  tasks:
    - name: create a snapshot for vm
      vmware_guest_snapshot:
        hostname: 192.168.201.31 # 指定vSphere
        username: AAAAA # 指定vSphere 具备创建vm的用户
        password: BBBBB # 指定密码
        validate_certs: no
        datacenter: Datacenter #指定datacenter
        folder: /
        name: my-vm
        state: present
        snapshot_name: snap1
        description: snap1_description

删除虚拟机快照

- name: delete a vm snapshot
  hosts: localhost
  tasks:
    - name: delete a vm snapshot
      vmware_guest_snapshot:
        hostname: 192.168.201.31 # 指定vSphere
        username: AAAAA # 指定vSphere 具备创建vm的用户
        password: BBBBB # 指定密码
        validate_certs: no
        datacenter: Datacenter #指定datacenter
        folder: /
        name: my-vm
        state: absent
        snapshot_name: snap1
        description: snap1_description
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐