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

1,388 views
Skip to first unread message

Mark Maas

unread,
May 11, 2016, 5:55:53 AM5/11/16
to Ansible Project
Hi List,

I've always been following along with the examples here:

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

Kai Stian Olstad

unread,
May 11, 2016, 10:31:12 AM5/11/16
to ansible...@googlegroups.com
On 11. mai 2016 11:55, Mark Maas wrote:
> 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.
> ```

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

--
Kai Stian Olstad

Mark Maas

unread,
May 12, 2016, 2:06:38 AM5/12/16
to Ansible Project, ansible-pr...@olstad.com


On Wednesday, May 11, 2016 at 4:31:12 PM UTC+2, Kai Stian Olstad wrote:
> ```
> - 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.
 

Kai Stian Olstad

unread,
May 12, 2016, 10:11:00 AM5/12/16
to ansible...@googlegroups.com
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"

--
Kai Stian Olstad

Brian Coca

unread,
May 12, 2016, 10:14:43 AM5/12/16
to ansible...@googlegroups.com
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.

​I've tested also and you DO NOT get the error by ommiting {{ }} in a when clause.​

----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages