select one and two caracter of variable

18 views
Skip to first unread message

ryad9...@gmail.com

unread,
Feb 13, 2019, 4:26:22 AM2/13/19
to Ansible Project
Hi all,

I don't now why this task not works:

- set_fact:
      my_variable: "{{ ansible_user[0 and 1] + 'gis' if ansible_user!='wildfly' else 'wildfly' }}"

But ansible_user[0] + ansible_user[1] works but i want one syntaxe shorter, exemple ansible_user[0 and 1] but this syntax not works !!

Someone have an idea please guy !!

Thank you very much

ryad9...@gmail.com

unread,
Feb 13, 2019, 9:43:56 AM2/13/19
to Ansible Project
upp please !!

Someone have an idea please guy !!!

If my var = "jesuisla"

How get only "jesuis"

Thanks

Hugo Gonzalez

unread,
Feb 13, 2019, 3:42:02 PM2/13/19
to ansible...@googlegroups.com


On 2/13/19 3:26 AM, ryad9...@gmail.com wrote:
Hi all,

I don't now why this task not works:

- set_fact:
      my_variable: "{{ ansible_user[0 and 1] + 'gis' if ansible_user!='wildfly' else 'wildfly' }}"

But ansible_user[0] + ansible_user[1] works but i want one syntaxe shorter, exemple ansible_user[0 and 1] but this syntax not works !!


Why would you expect it to work?  'and' is a logical operator.  Jinja2 (which is used for templating a in Ansible) uses python slicing and substrings.

Take a look here:

https://www.journaldev.com/23774/python-string-substring


The syntax you're looking for is slicing:   variable[start:end]


ryad9...@gmail.com

unread,
Feb 14, 2019, 4:33:20 AM2/14/19
to Ansible Project
Thank you very much Hugo Gonzalez,

You are strong, it's not easy find the solution same the more evident haha ;)

Regards,
Reply all
Reply to author
Forward
0 new messages