"Using bare variables is deprecated." but the docs are showing me to use that syntax.?

Hi List,

I’ve always been following along with the examples here:
http://docs.ansible.com/ansible/playbooks_conditionals.html#the-when-statement

for instance:


- name: Disable requiretty
lineinfile:
dest: /etc/sudoers
regexp: "Defaults requiretty"
line: "# Defaults requiretty"
when: ansible_os_family == "RedHat" and ansible_distribution_major_version == "7"

But when I run that with Ansible > 2.0 it gives me a deprecation message:


[DEPRECATION WARNING]: Using bare variables is deprecated. Update your playbooks so that the environment value uses the full variable syntax ('{{ansible_os_family}}').
This feature will be removed in a future release.

When I change the code to this:


- name: Disable requiretty
lineinfile:
dest: /etc/sudoers
regexp: "Defaults requiretty"
line: "# Defaults requiretty"
when: "{{ ansible_os_family }} == 'RedHat' and {{ ansible_distribution_major_version }} == '7'"

I get the error:

fatal: [10.20.113.218]: FAILED! => {"failed": true, "msg": "The conditional check '{{ ansible_os_family }} == 'RedHat' and {{ ansible_distribution_major_version }} == '7'' failed. The error was: error while evaluating conditional ({{ ansible_os_family }} == 'RedHat' and {{ ansible_distribution_major_version }} == '7'): 'Debian' is undefined\n\nThe error appears to have been in '/home/mark/Documents/persgroep-aws/roles/vnucommon/tasks/main.yml': line 8, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Disable requiretty\n ^ here\n"}

Am I missing something here?

Thanks,
Mark Maas

Are you sure that this message is because of the when statement and not from other places you have used a variable?

According to Brian Coca in this post {{ and }} is implied in when and should not be used.
https://groups.google.com/forum/#!msg/ansible-project/1DyCROTgMF0/6siUigwuAQAJ

- name: Disable requiretty
lineinfile:
dest: /etc/sudoers
regexp: "Defaults requiretty"
line: "# Defaults requiretty"
when: ansible_os_family == "RedHat" and
ansible_distribution_major_version == "7"

But when I run that with Ansible > 2.0 it gives me a deprecation message:

[DEPRECATION WARNING]: Using bare variables is deprecated. Update your
playbooks so that the environment value uses the full variable syntax
('{{ansible_os_family}}').
This feature will be removed in a future release.

Are you sure that this message is because of the when statement and not
from other places you have used a variable?

Yes, I’ve seem to have been able to fix it like so:


- name: Disable requiretty
lineinfile:
dest: /etc/sudoers
regexp: "Defaults requiretty"
line: "# Defaults requiretty"
when: "'{{ ansible_os_family }}' == 'RedHat' and '{{ ansible_distribution_major_version }}' == '7'"

Apparantly one needs to use, double quotes, single quotes and accolades to make ansible 2.0 happy.

This should actually be fixed in the docs, those pages are simply wrong at the moment.

According to Brian Coca in this post {{ and }} is implied in when and
should not be used.
https://groups.google.com/forum/#!msg/ansible-project/1DyCROTgMF0/6siUigwuAQAJ

Now I’m lost, when I leave those out, ansible cries foul and throws me a deprectation warning.

I tested this task with 2.0.2.0 and doesn't get any deprecation messages:

   - name: Create directories
     file: dest=/tmp/foo state=directory
     when: ansible_os_family == "Debian" and ansible_distribution_major_version == "15"

I’ve ended up with 2 simple rules on {{}}

  1. moustaches don’t stack!! {{ var1[{{var2}}] }} <= wrong, just {{var1[var2]}}
  2. always use moustaches except when when:. All conditionals already have implied {{ }} so you would be violating rule #1, which CAN work but sometimes gives you unexpected results.

The error you are getting should ONLY happen when using a var w/o {{ }} in a with_ clause.