Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible "when statement should not include jinja2 templating"

Tags:

ansible

jinja2

I'm trying to check if certain string is one of the elements of a list. I managed to get the desired results, but I can't find a way to do it without getting a warning saying:

[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}.

Could you help me understand how to make a proper 'when' statement for this use case?

This is the content of the variable repos:

TASK [debug msg={{ repos }}] 
ok: [zimbramta01.essi.lab] => {
"msg": {
    "changed": true,
    "cmd": "yum repolist | cut -f1 -d\" \" | grep -iv 
    \"repolist\\|loaded\\|repo\"",
    "delta": "0:00:10.884070",
    "end": "2018-09-20 08:03:55.210767",
    "failed": false,
    "rc": 0,
    "start": "2018-09-20 08:03:44.326697",
    "stderr": "",
    "stderr_lines": [],
    "stdout": "epel/x86_64\nrhel-7-server-htb-rpms/x86_64\nrhel-7-server- 
     rpms/7Server/x86_64\nzimbra\nzimbra-889-network\nzimbra-889-oss",
    "stdout_lines": [
        "epel/x86_64",
        "rhel-7-server-htb-rpms/x86_64",
        "rhel-7-server-rpms/7Server/x86_64",
        "zimbra",
        "zimbra-889-network",
        "zimbra-889-oss"
    ]
}
}

This is how my "when" statement looks like (it is working, i just want to learn how to write it properly).

- name: agrega repositorio epel
  shell: rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
  args:
    warn: False
  when: '"epel/x86_64" not in {{ repos.stdout_lines }}'

I've tried different ways to do it, and even when I read the ansible doc, the jinja2 doc, and some other posts in stackoverflow, I was not able to find a proper way to do this without any warnings :/

like image 708
carrotcakeslayer Avatar asked Sep 20 '18 06:09

carrotcakeslayer


1 Answers

As simple as:

when: "'epel/x86_64' not in repos.stdout_lines"

The warning means that the value of the when key is treated as a Jinja2 expressionas a whole, i.e.

{% "epel/x86_64" not in {{ repos.stdout_lines }} %}

And that a {{ ... }} inside {% ... %} is superfluous.

like image 98
techraf Avatar answered Oct 09 '22 05:10

techraf