# select one and two caracter of variable

**URL:** <https://forum.ansible.com/t/select-one-and-two-caracter-of-variable/29666>\
**Category:** Ansible Project\
**Created:** [February 13, 2019, 9:26am UTC](https://forum.ansible.com/t/select-one-and-two-caracter-of-variable/29666 "2019-02-13T09:26:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryad9200000](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@ryad9200000](https://forum.ansible.com/u/ryad9200000)\
**Post date:** [February 13, 2019, 9:26am UTC](https://forum.ansible.com/t/select-one-and-two-caracter-of-variable/29666/1 "2019-02-13T09:26:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ryad9200000](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@ryad9200000](https://forum.ansible.com/u/ryad9200000)\
**Post date:** [February 13, 2019, 2:43pm UTC](https://forum.ansible.com/t/select-one-and-two-caracter-of-variable/29666/2 "2019-02-13T14:43:56Z")

</div>

upp please !!

Someone have an idea please guy !!!

If my var = “jesuisla”

How get only “jesuis”

Thanks

---

<div class="post-metadata">

**Author:** ![hugonz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hugonz/32/503_2.png) [@hugonz](https://forum.ansible.com/u/hugonz)\
**Post date:** [February 13, 2019, 8:41pm UTC](https://forum.ansible.com/t/select-one-and-two-caracter-of-variable/29666/3 "2019-02-13T20:41:52Z")

</div>

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](https://www.journaldev.com/23774/python-string-substring)

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

---

<div class="post-metadata">

**Author:** ![ryad9200000](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@ryad9200000](https://forum.ansible.com/u/ryad9200000)\
**Post date:** [February 14, 2019, 9:33am UTC](https://forum.ansible.com/t/select-one-and-two-caracter-of-variable/29666/4 "2019-02-14T09:33:19Z")

</div>

Thank you very much Hugo Gonzalez,

You are strong, it’s not easy find the solution same the more evident haha 😉

Regards,
