Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail module error when using variables as items in EL6 #20309

Closed
rmfitzpatrick opened this issue Jan 16, 2017 · 11 comments
Closed

Fail module error when using variables as items in EL6 #20309

rmfitzpatrick opened this issue Jan 16, 2017 · 11 comments
Labels
affects_2.2 This issue/PR affects Ansible v2.2 bug This issue/PR relates to a bug.

Comments

@rmfitzpatrick
Copy link
Contributor

Ansible is throwing the following during a conditional check for the fail module:

    "msg": "The conditional check '{{ item }} is not defined' failed.
The error was: 'AnsibleEnvironment' object has no attribute '_generate'
The error appears to have been in '.../roles/role_one/tasks/main.yml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

bisect shows that this was introduced in 86eadc5.

This is causing failures in Tower 3.0.3 installs.

ISSUE TYPE
  • Bug Report
ANSIBLE VERSION
ansible 2.2.1.0
OS / ENVIRONMENT

EL6. Not observed in EL7.

SUMMARY

The condition that uses variables for items is incorrectly parsed.

STEPS TO REPRODUCE
# repro.yml
---
- hosts: my_group
  roles:
    - role: role_one
      used_vars:
        - var_one
        - var_two
# roles/role_one/tasks/main.yml:
---
- name: Ensure expected variables are defined
  fail: msg="{{ item }} value missing and is required"
  when: "{{ item }} is not defined"
  with_items: '{{ used_vars }}'

ansible-playbook -i inventory -e "var_one=123 var_two=234" repro.yml

@ansibot ansibot added affects_2.2 This issue/PR affects Ansible v2.2 bug_report needs_triage Needs a first human triage before being processed. labels Jan 16, 2017
@alikins
Copy link
Contributor

alikins commented Jan 16, 2017

ping @jimi-c

@alikins alikins removed the needs_triage Needs a first human triage before being processed. label Jan 16, 2017
@bcoca
Copy link
Member

bcoca commented Jan 16, 2017

that condition does not make much sense, the following would be the correct way to write it

when: item is not defined

but that would never execute as with_items will always define an item if it gets a list, if not it will skip the task anyways.

@alikins
Copy link
Contributor

alikins commented Jan 16, 2017

What version of jinja2 is installed? (the Environment._generate() method causing the error was added in jinja 2.5)

@rmfitzpatrick
Copy link
Contributor Author

@alikins python-jinja2-2.2.1-2.el6_5.x86_64. Has the version requirement changed?

@rmfitzpatrick
Copy link
Contributor Author

@bcoca, I agree the condition is not ideal but has worked with 2.2 until the most recent rc. Is it invalid syntax?

@bcoca
Copy link
Member

bcoca commented Jan 16, 2017

Ansible is not currently working with the latest jinja2 (2.9), but we have not set bottom version requirements (which we might need now due to the fix for the security issue).

@bcoca
Copy link
Member

bcoca commented Jan 16, 2017

@rmfitzpatrick it violates the 'no moustaches when when:' rule as it does double interpolation, which is not guaranteed to work.

@bcoca
Copy link
Member

bcoca commented Jan 16, 2017

IF you need the double interpolation, look at doing:

when: hostvars[inventory_hostname][item] is defined

possibly user the vars dictionary instead.

@alikins
Copy link
Contributor

alikins commented Jan 16, 2017

Looks like jinja2.2 jinja2.environment.Template doesnt have _generate(), but there is a jinja2.environment.generate() which could be used. And it looks like newer jinja2 still has the jinja2.environment.generate() so it could potentially be used from conditional.py

@jimi-c jimi-c closed this as completed in af96cba Jan 16, 2017
@jimi-c
Copy link
Member

jimi-c commented Jan 16, 2017

Closing This Ticket

Hi!

We believe the above commit should resolve this problem for you. This will also be included in the next release.

If you continue seeing any problems related to this issue, or if you have any further questions, please let us know by stopping by one of the two mailing lists, as appropriate:

Because this project is very active, we're unlikely to see comments made on closed tickets, but the mailing list is a great way to ask questions, or post if you don't think this particular issue is resolved.

Thank you!

jimi-c added a commit that referenced this issue Jan 16, 2017
jimi-c added a commit that referenced this issue Jan 16, 2017
@jimi-c
Copy link
Member

jimi-c commented Jan 16, 2017

Also cherry-picked to stable-2.2 and 2.1.

@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
weikunzz pushed a commit to weikunzz/test_cases that referenced this issue Apr 12, 2018
After change in ansible/ansible#20309
Not using quote in RHEL6 will fail with:
'AnsibleEnvironment' object has no attribute '_generate'

Change-Id: I581eb2b80e831c07ae942ed6c9a5fae326a69f0c
Signed-off-by: Hao Liu <hliu@redhat.com>
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.2 This issue/PR affects Ansible v2.2 bug This issue/PR relates to a bug.
Projects
None yet
Development

No branches or pull requests

5 participants