Issue with extra variables in 1.5.4

I’m seeing an issue with variable substitution between versions 1.5.3 and 1.5.4.

I have an include file that I use to create vm instances, create_instance.yml and a playbook create_cluster.yml that I use to spin up all the instances for the cluster in one go. I have variables in my group_vars that I use to configure the cluster e.g. the cluster_name and when I spin up a new cluster I usually override these using the extras vars switch -e

the create_cluster looks something like the following

  • hosts: localhost
    connection: local
    gather_facts: False

tasks:

  • {include: create_instance.yml,
    tags: {
    hostname: ‘blah_server1’,
    ansible_groups: [“{{cluster_name}}”, ‘blah-servers’],
    cluster_name: “{{cluster_name}}”
    }
    }

create_instance.yml looks like

  • debug: msg="creating instance for cluster ‘{{ cluster_name }}’ ‘{{tags}}’ "

group_vars/all

cluster_name: dev

For instance to spin up a new ‘qa’ cluster, I would issue

ansible-playbook -i hosts create_cluster.yml -e cluster_name=qa

Up until recently this would correctly override the cluster_name variable as expected and produce the output below. (Specifically notice how the cluster_name is set to qa in the debug message)

PLAY [localhost] **************************************************************

TASK: [debug msg="creating instance for cluster ‘qa’ ‘{‘cluster_name’: u’qa’, ‘hostname’: ‘blah_server1’, ‘ansible_groups’: [u’qa’, ‘blah-servers’]}’ "] ***
ok: [127.0.0.1] => {
“item”: “”,
“msg”: "creating instance for cluster ‘qa’ ‘{‘cluster_name’: u’qa’, ‘hostname’: ‘blah_server1’, ‘ansible_groups’: [u’qa’, ‘blah-servers’]}’ "
}

Between 1.5.3 and 1.5.4 this behavior has changed, running it with 1.5.4 produces.

PLAY [localhost] **************************************************************

TASK: [debug msg="creating instance for cluster ‘qa’ ‘{‘cluster_name’: u’qa’, ‘hostname’: ‘blah_server1’, ‘ansible_groups’: [u’qa’, ‘blah-servers’]}’ "] ***
ok: [127.0.0.1] => {
“item”: “”,
“msg”: "creating instance for cluster ‘qa’ ‘{‘cluster_name’: u’dev’, ‘hostname’: ‘blah_server1’, ‘ansible_groups’: [u’dev’, ‘blah-servers’]}’ "
}

Notice how the variable is correctly substituted in the task listing, but in the msg it is not overridden correctly at all and is still ‘dev’. This causes me to spin up instances into the wrong cluster and in other playbooks that configure the cluster it applies changes to incorrect machines.

The changes between 1.5.3 and 1.5.4 are very limited, I think its related to the split from SETUP_CACHE into VARS_CACHE, but I’ve not tracked it down yet. Anyone any insight into what might have happened?

A gist of the files can be found here

https://gist.github.com/10285167

Thanks,

Steve.

This looks to be because the system is able to “early resolve” the variable cluster_name and it tries to do so too soon.

Can you make sure there is a bug filed on this in github?

Thank you!

This appears to be the same issue as https://github.com/ansible/ansible/issues/6677 which has ben fixed and should roll out with 1.6.

Excellent. Sounded a bit familiar :slight_smile:

Hi,

I’ve finally had a chance to circle back around to verifying this is fixed and I noticed there was a bug in my gist. The all file should be in a group_vars folder. When I move the vars file there the failure returns. It seems as you suspected that the cluster_name from there is still being resolved too early, even with the fix for https://github.com/ansible/ansible/issues/6677

I’ve tracked it down to the following, _executor_internal is run in the runner module to execute the debug task, this calls on 564

module_vars = template.template(self.basedir, self.module_vars, module_vars_inject)

and the module vars sent in for templating are only the vars extracted from the hosts file, not the combined set

{‘cluster_name’: ‘dev’, ‘inventory_hostname’: ‘127.0.0.1’, ‘inventory_hostname_short’: ‘127’, ‘ansible_python_interpreter’: ‘/usr/local/bin/python’, ‘group_names’: [‘localhost’]}

So even though the task output shows the variables resolved and templated correctly using the combined set

TASK: [debug msg="creating instance for cluster ‘stage’ ‘{‘cluster_name’: u’stage’, ‘hostname’: ‘blah_server1’, ‘ansible_groups’: [u’stage’, ‘blah-servers’]}’ "] ***

the execution is using the incomplete set and resolves to

ok: [127.0.0.1] => {
“item”: “”,
“msg”: "creating instance for cluster ‘stage’ ‘{‘cluster_name’: u’dev’, ‘hostname’: ‘blah_server1’, ‘ansible_groups’: [u’dev’, ‘blah-servers’]}’ "
}

I’m going to see if I can’t get it to pass the combined set in here, if I can I’ll create a patch. Should I open a bug for this?

regards,

Steve.

Per the above, 1.6 is already released. If you have a bug in 1.6 please file it and include the most minimal reproducer possible along with it.

We will not be updating the 1.5.X series.

Didn’t realize 1.6 was gold, nice work. I’d pulled a day or so ago so the issue was still there then. I’ve tracked down what the issue is and have a patch. If its still there in the 1.6 branch I’ll issue a patch.

thanks.

Steve.