Easy_King
(Easy King)
September 25, 2019, 7:58am
1
- 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 ?
racke
(Stefan Hornburg)
September 25, 2019, 8:10am
2
> - 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
Easy_King
(Easy King)
September 25, 2019, 8:44am
3
@Stefan here is the output
racke
(Stefan Hornburg)
September 25, 2019, 8:52am
4
@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
Easy_King
(Easy King)
September 25, 2019, 9:13am
5
@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.
Easy_King
(Easy King)
September 25, 2019, 9:34am
6
Here is my entire playbook:
`
racke
(Stefan Hornburg)
September 25, 2019, 9:59am
7
@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
Easy_King
(Easy King)
September 25, 2019, 11:13am
8
There are two issues that i see.
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:
jolleye
(Eric)
September 25, 2019, 11:29am
9
Hello Mohtashim,
In your debug statement, try “var” instead of “vars”.
Easy_King
(Easy King)
September 25, 2019, 11:49am
10
@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
racke
(Stefan Hornburg)
September 25, 2019, 11:52am
11
@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
Easy_King
(Easy King)
September 25, 2019, 12:15pm
12
I did this but for some reason is is not getting user1 … it is still getting user2 as the value.
`
with_items: “{{ command_result.stdout_lines }}”
debug:
msg: “User has {{ USER }}”
with_items: “{{ command_result.stdout_lines }}”
`
Output:
racke
(Stefan Hornburg)
September 25, 2019, 12:20pm
13
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
Easy_King
(Easy King)
September 25, 2019, 12:27pm
14
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.
jolleye
(Eric)
September 25, 2019, 1:27pm
15
Your debug statement appears to not be indebted properly.
Easy_King
(Easy King)
September 25, 2019, 5:00pm
16
There are no issues with the debug.
I rest this case with the experts of this forum to suggest.