# replace string in file created by root on remote server

**URL:** <https://forum.ansible.com/t/replace-string-in-file-created-by-root-on-remote-server/27278>\
**Category:** Ansible Project\
**Created:** [April 14, 2018, 8:01am UTC](https://forum.ansible.com/t/replace-string-in-file-created-by-root-on-remote-server/27278 "2018-04-14T08:01:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Agoston\_Bejo](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@Agoston\_Bejo](https://forum.ansible.com/u/Agoston_Bejo)\
**Post date:** [April 14, 2018, 8:01am UTC](https://forum.ansible.com/t/replace-string-in-file-created-by-root-on-remote-server/27278/1 "2018-04-14T08:01:38Z")

</div>

Hi!

What I’m trying to do is very “complicated” task:

1. create a file on the remote server as root (in /root)
2. replace a string in the file

I just can’t seem to achieve this without resorting to executing shell commands directly. (Any more than I already am, that is.)

Version a)

- name: create the file as root  
shell: ‘echo “Test\_string” \> /root/test.txt’  
remote\_user: root

- name: replace ‘_’ with ‘-’ in the file  
replace:  
path: “/root/test.txt”  
regexp: '_’  
replace: ‘-’  
backup: no

==\> it fails, because the replace task doesn’t accept either remote\_user or sudo\_user as a parameter, so it can only be run as the user running the playbook, and that user can’t modify a file created by root

Version b)

- name: create release information file for all awwphp applications  
shell: ‘echo “Test\_string” \> /tmp/test.txt’

# no remote\_user!

- name: replace ‘_’ with ‘-’ in the file  
replace:  
path: “/tmp/test.txt”  
regexp: '_’  
replace: ‘-’  
backup: no

- name: copy test.txt into /root  
copy:  
src: “/tmp/test.txt”  
dest: “/root/test.txt”  
remote\_src: yes

==\> this fails here, because this task doesn’t accept remote\_user/sudo\_user either, and the regular user running the playbook doesn’t have rights to write into /root

How do you do that with Ansible?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [April 14, 2018, 8:42am UTC](https://forum.ansible.com/t/replace-string-in-file-created-by-root-on-remote-server/27278/2 "2018-04-14T08:42:18Z")

</div>

remote\_user and become\_user(sudo\_user is depricated) is a directive for the task, not the module so you indentation is probably wrong.  
They must be on the same level as replace.

---

<div class="post-metadata">

**Author:** ![Agoston\_Bejo](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@Agoston\_Bejo](https://forum.ansible.com/u/Agoston_Bejo)\
**Post date:** [April 15, 2018, 7:32am UTC](https://forum.ansible.com/t/replace-string-in-file-created-by-root-on-remote-server/27278/3 "2018-04-15T07:32:27Z")

</div>

Hi Kai,

indeed, that seemed to be the problem!

Thank you very much!
