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:
        hostname: 172.20.16.20   #vCenter的主机名或IP地址
        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}}"

本文分享自微信公众号 - 大魏分享(david-share)

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2018-04-04

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Kubernetes

原 荐 Flannel是如何工作的

Author: xidianwangtao@gmail.com 概述 最近我们的TaaS平台遇到很多的网络问题,事实证明“contiv + ovs + v...

1.9K110
来自专栏宋凯伦的技术小栈

【小结】IIS7下的Http Native Module开发

  今天接到Product Manager的通知,Exchange 2007环境下的Native Module不再需要开发(详情可见上篇),但最近几天一直在做P...

25590
来自专栏乐沙弥的世界

Linux/Unix shell 自动 FTP 备份档案

       使用shell脚本实现对Oracle数据库的监控与管理将大大简化DBA的工作负担,如常见的对实例的监控,监听的监控,告警日志的监控,以及数据库的备...

18310
来自专栏cs

万方数据库,文献下载的准备

 ###后来文献下载任务完成了 ,相关的代码,细节,介绍看这篇文章 https://www.jianshu.com/p/134530b46a65

61550
来自专栏杨建荣的学习笔记

GoldenGate简单复制环境的搭建(r10笔记第79天)

今天试了下搭建GoldenGate,搭建的过程也简单总结了一下。 目前源数据库是newtest2 目标数据库是dataguru 都是11.2.0.4.0版本...

29050
来自专栏liulun

CEF C++环境搭建

第一步:下载CEF 到这里下载最新版本的CEF http://cefbuilds.com/ 下载解压之后,大概会看到如下图所示的文件 cefclien...

33170
来自专栏Android源码框架分析

Android权限检查API checkSelfPermission失效问题为什么targetSdkVersion < 23 Context 的 checkSelfPermission失效target

84930
来自专栏技术小讲堂

LINQ to SQL(2):生成对象模型

在LINQ to SQL中,可以使用自己的编程语言的对象模型映射到关系数据库,在上一节课,已经有一部分内容,简单的介绍了一下这种对象模型的结构,这一节,我们主要...

29940
来自专栏杨建荣的学习笔记

10g,11g中的数据库克隆安装(r6笔记第7天)

有时候在很多工作环境中,如果彼此几个机器的配置相似,我们就可以不用一遍又一遍的安装数据库软件了,我们可以为了更快的完成安装工作,在静默安装,图形安装的选择之外,...

33880
来自专栏移动端周边技术扩展

iOS打开系统功能对应的URL

20730

扫码关注云+社区

领取腾讯云代金券