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源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。