# handle backslash in ansible

**URL:** <https://forum.ansible.com/t/handle-backslash-in-ansible/32593>\
**Category:** Ansible Project\
**Created:** [April 3, 2020, 4:01am UTC](https://forum.ansible.com/t/handle-backslash-in-ansible/32593 "2020-04-03T04:01:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Himanshu\_Pupneja](https://avatars.discourse-cdn.com/v4/letter/h/9de053/32.png) [@Himanshu\_Pupneja](https://forum.ansible.com/u/Himanshu_Pupneja)\
**Post date:** [April 3, 2020, 4:01am UTC](https://forum.ansible.com/t/handle-backslash-in-ansible/32593/1 "2020-04-03T04:01:45Z")

</div>

I have a variable which has value: “domain\user”. I am trying to split to get the user name

I have tried the below code which works well but can’t handle escape sequence like \n \r etc. for example “domain\naman”

Could someone please check and let me know if I am missing something here.

tasks:

- name: handle escape character in domain user

set\_fact:

username: “{{ domain\_username.split(separator)[-1] }}”

vars:

separator: ''

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [April 3, 2020, 7:09am UTC](https://forum.ansible.com/t/handle-backslash-in-ansible/32593/2 "2020-04-03T07:09:28Z")

</div>

Use "Single-Quoted Style" to declare domain\_username.  
[https://yaml.org/spec/1.2/spec.html#id2788097](https://yaml.org/spec/1.2/spec.html#id2788097)

For example

- hosts: localhost  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;dname: 'domain\naman'  
&nbsp;&nbsp;&nbsp;&nbsp;sep: '\'  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ dname.split(sep)[-1] }}"

gives

&nbsp;&nbsp;&nbsp;&nbsp;"msg": "naman"

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [April 3, 2020, 9:14am UTC](https://forum.ansible.com/t/handle-backslash-in-ansible/32593/3 "2020-04-03T09:14:45Z")

</div>

> It worked when domain user is inside the playbook. but it doesn't work when  
> passed at runtime.
> 
> ansible-playbook play.yml -e dname='domain\namam'
> 
> \>  
> \> Use "Single-Quoted Style" to declare domain\_username.  
> \> [https://yaml.org/spec/1.2/spec.html#id2788097](https://yaml.org/spec/1.2/spec.html#id2788097)  
> \>  
> \> For example  
> \>  
> \> - hosts: localhost  
> \> vars:  
> \> dname: 'domain\naman'  
> \> sep: '\'  
> \> tasks:  
> \> - debug:  
> \> msg: "{{ dname.split(sep)[-1] }}"  
> \>  
> \> gives  
> \>  
> \> "msg": "naman"

Right. Command-line can't work. It's evaluated by the shell. Put the extra  
variables into a file and let YAML to evaluate the "single-quoted" variables.  
For example the command-line below works fine again

> cat myvar.yml

dname: 'domain\namam'

> cat playbook.yml

- hosts: localhost  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ dname.split(sep)[-1] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sep: '\'

> ansible-playbook playbook.yml -e @myvar.yml

...  
TASK [debug]
