Assign and print variable inside a with_items loop based on condition.

- debug:
msg: "User was {{ item.split('\t')[3] }}"
with_items: "{{ command_result.stdout_lines }}"

The above give me the below output:

“msg”: “User was FrontEnd”
“msg”: “User was BackEnd”

I now wish to set USER variable as user1 is returned value is FrontEnd else USER value should be user2.

I dont know how to do this but below is my failing playbook.

- debug:
vars:
USER: "{{ 'user1' if item.split('\t')[3] == 'FrontEnd' else 'user2' }}"
msg: "User is {{ USER }}"

with_items: "{{ command_result.stdout_lines }}"

Expected output is:

“msg”: “User is user1”
“msg”: “User is user2”

Can you please suggest ?

> - debug:
msg: "User was {{ item.split('\t')[3] }}"
with_items: "{{ command_result.stdout_lines }}"|

The above give me the below output:

    "msg": "User was FrontEnd"
    "msg": "User was BackEnd"

I now wish to set USER variable as user1 is returned value is FrontEnd else USER value should be user2.

I dont know how to do this but below is my failing playbook.

> - debug:
vars:
USER: "{{ 'user1' if item.split('\t')[3] == 'FrontEnd' else 'user2' }}"
msg: "User is {{ USER }}"

 with\_items: "\{\{ command\_result\.stdout\_lines \}\}"|

Expected output is:

    "msg": "User is user1"
    "msg": "User is user2"

Can you please suggest ?

Hello,

what is the output that you actually get ... and please don't post the same question twice.

Regards
         Racke

@Stefan here is the output

@Stefan here is the output

Sorry but there is no "Hello world" in the tasks you posted. Providing inaccurate information isn't helpful.

Regards
         Racke

@Stefan. The information provided by me is complete.

The variable if not assigned a value defaults as “Hello World”

Reference: https://docs.ansible.com/ansible/latest/modules/debug_module.html

You can recreate the playbook output.

Here is my entire playbook:

`

@Stefan. The information provided by me is complete.

The variable if not assigned a value defaults as "Hello World"

Reference: https://docs.ansible.com/ansible/latest/modules/debug_module.html

Ha - I never ran in that one. Alas, you need to resort to proper if else statements in order to get your
logic to work:

  - debug:
      msg: "{% if item.split('\t')[3] == 'FrontEnd' %}user1{% else %}'user2'{% endif %}"
    with_items: "{{ command_result.stdout_lines }}"

Regards
         Racke

There are two issues that i see.

  1. I do not know how-to assign a variable USER value based on condition in the loop

Thus, instead of printing msg: “{% if item.split(‘\t’)[3] == ‘FrontEnd’ %}user1{% else %}‘user2’{% endif %}” i wish to print “{{ USER }}”

msg: “{% if item.split(‘\t’)[3] == ‘FrontEnd’ %}user1{% else %}‘user2’{% endif %}” always prints user2. I guess it is not able to match FrontEnd.

The issue could be with the if else statement or item.split(‘\t’)[3] may have some space due to which the match fails.

Below is the current output for this playbook:

- debug:
msg: "User was {{ item.split('\t')[3] }}"
with_items: "{{ command_result.stdout_lines }}"

- debug:
vars:
USER: "{% if item.split('\t')[3] == 'FrontEnd' %}user1{% else %}'user2'{% endif %}"
msg: "User is {{ USER }}"
with_items: "{{ command_result.stdout_lines }}"

- debug:

msg: "{% if item.split('\t')[3] == 'FrontEnd' %}user1{% else %}'user2'{% endif %}" with_items: "{{ command_result.stdout_lines }}"

Output:

Hello Mohtashim,
In your debug statement, try “var” instead of “vars”.

@ej does not help !

- debug:
var:
USER: "{% if item.split('\t')[3] == 'FrontEnd' %}user1{% else %}'user2'{% endif %}"
with_items: "{{ command_result.stdout_lines }}"

ERROR! 'var' is not a valid attribute for a Task
The error appears to be in '/app/testinclude.yml.bkp': line 24, column 6, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- debug:
^ here

@ej does not help !

>
-debug:
var:
USER:"{% if item.split('\t')[3] == 'FrontEnd' %}user1{% else %}'user2'{% endif %}" with_items:"{{
command_result.stdout_lines }}"
>

It's better to split this into two tasks, IMHO debug should just output the message.

- set_fact:
    USER: ....
   with_items: ...

- debug:
    msg: "{{ USER }}"

Regards
         Racke

I did this but for some reason is is not getting user1 … it is still getting user2 as the value.

`

  • set_fact:
    USER: “user1”
    when: item.split(‘\t’)[3] == “FrontEnd”
    with_items: “{{ command_result.stdout_lines }}”

  • set_fact:
    USER: “user2”
    when: item.split(‘\t’)[3] == “BackEnd”

with_items: “{{ command_result.stdout_lines }}”

  • debug:
    msg: “User has {{ USER }}”
    with_items: “{{ command_result.stdout_lines }}”

`

Output:

I did this but for some reason is is not getting user1 ... it is still getting user2 as the value.

>
-set_fact:
USER: "user1"
when:item.split('\t')[3]=="FrontEnd"
with_items:"{{ command_result.stdout_lines }}"

-set_fact:
USER: "user2"
when:item.split('\t')[3]=="BackEnd"

 with\_items:"\{\{ command\_result\.stdout\_lines \}\}"

-debug:
msg:"User has {{ USER }}"
with_items:"{{ command_result.stdout_lines }}"

Did you take in account that the array index starts with zero, so item.split('\t')[3] addresses the fourth column in
your output?

Regards
          Racke

Yes, If you look at my original post the very first post the same array item.split(‘\t’)[3] very much prints FrontEnd and BackEnd text.

Your debug statement appears to not be indebted properly.

There are no issues with the debug.

I rest this case with the experts of this forum to suggest.