remplace: "{{ mon_groupe | replace('&', '\&') }}"

25 views
Skip to first unread message

ryad9...@gmail.com

unread,
Mar 8, 2019, 8:06:42 AM3/8/19
to Ansible Project
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 " \ "

Someone have an idea please !! :)

Thank you very much !!

Regards,

Mauricio Tavares

unread,
Mar 8, 2019, 8:58:11 AM3/8/19
to ansible...@googlegroups.com
On Fri, Mar 8, 2019 at 8:06 AM <ryad9...@gmail.com> 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 ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> 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.

ryad9...@gmail.com

unread,
Mar 8, 2019, 9:29:21 AM3/8/19
to Ansible Project
Thanks for your answer,

but why when show my variable i see :

"atime": 1552039856.1979122,
                "ctime": 1552039856.1899123,
                "dev": 65027,
                "gid": 1003,
                "gr_name": "ansible",
                "inode": 125043,
                "isblk": false,
                "ischr": false,
                "isdir": false,
                "isfifo": false,
                "isgid": false,
                "islnk": false,
                "isreg": true,
                "issock": false,
                "isuid": false,
                "mode": "0644",
                "mtime": 1552039856.1899123,
                "nlink": 1,
                "path": "//home/my_user/roles/http/files/groups/my_groupe.txt",
                "pw_name": "ansible",
                "rgrp": true,
                "roth": true,
                "rusr": true,
                "size": 44,
                "uid": 1003,
                "wgrp": false,
                "woth": false,
                "wusr": true,
                "xgrp": false,
                "xoth": false,
                "xusr": false

My variable must content only my_groupe.txt

ryad9...@gmail.com

unread,
Mar 8, 2019, 9:49:18 AM3/8/19
to Ansible Project
The task that i use is :

- name: Liste les groupes dans mes_groupes
  find:
    paths: /{{ role_path }}/files/groups/
  register: mes_groupes
  delegate_to: localhost

- debug:
      var: mes_groupes

Why mes_groupes no content only the file who is in my path ?


Le vendredi 8 mars 2019 14:58:11 UTC+1, Mauricio Tavares a écrit :

ryad9...@gmail.com

unread,
Mar 8, 2019, 11:17:52 AM3/8/19
to Ansible Project
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 :

Mauricio Tavares

unread,
Mar 8, 2019, 12:15:28 PM3/8/19
to ansible...@googlegroups.com
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 ~]#
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/8cacf438-cf7e-40f2-9e39-15b0388e49f0%40googlegroups.com.

Mauricio Tavares

unread,
Mar 8, 2019, 1:05:42 PM3/8/19
to ansible...@googlegroups.com
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"
Reply all
Reply to author
Forward
0 new messages