0
votes

I just started learning Ansible. It has been a pain so far. I have this code to create a new vm. I followed this tutorial.

 ---
- hosts: localhost
  connection: local
  tasks:
    - vsphere_guest:
            vcenter_hostname:1.1.1.12
            username: root
            password: pasword
            guest: newvm001
            state: powered_on
            validate_certs: no
            vm_extra_config:
                vcpu.hotadd: yes
                mem.hotadd:  yes
                notes: This is a test VM
                folder: MyFolder
            vm_disk:
                disk1:
                        size_gb: 10
                        type: thin
                        datastore: storage001
            vm_nic:
                nic1:
                    type: vmxnet3
                    network: VM Network
                    network_type: standard
            vm_hardware:
                memory_mb: 256
                num_cpus: 1
                osid: ubuntu64Guest
                scsi: paravirtual
            esxi:
                datacenter: 1.1.1.12
                hostname: 1.1.1.12

I however keep getting this error.

[WARNING]: Host file not found: /etc/ansible/hosts

[WARNING]: provided hosts list is empty, only localhost is available

PLAY [localhost]


TASK [setup] ******************************************************************* ok: [localhost]

TASK [vsphere_guest] *********************************************************** fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "Cannot find datacenter named: 9.1.142.86"}

NO MORE HOSTS LEFT ************************************************************* [WARNING]: Could not create retry file 'testing.retry'. [Errno 2] No such file or directory: ''

PLAY RECAP ********************************************************************* localhost : ok=1 changed=0 unreachable=0
failed=1

Why is that so? And what is the difference between a host file and an inventory file?

1
This is completely offtopic--but have you looked at Hashicorp's Packer? I find that using Packer to interface with VMs, and Ansible to install software locally on the VM once it's been provisioned, is a much simpler process to work with.Alan
Hello! Thanks for the recommendation. Can you send a good link or tutorial that explains how to provision vms using ansible and packer? I looked it up but got nothing really solid.user3796292

1 Answers

1
votes

what is the difference between a host file and an inventory file?

They are the same. However, since you're doing everything on your local machine, it's fine that you only have localhost available.

This is your error:

TASK [vsphere_guest] *********************************************************** fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "Cannot find datacenter named: 9.1.142.86"}

It's not clear to me why you're receiving this with the playbook you've provided, as it doesn't mention that IP at all and the line I suspect is causing the problem is

datacenter: 1.1.1.12

Are you sure this is the file you're running, and that you've saved any changes you've made to it?