The answer in Ansible lookup values from complex structure?
<https://stackoverflow.com/questions/39685255/ansible-lookup-values-from-complex-structure>
may
give you and idea.
On Friday, February 2, 2018 at 11:43:51 AM UTC-5, davros1984 wrote:
>
> Hi,
>
> I've spent the past few days trying to find a solution to a challenge I
> have. Hopefully someone here has the answer.
>
> Here is a slimmed down version, which hopefully will help me move forwards.
> - hosts: satellite
> gather_facts: False
> remote_user: root
> tasks:
> - name: test
> vmware_guest_facts:
> hostname: vcenter
> username: me
> password: <somepassword>
> datacenter: DC1
> folder: /DC1/vm
> validate_certs: no
> name: "{{ item }}"
> register: facts
> delegate_to: localhost
> with_items:
> - ltdrob002
> - ltdrob003
>
> - debug: msg="{{ facts.results[0].instance.hw_eth0.macaddress }}"
> with_items: "{{ facts.results }}"
>
>
> The above works, it goes to vcenter and prints the MAC address of the
> first server (in reference to item [0]). However it prints the MAC address
> of the first VM when its getting the facts of the second VM. What I am
> trying to achieve is to get the MAC address printed for the relevant VM as
> it loops through. So in the debug area.... I want it to resemble something
> like:
>
> - debug: msg="{{ item.facts.results.instance.hw_eth0.macaddress }}"
> with_items: "{{ facts.results }}"
>
> The above code doesn't work and the error..
> "The task includes an option with an undefined variable. The error was:
> 'dict object' has no attribute 'facts'" is seen.
>
>
> A bit of background.... this is optional, help on the above is great for
> now.
> The challenge I have is that I need to provision a group of VMs, via the
> use of with_list (I have a seperate vars file with required vm info in,
> i.e. vmName: ltdrob003). Currently I have this working fine with just a
> single VM. I need to pass the MAC address of the VM to Satellite (Hammer)
> so that Satellite is able to PXE boot/configure the VM.
>
> The MAC address gets generated when the VM is created so this needs to be
> passed to Hammer. As stated, I have this working for 1 VM at a time, but it
> would be nice to pass to Ansible a list of VMs to create and have it create
> them in 1-run. I achieve this currently with the below and the hammer
> command just references "{{ vm_mac }}".
> - set_fact:
> vm_mac: "{{ results.instance.hw_eth0.macaddress }}"
>
> My plan will be to alter the name of the fact to match the name of the vm,
> i.e... "{{ vmName }}_mac": <macaddress>
> and then have Hammer reference the MAC via lookup:
>
> mac="{{ vars[vmName + '_mac'] }}":
>
>
> I can add further detail, but initially I wanted to try and put a simple
> example in the hope that this will get some responses.
>
> Many thanks in advance.
>
>
>
>
>
>
>
>
--
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/ffd6163f-a860-408f-8a64-90b73e4c8f09%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.