Trying to capture value from with_lines loop

21 views
Skip to first unread message

Evan

unread,
Mar 4, 2018, 10:46:27 PM3/4/18
to Ansible Project
I'm trying to loop through a list (file) of powervc regions, delegating a command to them, and if that command (find a vm) is successful (ie: the vm is on that region) I want to record the region name. I'm using delegate_to because I have other commands to run that are not on that region.

---
- hosts: all
  gather_facts
: false
  vars
:
    host
: host1

  tasks
:

 
- name: "Validate if VM exists on Region"
    shell
: source /opt/ibm/powervc/powervcrc.auto && openstack --insecure server show {{host}} |grep -owP 'id.*\|\s\K.*[^\|]+' | awk {' print $1 '}
   
register: vm_exist
    delegate_to
: "{{ item }}"
    with_lines
: cat regions.inv
    ignore_errors
: false


This currently works - regions.inv is a list of names (region1-pvc, region2-pvc etc). It loops through each entry, connects to the host and runs the shell command. Is it possible to capture the value of {{ item }} for use in other tasks if the shell command returns successfully? I've tried using hostvar, but the powervc node isn't internally called region1. It returns the hostname 'powervc' - the region1-pvc/IP name is just a DNS entry.

Every attempt to use {{ item }} in the next task returns an "undefined" error. This may be a logic issue with how I'm trying to structure my playbooks, but it's currently the only way I can think of doing it. Any suggestions?

flowerysong

unread,
Mar 4, 2018, 11:50:48 PM3/4/18
to Ansible Project
You should look more closely at the contents of the `vm_exist` variable. It contains a list of results, and each result includes the value of `item` for that iteration of the loop.
Message has been deleted

Evan

unread,
Mar 5, 2018, 1:27:57 AM3/5/18
to Ansible Project
You should look more closely at the contents of the `vm_exist` variable. It contains a list of results, and each result includes the value of `item` for that iteration of the loop.

Hi flowerysong,
The only reference that I can find in the output of that command is the hypervisor name, which is just the node it's located on, rather than the powervc region (my powervc region controls several nodes). Unless you mean a reference to the way register stores the values returned?
Reply all
Reply to author
Forward
0 new messages