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?