In a string instead of in a file:
raub@desktop:~/dev/ansible$ cat roles/test/tasks/string.yml
---
# file tasks/string
# String tests
- name: Create strings
set_fact:
bob: 'Left & Right'
- debug:
var: bob
- name: Edit strings
set_fact:
new_bob: "{{ bob | regex_replace ('&', '\\&') }}"
- debug:
var: new_bob
raub@desktop:~/dev/ansible$
TASK [test : Create strings] ***************************************************
ok: [testcentos]
TASK [test : debug] ************************************************************
ok: [testcentos] => {
"bob": "Left & Right"
}
TASK [test : Edit strings] *****************************************************
ok: [testcentos]
TASK [test : debug] ************************************************************
ok: [testcentos] => {
"new_bob": "Left \\& Right"
}
On Fri, Mar 8, 2019 at 12:15 PM Mauricio Tavares <[email protected]> wrote:
>
> raub@desktop:~/dev/ansible$ cat roles/test/tasks/file.yml
> ---
> # File tests
>
> - name: upload a temp file
> copy:
> src: testfile
> dest: /tmp/testfile
>
> - name: The contents of testfile
> shell: cat /tmp/testfile
> register: file_contents
>
> - debug:
> var: file_contents.stdout
>
> - name: now change something in it
> replace:
> path: /tmp/testfile
> regexp: '(\&)'
> replace: '\&'
>
> - name: The contents of modified testfile
> shell: cat /tmp/testfile
> register: file_contents
>
> - debug:
> var: file_contents.stdout
> raub@desktop:~/dev/ansible$
>
> raub@desktop:~/dev/ansible$ cat roles/test/files/testfile
> This file has text & spaces
> Do not ask me why
> raub@desktop:~/dev/ansible$
>
> raub@desktop:~/dev/ansible$ ansible-playbook test.yml --limit testcentos
> [...]
> TASK [test : upload a temp file]
> ***********************************************
> changed: [testcentos]
>
> TASK [test : The contents of testfile]
> *****************************************
> changed: [testcentos]
>
> TASK [test : debug]
> ************************************************************
> ok: [testcentos] => {
> "file_contents.stdout": "This file has text & spaces\nDo not ask me why"
> }
>
> TASK [test : now change something in it]
> ***************************************
> changed: [testcentos]
>
> TASK [test : The contents of modified testfile]
> ********************************
> changed: [testcentos]
>
> TASK [test : debug]
> ************************************************************
> ok: [testcentos] => {
> "file_contents.stdout": "This file has text \\& spaces\nDo not ask me why"
> }
>
> [root@testcentos ~]# cat /tmp/testfile
> This file has text \& spaces
> Do not ask me why
> [root@testcentos ~]#
>
> On Fri, Mar 8, 2019 at 11:18 AM <[email protected]> wrote:
> >
> > Same answer '\\&' not works !!
> >
> > someone have an idea please why replace('&', '\&') not works ?!!
> >
> > Thank you very much
> >
> > Regards,
> >
> > Le vendredi 8 mars 2019 14:58:11 UTC+1, Mauricio Tavares a écrit :
> >>
> >> On Fri, Mar 8, 2019 at 8:06 AM <[email protected]> wrote:
> >> >
> >> > hi all,
> >> >
> >> > I want replace all caracter " & " by " \& "
> >> >
> >> >
> >> > - set_fact:
> >> > remplace: "{{ mon_groupe | replace('&', '\&') }}"
> >> >
> >> > but when i use this task it's not work.
> >> > It's works only i skip " \ "
> >> >
> >> Try '\\&' instead
> >>
> >> > Someone have an idea please !! :)
> >> >
> >> > Thank you very much !!
> >> >
> >> > Regards,
> >> >
> >> > --
> >> > 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 post to this group, send email to [email protected].
> >> > To view this discussion on the web visit
> >> > https://groups.google.com/d/msgid/ansible-project/bdfa8028-f922-471b-94b5-1d7b81726a6c%40googlegroups.com.
> >> > For more options, visit https://groups.google.com/d/optout.
> >
> > --
> > 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 post to this group, send email to [email protected].
> > To view this discussion on the web visit
> > https://groups.google.com/d/msgid/ansible-project/8cacf438-cf7e-40f2-9e39-15b0388e49f0%40googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
--
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/CAHEKYV7GPmf67w8h014veFkX79LHAnsO_5SZWNtTo2KjR0ByYg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.