Skip to content

Instantly share code, notes, and snippets.

@bcoca
Created January 19, 2015 14:47
Show Gist options
  • Save bcoca/f72d602c78943d70132c to your computer and use it in GitHub Desktop.
Save bcoca/f72d602c78943d70132c to your computer and use it in GitHub Desktop.
- hosts: legacy_servers
gather_facts: false
tags:
- legacy
tasks:
- name: get metadata on legacy servers
shell: >
hostname; len=`hostname | wc -c` ; export len; perl -E 'say "=" x ($ENV{"len"} - 1)' ;
ec2-metadata -i -a -z -s -v
register: result
- name: set facts
set_fact: mymeta=result
- hosts: localhost
connection: local
tags:
- legacy gather_facts: false
tasks:
- name: copy results to local file
template: src=test.j2 "{{ hostvars[item]['mymeta' }}" dest="/tmp/results.txt
---
test.j2
{% for host in groups['legacy_servers']%}
{{ host }}:
=========
{{ hostvars[host]['mymeta']}}
{%endfor%}
@thcorre
Copy link

thcorre commented Mar 5, 2020

Hi Brian,

Trying to reuse and apply your yml file for my use case, I get the following error:

fatal: [localhost]: FAILED! => {
"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined\n\nThe error appears to be in '/home/osboxes/Documents/ansible/sros_classic_cli_check_port.yml': line 31, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: copy results to local file\n ^ here\n"
}

For my understanding, where does the "item" variable come from?

Many thanks,
Thomas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment