Variables between tasksand errors between Debian and Mac OS

Hi All,

I’m using a playbook to create an instance. Essentially, I’ve modified the code snippet on example 4 here → http://docs.ansible.com/guide_aws.html

From the command_line, I pass in a variable “instance_name”, which I use to set the tags for the EC2 instance and the hostname for the instance I just created.

The below code snippet works fine on Mac (Ansible 1.7.2), but fails on Debian 8.0 with the same version of Ansible 1.7.2.

What am I doing wrong? Or is this something with the code?

`

  • hosts: localhost

connection: local

gather_facts: False

vars:

instance_name: “somename”
-tasks:

–snipped–
–snipped–

Connect to the node and gather facts, including the instance-id. These facts

are added to inventory hostvars for the duration of the playbook’s execution

Typical “provisioning” tasks would go in this playbook.

  • hosts: ec2hosts
    gather_facts: True
    user: admin
    sudo: True

tasks:

fetch instance data from the metadata servers in ec2

  • ec2_facts:

show all known facts for this host

  • debug: var=hostvars[inventory_hostname]

just show the instance-id

  • debug: msg=“{{ hostvars[inventory_hostname][‘ansible_ec2_instance_id’] }}”

  • name: set hostname to {{ instance_name }}
    hostname:
    name: “{{ instance_name }}”

`

The error I get on debian is:

`
TASK: [set hostname to {{ instance_name }}] ***********************************

fatal: [54.xx.xx.xx] => One or more undefined variables: ‘instance_name’ is undefined

FATAL: all hosts have already failed – aborting

PLAY RECAP ********************************************************************
`

On the Mac (Yosemite), this part goes without any incident:

`
TASK: [set hostname to somename] *************************************************

changed: [54.xx.xx.xx]

`

It looks like your variable is defined to the localhost action and then is not defined when talking to your ec2_hosts.