Stuck on with_items and trying to pass specific value from 1 task to the next

43 views
Skip to first unread message

davros1984

unread,
Feb 2, 2018, 11:43:51 AM2/2/18
to Ansible Project
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.







ZillaYT

unread,
Feb 5, 2018, 1:10:27 PM2/5/18
to Ansible Project

The answer in Ansible lookup values from complex structure? may give you and idea.

Reply all
Reply to author
Forward
0 new messages