NameError: free variable 'l_1_ansible_user' referenced before assignment in enclosing scope

Hello Roots community.

Back again with another silly question. Thank you in advance for your time and support.

I have successfully installed WP using trellis / bedrock a number of times now, fixing various issues along the way, but I have run into an error that is stumping me on a fresh install. I have google foo-ed Trellis / Ansible / Vagrant to the best of my ability and appreciate any support you can provide.

I am running:

Host:
Windows 10
Vagrant 1.9.1
VirtualBox 5.1.12
vagrant-hostmanager

Development VM (Setup with Vagrant):
Ubuntu 16.04
Ansible 2.2.0.0

Remote Server:
Subdomain: “blog.example.com
DreamCompute
Clean Ubuntu 16.04

Using the most recent versions of Trellis / Bedrock as of today (January 8, 2017).

Local Development setup goes smoothly, no issues.

The problems start when I try to provision the Remote Server.

So the following error initially pops up while running “ansible-playbook server.yml -e env=production” in the “Load Become Password” section:

Unexpected failure during module execution.
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: NameError: free variable ‘l_1_ansible_user’ referenced before assignment in enclosing scope

I verified that all of my variable settings were correct and messed around with everything I could think of. I can bypass that section by prompting for --ask-become-pass, but then I hit the exact same error again in the “Fail if root login will be disabled but admin_user will not be a sudoer” section.

If I permit logging in as root in the security.yml, its fails at the “Setup users” section with the same error. Running with -vvv gives me the following before the error:

Unexpected failure during module execution.
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File “/usr/local/lib/python2.7/dist-packages/ansible/executor/task_executor.py”, line 91, in run
item_results = self._run_loop(items)
File “/usr/local/lib/python2.7/dist-packages/ansible/executor/task_executor.py”, line 274, in _run_loop
res = self._execute(variables=task_vars)
File “/usr/local/lib/python2.7/dist-packages/ansible/executor/task_executor.py”, line 431, in _execute
self._task.post_validate(templar=templar)
File “/usr/local/lib/python2.7/dist-packages/ansible/playbook/task.py”, line 248, in post_validate
super(Task, self).post_validate(templar)
File “/usr/local/lib/python2.7/dist-packages/ansible/playbook/base.py”, line 371, in post_validate
value = templar.template(getattr(self, name))
File “/usr/local/lib/python2.7/dist-packages/ansible/template/init.py”, line 359, in template
d[k] = self.template(variable[k], preserve_trailing_newlines=preserve_trailing_newlines, fail_on_undefined=fail_on_undefined, overrides=overrides)
File “/usr/local/lib/python2.7/dist-packages/ansible/template/init.py”, line 331, in template
result = self._do_template(variable, preserve_trailing_newlines=preserve_trailing_newlines, escape_backslashes=escape_backslashes, fail_on_undefined=fail_on_undefined, overrides=overrides)
File “/usr/local/lib/python2.7/dist-packages/ansible/template/init.py”, line 507, in _do_template
res = j2_concat(rf)
File “”, line 16, in root
File “/usr/local/lib/python2.7/dist-packages/jinja2/runtime.py”, line 358, in init
self._after = self._safe_next()
File “/usr/local/lib/python2.7/dist-packages/jinja2/runtime.py”, line 378, in _safe_next
return next(self._iterator)
File “”, line 16, in
NameError: free variable ‘l_1_item’ referenced before assignment in enclosing scope

fatal: [blog.example.com]: FAILED! => {
“failed”: true,
“stdout”: “”
}
to retry, use: --limit @/home/vagrant/trellis/server.retry

I have tried downgrading Ansible to 2.0.2.0 / changing various variable settings, but I am at a loss for my next steps. I can SSH into my remote server both using Putty and from the development VM itself, so I don’t think its an access issue.

I am sure this is something stupid on my end, but I am hitting the cap on my current experience and was hoping someone could point me in the right direction.

Thank you again for your time and effort!

P.S. Sorry if too much random information.

1 Like

I am having the same exact problem and can’t figure it out for the life of me… This is my first time trying set up a trellis website though. I’ve been following the guide available here.

I am running:

Host:
macOS 10.12.2
Vagrant 1.9.1
VirtualBox 5.1.12
vagrant-bindfs
vagrant-hostmanager

Development VM (Setup with Vagrant):
Ubuntu 16.04
Ansible 2.2.0.0

Remote Server:
DigitalOcean
Ubuntu 16.04

GitHub Repository
here

This is the full error I get when using -vvv:

TASK [remote-user : Load become password] **************************************
task path: /Users/kasey/GitHub/trellis-test/trellis/roles/remote-user/tasks/main.yml:25
System info:
Ansible 2.2.0.0; Darwin
Trellis at “Fix #727 - HSTS: default preload to off”

Unexpected failure during module execution.
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File “/Library/Python/2.7/site-packages/ansible/executor/task_executor.py”, line 119, in run
res = self._execute()
File “/Library/Python/2.7/site-packages/ansible/executor/task_executor.py”, line 431, in _execute
self._task.post_validate(templar=templar)
File “/Library/Python/2.7/site-packages/ansible/playbook/task.py”, line 248, in post_validate
super(Task, self).post_validate(templar)
File “/Library/Python/2.7/site-packages/ansible/playbook/base.py”, line 371, in post_validate
value = templar.template(getattr(self, name))
File “/Library/Python/2.7/site-packages/ansible/template/init.py”, line 359, in template
d[k] = self.template(variable[k], preserve_trailing_newlines=preserve_trailing_newlines, fail_on_undefined=fail_on_undefined, overrides=overrides)
File “/Library/Python/2.7/site-packages/ansible/template/init.py”, line 331, in template
result = self._do_template(variable, preserve_trailing_newlines=preserve_trailing_newlines, escape_backslashes=escape_backslashes, fail_on_undefined=fail_on_undefined, overrides=overrides)
File “/Library/Python/2.7/site-packages/ansible/template/init.py”, line 507, in _do_template
res = j2_concat(rf)
File “”, line 13, in root
File “/Library/Python/2.7/site-packages/jinja2/runtime.py”, line 358, in init
self._after = self._safe_next()
File “/Library/Python/2.7/site-packages/jinja2/runtime.py”, line 378, in _safe_next
return next(self._iterator)
File “”, line 13, in
NameError: free variable ‘l_1_ansible_user’ referenced before assignment in enclosing scope

fatal: [138.197.204.24]: FAILED! => {
“failed”: true,
“stdout”: “”
}

2 Likes

Thank you @Ocificap and @kculmback for the excellent and thorough reports. It’s really helpful.

I can reproduce this on Windows and macOS under these conditions:

As far as I can tell, python 2.7.12 changed how python processes the {% if loop.first %} (e.g., here). The vagrant bento/ubuntu-16.04 includes python 2.7.12.

I’m actively working on a PR fix. It shouldn’t take long.

4 Likes

Thanks for the quick reply, @fullyint! Definitely looks like you’re right. When I tried using ansible-playbook server.yml -e env=production --ask-become-pass, suggested by @Ocificap, it also failed at
TASK [users : Fail if root login will be disabled but admin_user will not be a sudoer]

The code for that task also includes an {% if loop.first %}, seen here.

Full error:

TASK [users : Fail if root login will be disabled but admin_user will not be a sudoer] ***
task path: /Users/kasey/GitHub/trellis-test/trellis/roles/users/tasks/main.yml:16
System info:
  Ansible 2.2.0.0; Darwin
  Trellis at "Fix #727 - HSTS: default preload to off"
---------------------------------------------------
Unexpected failure during module execution.
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/ansible/executor/task_executor.py", line 119, in run
    res = self._execute()
  File "/Library/Python/2.7/site-packages/ansible/executor/task_executor.py", line 431, in _execute
    self._task.post_validate(templar=templar)
  File "/Library/Python/2.7/site-packages/ansible/playbook/task.py", line 248, in post_validate
    super(Task, self).post_validate(templar)
  File "/Library/Python/2.7/site-packages/ansible/playbook/base.py", line 371, in post_validate
    value = templar.template(getattr(self, name))
  File "/Library/Python/2.7/site-packages/ansible/template/__init__.py", line 359, in template
    d[k] = self.template(variable[k], preserve_trailing_newlines=preserve_trailing_newlines, fail_on_undefined=fail_on_undefined, overrides=overrides)
  File "/Library/Python/2.7/site-packages/ansible/template/__init__.py", line 352, in template
    return [self.template(v, preserve_trailing_newlines=preserve_trailing_newlines, fail_on_undefined=fail_on_undefined, overrides=overrides) for v in variable]
  File "/Library/Python/2.7/site-packages/ansible/template/__init__.py", line 331, in template
    result = self._do_template(variable, preserve_trailing_newlines=preserve_trailing_newlines, escape_backslashes=escape_backslashes, fail_on_undefined=fail_on_undefined, overrides=overrides)
  File "/Library/Python/2.7/site-packages/ansible/template/__init__.py", line 507, in _do_template
    res = j2_concat(rf)
  File "<template>", line 12, in root
  File "/Library/Python/2.7/site-packages/jinja2/runtime.py", line 358, in __init__
    self._after = self._safe_next()
  File "/Library/Python/2.7/site-packages/jinja2/runtime.py", line 378, in _safe_next
    return next(self._iterator)
  File "<template>", line 12, in <genexpr>
NameError: free variable 'l_1_admin_user' referenced before assignment in enclosing scope
fatal: [138.197.204.24]: FAILED! => {
    "failed": true,
    "stdout": ""
}

One last note… I tried setting sshd_permit_root_login to true and ran server.yml. This time I got an error at:

TASK [users : Setup users] *****************************************************
task path: /Users/kasey/GitHub/trellis-test/trellis/roles/users/tasks/main.yml:31

See task here.

Full error report:

TASK [users : Setup users] *****************************************************
task path: /Users/kasey/GitHub/trellis-test/trellis/roles/users/tasks/main.yml:31
System info:
  Ansible 2.2.0.0; Darwin
  Trellis at "Fix #727 - HSTS: default preload to off"
---------------------------------------------------
Unexpected failure during module execution.
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/ansible/executor/task_executor.py", line 91, in run
    item_results = self._run_loop(items)
  File "/Library/Python/2.7/site-packages/ansible/executor/task_executor.py", line 274, in _run_loop
    res = self._execute(variables=task_vars)
  File "/Library/Python/2.7/site-packages/ansible/executor/task_executor.py", line 431, in _execute
    self._task.post_validate(templar=templar)
  File "/Library/Python/2.7/site-packages/ansible/playbook/task.py", line 248, in post_validate
    super(Task, self).post_validate(templar)
  File "/Library/Python/2.7/site-packages/ansible/playbook/base.py", line 371, in post_validate
    value = templar.template(getattr(self, name))
  File "/Library/Python/2.7/site-packages/ansible/template/__init__.py", line 359, in template
    d[k] = self.template(variable[k], preserve_trailing_newlines=preserve_trailing_newlines, fail_on_undefined=fail_on_undefined, overrides=overrides)
  File "/Library/Python/2.7/site-packages/ansible/template/__init__.py", line 331, in template
    result = self._do_template(variable, preserve_trailing_newlines=preserve_trailing_newlines, escape_backslashes=escape_backslashes, fail_on_undefined=fail_on_undefined, overrides=overrides)
  File "/Library/Python/2.7/site-packages/ansible/template/__init__.py", line 507, in _do_template
    res = j2_concat(rf)
  File "<template>", line 16, in root
  File "/Library/Python/2.7/site-packages/jinja2/runtime.py", line 358, in __init__
    self._after = self._safe_next()
  File "/Library/Python/2.7/site-packages/jinja2/runtime.py", line 378, in _safe_next
    return next(self._iterator)
  File "<template>", line 16, in <genexpr>
NameError: free variable 'l_1_item' referenced before assignment in enclosing scope
fatal: [138.197.204.24]: FAILED! => {
    "failed": true,
    "stdout": ""
}

Proposed fix in roots/trellis#729
Testing and feedback welcome.

3 Likes

Solved my issues!

Thanks for the quick reply and fix. :grin:

Thanks for the fix @fullyint! This fixed those errors.

Although now I’ve run into a new error :sweat_smile: Which I’ve made a topic about here.