create/delete/poweron/poweroff multiple vms from inventory using template live kde get facts of ans-web01 vm
[root@localhost ~]# cat createvms
[web-vms]
ans-web[01:05] disk='10' datastore='datastore1' memory='256' cpucount='1' guest_id='rhel6Guest'
[root@localhost ~]#
[root@localhost ~]# cat zing1.yml
---
- hosts: all
gather_facts: false
connection: local
vars_prompt:
- name: "vsphere_password"
prompt: "vSphere Password"
- name: "notes"
prompt: "VM notes"
private: no
default: "Deployed with ansible"
tasks:
# get date
# - set_fact: creationdate="{{lookup('pipe','date "+%Y/%m/%d %H:%M"')}}"
# Create a VM from a template
- name: create the VM
vmware_guest:
hostname: 192.168.0.107
username: root
password: vmware
validate_certs: no
esxi_hostname: 192.168.0.108
datacenter: dc
folder: /dc/vm
name: '{{ inventory_hostname }}'
state: poweredon
guest_id: rhel7Guest
# annotation: "{{ notes }} - {{ creationdate }}"
disk:
- size_gb: 10
type: thin
datastore: datastore1
hardware:
memory_mb: 256
num_cpus: 1
scsi: paravirtual
hostname: '{{ inventory_hostname }}'
template: livekde
wait_for_ip_address: True
[root@localhost ~]# ansible-playbook -i createvms zing1.yml
[DEPRECATION WARNING]: The TRANSFORM_INVALID_GROUP_CHARS settings is set to allow bad
characters in group names by default, this will change, but still be user configurable
on deprecation. This feature will be removed in version 2.10. Deprecation warnings
can be disabled by setting deprecation_warnings=False in ansible.cfg.
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv
to see details
vSphere Password:
VM notes [Deployed with ansible]:
PLAY [all] ****************************************************************************
TASK [create the VM] ******************************************************************
changed: [ans-web01]
changed: [ans-web03]
changed: [ans-web05]
changed: [ans-web02]
changed: [ans-web04]
PLAY RECAP ****************************************************************************
ans-web01 : ok=0 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ans-web02 : ok=0 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ans-web03 : ok=0 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ans-web04 : ok=0 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ans-web05 : ok=0 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
poeroffvms
[root@localhost ~]# vi zing1.yml
[root@localhost ~]# ansible-playbook -i createvms zing1.yml
[DEPRECATION WARNING]: The TRANSFORM_INVALID_GROUP_CHARS settings is set to allow bad
characters in group names by default, this will change, but still be user configurable
on deprecation. This feature will be removed in version 2.10. Deprecation warnings
can be disabled by setting deprecation_warnings=False in ansible.cfg.
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv
to see details
vSphere Password:
VM notes [Deployed with ansible]:
PLAY [all] ****************************************************************************
TASK [create the VM] ******************************************************************
ok: [ans-web01]
ok: [ans-web03]
ok: [ans-web05]
ok: [ans-web02]
ok: [ans-web04]
PLAY RECAP ****************************************************************************
ans-web01 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ans-web02 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ans-web03 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ans-web04 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ans-web05 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@localhost ~]#
deletevms where state: absent
[root@localhost ~]# cat zing1.yml
---
- hosts: all
gather_facts: false
connection: local
vars_prompt:
- name: "vsphere_password"
prompt: "vSphere Password"
- name: "notes"
prompt: "VM notes"
private: no
default: "Deployed with ansible"
tasks:
# get date
# - set_fact: creationdate="{{lookup('pipe','date "+%Y/%m/%d %H:%M"')}}"
# Create a VM from a template
- name: delete the VM
vmware_guest:
hostname: 192.168.0.107
username: root
password: vmware
validate_certs: no
esxi_hostname: 192.168.0.108
datacenter: dc
folder: /dc/vm
name: '{{ inventory_hostname }}'
state: absent
guest_id: rhel6Guest
# annotation: "{{ notes }} - {{ creationdate }}"
disk:
- size_gb: 10
type: thin
datastore: datastore1
hardware:
memory_mb: 256
num_cpus: 1
scsi: paravirtual
hostname: '{{ inventory_hostname }}'
template: livekde
wait_for_ip_address: True
[root@localhost ~]# ansible-playbook -i createvms zing1.yml
vSphere Password:
VM notes [Deployed with ansible]:
PLAY [all] ****************************************************************************
TASK [delete the VM] ******************************************************************
changed: [ans-web05]
changed: [ans-web01]
changed: [ans-web02]
changed: [ans-web03]
changed: [ans-web04]
PLAY RECAP ****************************************************************************
ans-web01 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ans-web02 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ans-web03 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ans-web04 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ans-web05 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@localhost ~]#
[root@localhost ~]# cat vcentervm.yml
- hosts: localhost
connection: local
tasks:
- name: get facts from vcenter
vsphere_guest:
vcenter_hostname: 192.168.0.107
username: root
password: vmware
guest: ans-web01
vmware_guest_facts: yes #Gather facts from vCenter on a particular VM
validate_certs: no
register: facts
- name: show msg
debug: msg="{{facts}}"
[root@localhost ~]#
[root@localhost ~]# ansible-playbook vcentervm.yml
[DEPRECATION WARNING]: vsphere_guest is kept for backwards compatibility but
usage is discouraged. The module documentation details page may explain more
about this rationale.. This feature will be removed in a future release.
Deprecation warnings can be disabled by setting deprecation_warnings=False in
ansible.cfg.
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [get facts from vcenter] **************************************************
[DEPRECATION WARNING]: The 'vsphere_guest' module has been deprecated. Use
'********_guest' instead.. This feature will be removed in version 2.9.
Deprecation warnings can be disabled by setting deprecation_warnings=False in
ansible.cfg.
ok: [localhost]
TASK [show msg] ****************************************************************
ok: [localhost] => {
"msg": {
"ansible_facts": {
"hw_eth0": {
"addresstype": "assigned",
"ipaddresses": [
"192.168.0.106",
"fe80::1dc9:880a:7f1d:bb9c"
],
"label": "Network adapter 1",
"macaddress": "00:50:56:93:91:a5",
"macaddress_dash": "00-50-56-93-91-a5",
"summary": "VM Network"
},
"hw_guest_full_name": "Red Hat Enterprise Linux 6 (32-bit)",
"hw_guest_id": "rhel6Guest",
"hw_instance_uuid": "50132f40-9260-e248-70ef-5cc9711506ce",
"hw_interfaces": [
"eth0"
],
"hw_memtotal_mb": 1024,
"hw_name": "ans-web01",
"hw_power_status": "POWERED ON",
"hw_processor_count": 1,
"hw_product_uuid": "4213f781-030d-0024-a91a-ed3905357820",
"module_hw": true
},
"changed": false,
"deprecations": [
{
"msg": "The 'vsphere_guest' module has been deprecated. Use '********_guest' instead.",
"version": 2.9
}
],
"failed": false
}
}
PLAY RECAP *********************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Comments
Post a Comment