Ansible支持的模块:html
http://docs.ansible.com/ansible/latest/modules/modules_by_category.htmlshell
对VMware的支持:json
Vmware
vca_fw – add remove firewall rules in a gateway in a vcavim
vca_nat – add remove nat rules in a gateway in a vcaapi
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 configurationapp
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}}" |
大魏分享:
魏新宇
"大魏分享"运营者、红帽资深解决方案架构师
专一开源云计算、容器及自动化运维在金融行业的推广
拥有红帽RHCE/RHCA、VMware VCP-DCV、VCP-DT、VCP-Network、VCP-Cloud、ITIL V三、Cobit五、C-STAR、AIX、HPUX等相关认证。
本文分享自微信公众号 - 大魏分享(david-share)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。