Ansible配置管理vCenter及相关虚拟机

Posted 大魏分享

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ansible配置管理vCenter及相关虚拟机相关的知识,希望对你有一定的参考价值。

Ansible支持的模块:

http://docs.ansible.com/ansible/latest/modules/modules_by_category.html


对VMware的支持:

Vmware

  • vca_fw – add remove firewall rules in a gateway in a vca

  • vca_nat – add remove nat rules in a gateway in a vca

  • vca_vapp – Manages vCloud Air vApp instances.

  • vcenter_folder – Manage folders on given datacenter

  • vcenter_license – Manage VMware vCenter license keys

  • vmware_cfg_backup – Backup / Restore / Reset ESXi host configuration

  • vmware_cluster – Manage VMware vSphere clusters

  • vmware_datacenter – Manage VMware vSphere Datacenters

  • vmware_datastore_facts – Gather facts about datastores

  • vmware_dns_config – Manage VMware ESXi DNS Configuration

  • vmware_drs_rule_facts – Gathers facts about DRS rule on the given cluster

  • vmware_dvs_host – Add or remove a host from distributed virtual switch

  • vmware_dvs_portgroup – Create or remove a Distributed vSwitch portgroup.

  • vmware_dvswitch – Create or remove a distributed vSwitch

  • vmware_guest – Manages virtual machines in vCenter

  • vmware_guest_facts – Gather facts about a single VM

  • vmware_guest_file_operation – Files operation in a VMware guest operating system without network

  • vmware_guest_find – Find the folder path(s) for a virtual machine by name or UUID

  • vmware_guest_powerstate – Manages power states of virtual machines in vCenter

  • vmware_guest_snapshot – Manages virtual machines snapshots in vCenter

  • vmware_guest_tools_wait – Wait for VMware tools to become available

  • vmware_host – Add / Remove ESXi host to / from vCenter

  • vmware_host_acceptance – Manage acceptance level of ESXi host

  • vmware_host_config_facts – Gathers facts about an ESXi host’s advance configuration information

  • vmware_host_config_manager – Manage advance configurations about an ESXi host

  • vmware_host_datastore – Manage a datastore on ESXi host

  • vmware_host_dns_facts – Gathers facts about an ESXi host’s DNS configuration information

  • vmware_host_facts – Gathers facts about remote vmware host

  • vmware_host_firewall_facts – Gathers facts about an ESXi host’s firewall configuration information

  • vmware_host_firewall_manager – Manage firewall configurations about an ESXi host

  • vmware_host_lockdown – Manage administrator permission for the local administrative account for the ESXi host

  • vmware_host_ntp – Manage NTP configurations about an ESXi host

  • vmware_host_package_facts – Gathers facts about available packages on an ESXi host

  • vmware_host_service_facts – Gathers facts about an ESXi host’s services

  • vmware_host_service_manager – Manage services on a given ESXi host

  • vmware_host_vmnic_facts – Gathers facts about vmnics available on the given ESXi host

  • vmware_local_role_manager – Manage local roles on an ESXi host

  • vmware_local_user_manager – Manage local users on an ESXi host

  • vmware_maintenancemode – Place a host into maintenance mode

  • vmware_migrate_vmk – Migrate a VMK interface from VSS to VDS

  • vmware_portgroup – Create a VMware portgroup

  • vmware_resource_pool – Add/remove resource pools to/from vCenter

  • vmware_target_canonical_facts – Return canonical (NAA) from an ESXi host

  • vmware_vm_facts – Return basic facts pertaining to a vSphere virtual machine guest

  • vmware_vm_shell – Run commands in a VMware guest operating system

  • vmware_vm_vm_drs_rule – Configure VMware DRS Affinity rule for virtual machine in given cluster

  • vmware_vm_vss_dvs_migrate – Migrates a virtual machine from a standard vswitch to distributed

  • vmware_vmkernel – Manage a VMware VMkernel Interface aka. Virtual NICs of host system.

  • vmware_vmkernel_facts – Gathers VMKernel facts about an ESXi host

  • vmware_vmkernel_ip_config – Configure the VMkernel IP Address

  • vmware_vmotion – Move a virtual machine using vMotion

  • vmware_vsan_cluster – Configure VSAN clustering on an ESXi host

  • vmware_vswitch – Add or remove a VMware Standard Switch to an ESXi host

  • vsphere_copy – Copy a file to a vCenter datastore

  • vsphere_guest **(D)** – Create/delete/manage a guest VM through VMware vSphere.


下面,我们列出8个案例,展示Ansible对VMware的管理。

1. 获取vCenter整体信息

---

-  name: vmware facts from vCenter

  hosts: localhost        #指定运行playbook的服务器,一般设置为localhost

  tasks:

    - name: get all registered vms

      vmware_vm_facts:

        username: administrator@vsphere.local  #vCenter登录名称

        password: P@ssw0rd  #vCenter登录密码

        validate_certs: False  #关闭证书认证

      register: vmfacts

 

    - name: print facts

      debug:

        msg: "{{vmfacts}}"

在vmfacts里面显示从vCenter中获取的所有配置相关信息。其中vmfacts返回json格式,可以根据需要过滤、筛选特定的信息。



2.通过vCenter获取单个虚拟机配置信息

---

-  name: get specific facts from vcenter

  hosts: localhost

  connection: local

  tasks:

    - name: get facts from vcenter

      vsphere_guest:

        vcenter_hostname: 172.20.16.20

        username: administrator@vsphere.local

        password: P@ssw0rd

        guest: ansible_test_name #想要获取信息的虚拟机名称

        vmware_guest_facts: yes #Gather facts  from vCenter on a particular VM

        validate_certs: no

      register: facts

 

    - name: show msg

      debug: msg="{{facts}}"


3.通过vCenter在指定ESXi创建虚拟机

[root@localhost]#  cat 0402-vmware-create.yml

---

-  hosts: localhost

  vars_files:

    - vmware-info.yml

  tasks:

    - name: create vm from template

      vmware_guest:

        hostname:  "{{vcenter_host}}"

        username:  "{{vcenter_username}}"

        password:  "{{vcenter_password}}"

        validate_certs: no

        folder: /VMTEST

 

        esxi_hostname: rpo35.bxcs.com #指定esxi_name

        datacenter: DateCenter #指定datacenter

 

        name: ansible_test_name

        state: poweredon

        guest_id: rhel6Guest #VMware指定guest 类型,需要和https://www.vmware.com/support/developer/converter-sdk/conv55_apireference/vim.vm.GuestOsDescriptor.GuestOsIdentifier.html保持一致。

        disk:

        - size_gb: 200

          type: thin

          datastore: BXCS_vSphere_01 #指定特定datastore

        hardware:

          memory_mb: 512

          num_cpus: 1

          scsi: paravirtual

        template: RHEL6.4-STP # 指定特定模板名称

        wait_for_ip_address: True # 等待虚拟机成功启动后继续       

      register: newvm

 

    - name: IP address info

      debug:

        msg: "{{newvm}}  ansible_test_name"


4.通过vCenter在Cluster创建虚拟机

[root@localhost]#  cat 0410-vmware-create.yml

---

-  hosts: localhost

  vars_files:

    - vmware-info.yml

  tasks:

    - name: create vm from template

      vmware_guest:

        hostname:  "{{vcenter_host}}"

        username:  "{{vcenter_username}}"

        password:  "{{vcenter_password}}"

        validate_certs: no

        folder: /VMTEST

 

        cluster: vRealize-Cluster02 # 指定cluster

        datacenter: DateCenter #datacenter

 

        name: ansible_test_name_01

        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-TP01

        wait_for_ip_address: True

       

      register: newvm

 

    - name: IP address info

      debug:

        msg: "{{newvm}}  ansible_test_name"

5.通过vCenter在datastore cluster创建虚拟机

[root@localhost]#  cat check-rpm-info.yml

---

-  hosts: localhost

  vars_files:

    - vmware-info.yml

  tasks:

    - name: create vm from template

      vmware_guest:

        hostname:  "{{vcenter_host}}"

        username:  "{{vcenter_username}}"

        password:  "{{vcenter_password}}"

        validate_certs: no

        folder: /VMTEST

 

        esxi_hostname: rpo35.bxcs.com  #esxi_name

        datacenter: DateCenter #datacenter

 

        name: ansible_test_name_01

        state: poweredon

        guest_id: rhel6Guest

        disk:

        - size_gb: 200

          type: thin

          datastore: BXCS_vSphere_Cluster  #指定datastore cluster

        hardware:

          memory_mb: 512

          num_cpus: 1

          scsi: paravirtual

        template: ANSIBLE-TP01

        wait_for_ip_address: True

       

      register: newvm

 

    - name: IP address info

      debug:

        msg: "{{newvm}}  ansible_test_name"


6. 通过vCenter修改创建虚拟机的各种配置信息

[root@localhost]#  cat 0403-vmware-shell-ip-change.yml

---

-  hosts: localhost

  vars_files:

    - vmware-info.yml

  vars:

    vmname: "ansible_test_name"

    os_user: "root"

    os_pass: "123456"

  tasks:

    - name: change vmware via shell

      vmware_vm_shell:

        hostname:  "{{vcenter_host}}"

        username:  "{{vcenter_username}}"

        password:  "{{vcenter_password}}"

        validate_certs: no

        datacenter: DateCenter

 

        vm_id: "{{vmname}}"

        vm_username: "{{os_user}}"

        vm_password: "{{os_pass}}"

 

        vm_shell: /bin/echo

        vm_shell_args: "$VAR >  ifcfg-eth1"

        vm_shell_env:

          - "PATH=/bin"

          - 'VAR= -e

           DEVICE=eth1\nNAME=eth1\nONBOOT=yes\nBOOTPROTO=static\nIPADDR=172.20.6.150\nPREFIX=24\nGATEWAY=172.20.6.254

          '

        vm_shell_cwd:  "/etc/sysconfig/network-scripts/"

      register: vmfacts

 

    - name: restart network serviceof vm from  vcenter

      vmware_vm_shell:

        hostname:  "{{vcenter_host}}"

        username:  "{{vcenter_username}}"

        password:  "{{vcenter_password}}"

        validate_certs: no

        datacenter: DateCenter

 

        vm_id: "{{vmname}}"

        vm_username: "{{os_user}}"

        vm_password: "{{os_pass}}"

 

        vm_shell: /sbin/service

        vm_shell_args: "network  restart"

        vm_shell_env:

          - "PATH=/bin"

        vm_shell_cwd: "/tmp"

 

    - debug:

        msg: "{{vmfacts}}"

通过vm_tools将任意shell脚本注入到VMware虚拟机中,可灵活配置任意操作。


7. 通过vCenter关闭虚拟机

[root@localhost]#  cat 0404-vmware-poweroff.yml

---

-  hosts: localhost

  vars_files:

    - vmware-info.yml

  tasks:

    - name: poweroff vm

      vmware_guest:

        hostname:  "{{vcenter_host}}"

        username:  "{{vcenter_username}}"

        password:  "{{vcenter_password}}"

        validate_certs: no

        uuid:  "422746a9-f303-fa93-aed4-0bfa655b507f" # 待关闭虚拟机UUID

        name: "ansible_test_name" #待关闭虚拟机名称

        state: poweredoff

        force: yes

      delegate_to: localhost

可以实现对虚拟机如下的操作:present、absent、poweredon、poweredoff、restarted、suspended、shutdownguest、rebootguest。


8.通过vCenter创建删除管理snapshot信息

[root@localhost]#  cat 0405-vmware-snapshot-create.yml

---

-  hosts: localhost

  vars_files:

    - vmware-info.yml

  vars:

    vmname: "ansible_test_name"

    os_user: "root"

    os_pass: "123456"

  tasks:

    - name: create snapshot

      vmware_guest_snapshot:

        hostname:  "{{vcenter_host}}"

        username:  "{{vcenter_username}}"

        password:  "{{vcenter_password}}"

        validate_certs: no

        datacenter: DateCenter

        folder: /VMTEST

 

        name: ansible_test_name

        state: present

        snapshot_name:  ansible_test_snapshot_01

      register: result

 

    - name: show result

      shell: echo "{{result}}"

[root@localhost]#  cat 0405-vmware-snapshot-create.yml

---

-  hosts: localhost

  vars_files:

    - vmware-info.yml

  vars:

    vmname: "ansible_test_name"

    os_user: "root"

    os_pass: "123456"

  tasks:

    - name: create snapshot

      vmware_guest_snapshot:

        hostname:  "{{vcenter_host}}"

        username:  "{{vcenter_username}}"

        password:  "{{vcenter_password}}"

        validate_certs: no

        datacenter: DateCenter

        folder: /VMTEST

 

        name: ansible_test_name

        state: absent

        snapshot_name: ansible_test_snapshot_01

      register: result

 

    - name: show result

      shell: echo "{{result}}"

大魏分享:

魏新宇

"大魏分享"运营者、红帽资深解决方案架构师

专注开源云计算、容器及自动化运维在金融行业的推广

拥有红帽RHCE/RHCA、VMware VCP-DCV、VCP-DT、VCP-Network、VCP-Cloud、ITIL V3、Cobit5、C-STAR、AIX、HPUX等相关认证。


以上是关于Ansible配置管理vCenter及相关虚拟机的主要内容,如果未能解决你的问题,请参考以下文章

VMware vCenter 6.5 安装及群集配置介绍

vcenter6.0可以管理多少虚拟机

PowerCLi 通过vCenter批量创建及管理虚拟交换机到所有esxi主机

vmware vcenter server数据库怎么配置

vCenter Server 相关介绍

vmware-vcenter-server ova怎么安装