jinja2 warning

45 views
Skip to first unread message

Mahesh Nalavade

unread,
Aug 9, 2021, 9:10:54 AM8/9/21
to Ansible Project
Hi ,

While running the ansible task in azure pipeline we are getting the below warning message. Please let us know if we can ignore/suppress the warning.

[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: {% for item in hostvars.values() %}{%
if item.MyDoneVar|default(False) %}True{% endif %}{% endfor %}

==========================================================
Playbook:-
=============
---
- name: Check the status of DSE layer 7 cluster
  hosts: XXXX
  gather_facts: False
  serial: 1
  tasks:
    - meta: end_play
      when: "{% for item in hostvars.values() %}{% if item.MyDoneVar|default(False) %}True{% endif %}{% endfor %}"
    - name: nodetool status playbook execution
      import_role:
        name: ../roles/dse/nodetool_status_check

Role:-
=========
---
- name: Nodetool status for DSE layer 7 cluster
  block: 
    - name: Nodetool Status
      shell: nodetool -u controlRole -pw '{{ controlRole_dse_layer7_pass }}' status | grep -A 10 {{ dse_layer_7_datacenter_name }}
      register: result_nodetool
   - set_fact:
         MyDoneVar: True

Stefan Hornburg (Racke)

unread,
Aug 9, 2021, 10:18:58 AM8/9/21
to ansible...@googlegroups.com
On 09/08/2021 15:10, Mahesh Nalavade wrote:
> Hi ,
>
> While running the ansible task in azure pipeline we are getting the below warning message. Please let us know if we can ignore/suppress the warning.
>
> [WARNING]: conditional statements should not include jinja2 templating
> delimiters such as {{ }} or {% %}. Found: {% for item in hostvars.values() %}{%
> if item.MyDoneVar|default(False) %}True{% endif %}{% endfor %}

The logic behind your condition eludes me, but the expression for conditions are always subject to templating. This means you can't use {{, {% if, {% for etc.

You can start to convert your logic to

hostvar.values | dict2items | ... | count

Regards
Racke

>
> ==========================================================
> Playbook:-
> =============
> ---
> - name: Check the status of DSE layer 7 cluster
>   hosts: XXXX
>   gather_facts: False
>   serial: 1
>   tasks:
>     - meta: end_play
>       when: "{% for item in hostvars.values() %}{% if item.MyDoneVar|default(False) %}True{% endif %}{% endfor %}"
>     - name: nodetool status playbook execution
>       import_role:
>         name: ../roles/dse/nodetool_status_check
>
> Role:-
> =========
> ---
> - name: Nodetool status for DSE layer 7 cluster
>   block:
>     - name: Nodetool Status
>       shell: nodetool -u controlRole -pw '{{ controlRole_dse_layer7_pass }}' status | grep -A 10 {{ dse_layer_7_datacenter_name }}
>       register: result_nodetool
>    - set_fact:
>          MyDoneVar: True
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/c7db9905-75f3-452e-88a3-3ca2b95b3836n%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/c7db9905-75f3-452e-88a3-3ca2b95b3836n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration.

Mahesh Nalavade

unread,
Aug 10, 2021, 1:54:13 AM8/10/21
to Ansible Project
Hi  Racke,

Can you please help me to form the logic as you suggested above for below condition

when: "{% for item in hostvars.values() %}{% if item.MyDoneVar|default(False) %}True{% endif %}{% endfor %}"

Dick Visser

unread,
Aug 10, 2021, 2:54:30 AM8/10/21
to ansible...@googlegroups.com
On Tue, 10 Aug 2021 at 07:54, Mahesh Nalavade
<mahesh.na...@gmail.com> wrote:
>
> Hi Racke,
>
> Can you please help me to form the logic as you suggested above for below condition

Instead of reverse engineering/guessing, why not tell us what you
actually had in mind when you added that condition?

Mahesh Nalavade

unread,
Aug 10, 2021, 4:47:08 AM8/10/21
to Ansible Project

Hi,

I have 3 host in my inventory group as below
Inventory:-
==========
---
all:
  children:
    dse_layer_7:
      hosts:
        XXXXX:
        XXXXX:
        XXXXX:
      vars:  
        ansible_user: XXXX
        ansible_connection: ssh
        ansible_password: "{{ lookup('file', 'XXXXXX) }}"


Scenario: - Want to run the below command on first host and if the command executed successfully skip the rest of the host. If it fails on first host ,command should run on second host and if the command executed successfully skip the rest of the host and so on.

command:-
=========
nodetool -u controlRole -pw '{{ controlRole_dse_layer7_pass }}' status | grep -A 10 {{ dse_layer_7_datacenter_name }}


so to achieve this  i wrote the playbook as below, which is working fine with warnings .In azure pipeline this warning is considered as failure.
Playbook:-
=============
---
- name: Check the status of DSE layer 7 cluster
  hosts: XXXX
  gather_facts: False
  serial: 1
  tasks:
    - meta: end_play
      when: "{% for item in hostvars.values() %}{% if item.MyDoneVar|default(False) %}True{% endif %}{% endfor %}"
    - name: nodetool status playbook execution
      import_role:
        name: ../roles/dse/nodetool_status_check

Role:-
=========
---
- name: Nodetool status for DSE layer 7 cluster
  block: 
    - name: Nodetool Status
      shell: nodetool -u controlRole -pw '{{ controlRole_dse_layer7_pass }}' status | grep -A 10 {{ dse_layer_7_datacenter_name }}
      register: result_nodetool
   - set_fact:
         MyDoneVar: True

Reply all
Reply to author
Forward
0 new messages