Hi Kai

sorry, I just did a bad copy and paste; in reality my playbook is correct
about spaces and indentation in fact as I wrote (I posted the complete
output) it works using the "loop" statement but, in this way, I'm not able
to filter by vm name.
Instead using:
- debug: msg="{{
vm_guest_facts.virtual_machines['testvm'].mac_address }}"

the playbook gives me an error.

This is my playbook:


# Deploy a guest from a template*
- hosts: 127.0.0.1
  vars:
    vcenter_hostname: virtvcsami1.virtlab.local
    vcenter_user: [email protected]
    vcenter_pass: Esxilab!1
    vcenter_datastore: vsanDatastore
    vcenter_datacenter: DatacenterMI
  tasks:
  - name: Gather VMware guest facts
    vmware_vm_facts:
      validate_certs: False
      hostname: "{{ vcenter_hostname }}"
      username: "{{ vcenter_user }}"
      password: "{{ vcenter_pass }}"
      vm_type: vm
    delegate_to: localhost
    register: vm_guest_facts

#  - debug:  msg="{{ item.value.mac_address }}"
#    loop: "{{ vm_guest_facts.virtual_machines|dict2items }}"
   - debug: msg="{{
vm_guest_facts.virtual_machines['testvm'].mac_address }}"

Could someone test it against a vsphere environment?


On Wed, May 22, 2019 at 11:48 PM Kai Stian Olstad <
[email protected]> wrote:

> On 22.05.2019 22:55, Marco Reale wrote:
> > Problem 1)
> >
> > # Deploy a guest from a template*
> > - hosts: 127.0.0.1
> >   vars:
> >     vcenter_hostname: virtvcsami1.virtlab.local
> >     vcenter_user: [email protected]
> >     vcenter_pass: Esxilab!1
> >     vcenter_datastore: vsanDatastore
> >     vcenter_datacenter: DatacenterMI
> >   tasks:
> >   - name: Gather VMware guest facts
> >     vmware_vm_facts:
> >       validate_certs: False
> >       hostname: "{{ vcenter_hostname }}"
> >       username: "{{ vcenter_user }}"
> >       password: "{{ vcenter_pass }}"
> >       vm_type: vm
> >     delegate_to: localhost
> >     register: vm_guest_facts
> >
> > #  - debug:  msg="{{ item.value.mac_address }}"
> > #    loop: "{{ vm_guest_facts.virtual_machines|dict2items }}"
> >    - debug: msg="{{
> > vm_guest_facts.virtual_machines['testvm'].mac_address }}"
> >
> >
> > but is this way I get an error:
> >
> >
> > [root@nlnxmi1 testvmcdromiso]# ansible-playbook getvminfo.yml
> > ERROR! Syntax Error while loading YAML.
> > did not find expected key
>
> Your debug is indented to far, remove one space.
>
>
> > Problem 2)
> >
> >
> > Add mac address to existing inventory file or, if not possible, at
> > least in
> > some file
> >
> > Just as a stupid test I tried adding the following code at the end:
> >
> >
> >  - set_fact: vm_mac_address="prova"
> >
> >   - name: Register host to Inventory
> >     lineinfile:
> >       path: /etc/ansible/testvmcdromiso/inventory
> >       regexp: '(testvm)'
> >       line: '\1 macaddres={{ vm_mac_address }}'
> >       backrefs: yes  [root@nlnxmi1 testvmcdromiso]# cat inventory
> >   [testhost]
> >   testvm macaddress=prova
> >
> >
> >
> > but as you can see I just used a "fixed" string instead I need to get
> > the
> > mac address from the running vm but never figure it out even after 2
> > days
> > of attempts :(
> >
> > I'm just a beginner with ansible. Could you please help me?
>
> Just remove the set_fact and use the variable directly
>
>    line: '\1 macaddress={{
> vm_guest_facts.virtual_machines["\1"].mac_address }}'
>
>
> --
> Kai Stian Olstad
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/c645873ce363abe78085cd72b468b178%40olstad.com
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAFHYfv9qq_%2Bv47tUDy3CTqk85b5TXdvHiSG_ky9WqzK8UeUF8w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to