create vms from inventory createvms from template livekde

 cat createvms

ans-db[01:02] disk='10' datastore='datastore1'  memory='256' cpucount='1' guest_id='rhel6Guest'
ans-nginx[01:02] disk='10' datastore='datastore1'  memory='256' cpucount='1' guest_id='rhel6Guest'
[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: poweredoff
      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
[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-nginx02]
changed: [ans-nginx01]
changed: [ans-db01]
changed: [ans-db02]

PLAY RECAP *********************************************************************
ans-db01                   : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ans-db02                   : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ans-nginx01                : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ans-nginx02                : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
\
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# cat vspherefacts.yml
---
- hosts: localhost
  tasks:
    - name: get all registered vms
      vmware_vm_facts:
        hostname: 192.168.0.108
        username: root
        password: vgskiran
        validate_certs: False
      register: vmfacts
    - name: print facts
      debug:
        msg: "{{vmfacts}}"
[root@localhost ~]# ansible-playbook vspherefacts.yml

PLAY [localhost] *******************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [localhost]

TASK [get all registered vms] ******************************************************************************************************************************************
ok: [localhost]

TASK [print facts] *****************************************************************************************************************************************************
ok: [localhost] => {
    "msg": {
        "changed": false,
        "failed": false,
        "virtual_machines": [
            {
                "attributes": {},
                "cluster": null,
                "esxi_hostname": "localhost.localdomain",
                "guest_fullname": "Red Hat Enterprise Linux 6 (32-bit)",
                "guest_name": "ans-nginx02",
                "ip_address": "192.168.0.110",
                "mac_address": [
                    "00:50:56:93:03:58"
                ],
                "power_state": "poweredOn",
                "uuid": "42136f70-914b-4f1f-056f-a063e24519af",
                "vm_network": {
                    "00:50:56:93:03:58": {
                        "ipv4": [
                            "192.168.0.110"
                        ],
                        "ipv6": [
                            "fe80::7698:1c63:1967:6ed6"
                        ]
                    }
                }
            },
            {
                "attributes": {},
                "cluster": null,
                "esxi_hostname": "localhost.localdomain",
                "guest_fullname": "Red Hat Enterprise Linux 6 (32-bit)",
                "guest_name": "ans-db01",
                "ip_address": "",
                "mac_address": [
                    "00:50:56:93:d2:93"
                ],
                "power_state": "poweredOn",
                "uuid": "421333cc-5a15-9248-c0f4-852a1099878b",
                "vm_network": {
                    "00:50:56:93:d2:93": {
                        "ipv4": [],
                        "ipv6": []
                    }
                }
            },
            {
                "attributes": {},
                "cluster": null,
                "esxi_hostname": "localhost.localdomain",
                "guest_fullname": "Red Hat Enterprise Linux 6 (32-bit)",
                "guest_name": "ans-nginx01",
                "ip_address": "192.168.0.104",
                "mac_address": [
                    "00:50:56:93:ca:91"
                ],
                "power_state": "poweredOn",
                "uuid": "42137010-0489-3a76-a4d7-78de34cfc8ca",
                "vm_network": {
                    "00:50:56:93:ca:91": {
                        "ipv4": [
                            "192.168.0.104"
                        ],
                        "ipv6": [
                            "fe80::5146:b386:6ebf:279"
                        ]
                    }
                }
            },
            {
                "attributes": {},
                "cluster": null,
                "esxi_hostname": "localhost.localdomain",
                "guest_fullname": "CentOS 4/5/6/7 (64-bit)",
                "guest_name": "New Virtual Machine",
                "ip_address": "",
                "mac_address": [
                    "00:50:56:93:ad:b7"
                ],
                "power_state": "poweredOff",
                "uuid": "42134273-c263-57cb-b5f9-26471cf17a95",
                "vm_network": {}
            },
            {
                "attributes": {},
                "cluster": null,
                "esxi_hostname": "localhost.localdomain",
                "guest_fullname": "CentOS 4/5/6/7 (64-bit)",
                "guest_name": "livekde1",
                "ip_address": "",
                "mac_address": [
                    "00:50:56:93:0e:78"
                ],
                "power_state": "poweredOff",
                "uuid": "4213337b-5512-ae38-345b-520338279cbc",
                "vm_network": {}
            },
            {
                "attributes": {},
                "cluster": null,
                "esxi_hostname": "localhost.localdomain",
                "guest_fullname": "Red Hat Enterprise Linux 6 (32-bit)",
                "guest_name": "ans-db02",
                "ip_address": "",
                "mac_address": [
                    "00:50:56:93:68:83"
                ],
                "power_state": "poweredOn",
                "uuid": "421321d8-e1ea-394c-1bb5-bb29fc4c7034",
                "vm_network": {
                    "00:50:56:93:68:83": {
                        "ipv4": [],
                        "ipv6": []
                    }
                }
            }
        ]
    }
}

PLAY RECAP *************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[root@localhost ~]# ansible-playbook vspherefacts.yml | grep 192
                "ip_address": "192.168.0.110",
                            "192.168.0.110"
                "ip_address": "192.168.0.104",
                            "192.168.0.104"

Comments