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
}}"
2.
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 }}"
- debug:
msg: "{% if item.split('\t')[3] == 'FrontEnd' %}user1{% else
%}'user2'{% endif %}"
Output:
TASK [debug]
> ************************************************************************************************************************************************
> ok: [localhost] => (item=10.12.1.13 10.12.1.13\n-rw-rw-r-- user1
> 2019-09-13 15:39 /was//testingjsp/testingcom.jsp 1786385840
> /was//testingjsp FrontEnd) => {
> "msg": "User was FrontEnd"
> }
> ok: [localhost] => (item=10.12.1.13 10.12.1.13\n-rw-rw-r-- user2
> 2019-09-13 15:29 /fin/scripts/testingscr.scr 367595418\n-rw-rw-r-- user2
> 2019-09-13 15:36 /fin/mrt/testingmrt.mrt 1251350031\n-rw-rw-r-- user2
> 2019-09-13 15:37 /fin/exe/testingexe.exe 1390265645\n-rw-rw-r-- user2
> 2019-09-13 15:38 /fin/com/testingcom.com90193476
> /fin/scripts\n/fin/mrt\n/fin/exe\n/fin/com BackEnd) => {
> "msg": "User was BackEnd"
> }
> TASK [debug]
> ************************************************************************************************************************************************
> ok: [localhost] => (item=10.12.1.13 10.12.1.13\n-rw-rw-r-- user1
> 2019-09-13 15:39 /was//testingjsp/testingcom.jsp 1786385840
> /was//testingjsp FrontEnd) => {
> "msg": "'user2'"
> }
> ok: [localhost] => (item=10.12.1.13 10.12.1.13\n-rw-rw-r-- user2
> 2019-09-13 15:29 /fin/scripts/testingscr.scr 367595418\n-rw-rw-r-- user2
> 2019-09-13 15:36 /fin/mrt/testingmrt.mrt 1251350031\n-rw-rw-r-- user2
> 2019-09-13 15:37 /fin/exe/testingexe.exe 1390265645\n-rw-rw-r-- user2
> 2019-09-13 15:38 /fin/com/testingcom.com90193476
> /fin/scripts\n/fin/mrt\n/fin/exe\n/fin/com BackEnd) => {
> "msg": "'user2'"
> }
> TASK [debug]
> ************************************************************************************************************************************************
> ok: [localhost] => (item=10.12.1.13 10.12.1.13\n-rw-rw-r-- user1
> 2019-09-13 15:39 /was//testingjsp/testingcom.jsp 1786385840
> /was//testingjsp FrontEnd) => {
> "msg": "Hello world!"
> }
> ok: [localhost] => (item=10.12.1.13 10.12.1.13\n-rw-rw-r-- user2
> 2019-09-13 15:29 /fin/scripts/testingscr.scr 367595418\n-rw-rw-r-- user2
> 2019-09-13 15:36 /fin/mrt/testingmrt.mrt 1251350031\n-rw-rw-r-- user2
> 2019-09-13 15:37 /fin/exe/testingexe.exe 1390265645\n-rw-rw-r-- user2
> 2019-09-13 15:38 /fin/com/testingcom.com90193476
> /fin/scripts\n/fin/mrt\n/fin/exe\n/fin/com BackEnd) => {
> "msg": "Hello world!"
> }
On Wednesday, September 25, 2019 at 3:30:19 PM UTC+5:30, Stefan Hornburg
(Racke) wrote:
>
> On 9/25/19 11:13 AM, Mohtashim S wrote:
> > @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
>
> > You can recreate the playbook output.
> >
> > On Wednesday, September 25, 2019 at 2:23:22 PM UTC+5:30, Stefan Hornburg
> (Racke) wrote:
> >
> > On 9/25/19 10:44 AM, Mohtashim S wrote:
> > > @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
> >
> >
> > > TASK [debug]
> > >
> >
> ************************************************************************************************************************************************
>
>
> >
> > > ok: [localhost] => (item=10.12.1.13 10.12.1.13\n-rw-rw-r--
> user1 2019-09-13 15:39
> > > /was//testingjsp/testingcom.jsp 1786385840 /was//testingjsp
> FrontEnd) => {
> > > "msg": "User was FrontEnd"
> > > }
> > > ok: [localhost] => (item=10.12.1.13 10.12.1.13\n-rw-rw-r--
> user2 2019-09-13 15:29 /fin/scripts/testingscr.scr
> > > 367595418\n-rw-rw-r-- user2 2019-09-13 15:36
> /fin/mrt/testingmrt.mrt 1251350031\n-rw-rw-r-- user2 2019-09-13
> > 15:37
> > > /fin/exe/testingexe.exe 1390265645\n-rw-rw-r-- user2
> 2019-09-13 15:38 /fin/com/testingcom.com
> > <http://testingcom.com> 90193476
> > > /fin/scripts\n/fin/mrt\n/fin/exe\n/fin/com BackEnd) => {
> > > "msg": "User was BackEnd"
> > > }
> > > TASK [debug]
> > >
> >
> ************************************************************************************************************************************************
>
>
> >
> > > ok: [localhost] => (item=10.12.1.13 10.12.1.13\n-rw-rw-r--
> user1 2019-09-13 15:39
> > > /was//testingjsp/testingcom.jsp 1786385840 /was//testingjsp
> FrontEnd) => {
> > > "msg": "Hello world!"
> > > }
> > > ok: [localhost] => (item=10.12.1.13 10.12.1.13\n-rw-rw-r--
> user2 2019-09-13 15:29 /fin/scripts/testingscr.scr
> > > 367595418\n-rw-rw-r-- user2 2019-09-13 15:36
> /fin/mrt/testingmrt.mrt 1251350031\n-rw-rw-r-- user2 2019-09-13
> > 15:37
> > > /fin/exe/testingexe.exe 1390265645\n-rw-rw-r-- user2
> 2019-09-13 15:38 /fin/com/testingcom.com
> > <http://testingcom.com> 90193476
> > > /fin/scripts\n/fin/mrt\n/fin/exe\n/fin/com BackEnd) => {
> > > "msg": "Hello world!"
> > > }
> > >
> > >
> > > On Wednesday, September 25, 2019 at 1:40:38 PM UTC+5:30, Stefan
> Hornburg (Racke) wrote:
> > >
> > > On 9/25/19 9:58 AM, Mohtashim S wrote:
> > > > | - 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
> > >
> > > > --
> > > > 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
> > > > [email protected] <javascript:> <mailto:
> [email protected] <javascript:> <javascript:>
> > <javascript:>>.
> > > > To view this discussion on the web visit
> > > >
> https://groups.google.com/d/msgid/ansible-project/11286705-639c-43e2-8e0d-47080203cfc1%40googlegroups.com
>
> > <
> https://groups.google.com/d/msgid/ansible-project/11286705-639c-43e2-8e0d-47080203cfc1%40googlegroups.com>
>
>
> > > <
> https://groups.google.com/d/msgid/ansible-project/11286705-639c-43e2-8e0d-47080203cfc1%40googlegroups.com
>
> > <
> https://groups.google.com/d/msgid/ansible-project/11286705-639c-43e2-8e0d-47080203cfc1%40googlegroups.com>>
>
>
> > > >
> > >
> > <
> https://groups.google.com/d/msgid/ansible-project/11286705-639c-43e2-8e0d-47080203cfc1%40googlegroups.com?utm_medium=email&utm_source=footer
>
> > <
> https://groups.google.com/d/msgid/ansible-project/11286705-639c-43e2-8e0d-47080203cfc1%40googlegroups.com?utm_medium=email&utm_source=footer>
>
>
> >
> > >
> > <
> https://groups.google.com/d/msgid/ansible-project/11286705-639c-43e2-8e0d-47080203cfc1%40googlegroups.com?utm_medium=email&utm_source=footer
>
> > <
> https://groups.google.com/d/msgid/ansible-project/11286705-639c-43e2-8e0d-47080203cfc1%40googlegroups.com?utm_medium=email&utm_source=footer>>>.
>
>
> >
> > >
> > >
> > >
> > > --
> > > Ecommerce and Linux consulting + Perl and web application
> programming.
> > > Debian and Sympa administration. Provisioning with Ansible.
> > >
> > > --
> > > 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
> > > [email protected] <javascript:> <mailto:
> [email protected] <javascript:>
> <javascript:>>.
> > > To view this discussion on the web visit
> > >
> https://groups.google.com/d/msgid/ansible-project/d2354140-209e-4554-b057-95514e0ba08c%40googlegroups.com
>
> > <
> https://groups.google.com/d/msgid/ansible-project/d2354140-209e-4554-b057-95514e0ba08c%40googlegroups.com>
>
>
> > >
> > <
> https://groups.google.com/d/msgid/ansible-project/d2354140-209e-4554-b057-95514e0ba08c%40googlegroups.com?utm_medium=email&utm_source=footer
>
> > <
> https://groups.google.com/d/msgid/ansible-project/d2354140-209e-4554-b057-95514e0ba08c%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
>
> >
> >
> >
> > --
> > Ecommerce and Linux consulting + Perl and web application
> programming.
> > Debian and Sympa administration. Provisioning with Ansible.
> >
> > --
> > 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
> > [email protected] <javascript:> <mailto:
> [email protected] <javascript:>>.
> > To view this discussion on the web visit
> >
> https://groups.google.com/d/msgid/ansible-project/0dec0bef-8fdd-436b-8872-114d8c54ef3b%40googlegroups.com
>
> > <
> https://groups.google.com/d/msgid/ansible-project/0dec0bef-8fdd-436b-8872-114d8c54ef3b%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
>
>
>
> --
> Ecommerce and Linux consulting + Perl and web application programming.
> Debian and Sympa administration. Provisioning with Ansible.
>
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/ceeaf59b-c8e9-434e-b285-c0cf33b8f625%40googlegroups.com.