Modifying a file

14 views
Skip to first unread message

Aris Aguirre

unread,
Jan 25, 2021, 1:58:45 PM1/25/21
to Ansible Project
I can easily use a file module to delete the existing file and create a new one with File1 content.
But I want to modifile the file using lineinfile or replace. Please help.
Thanks

Existing File:
This is an existing file / $ % with invalid characters &*() Blahh

Expected Output:
File1

Mauricio Tavares

unread,
Jan 25, 2021, 2:05:37 PM1/25/21
to ansible...@googlegroups.com
If you can define it in regexp you can do it.

> --
> 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 view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/863d3e1d-9091-4654-83ee-6da380cb3fe7n%40googlegroups.com.

Aris Aguirre

unread,
Jan 25, 2021, 2:37:31 PM1/25/21
to Ansible Project
you cant...cos a lot of characters there.. i cant find a way how to delete them in regex.

Antony Stone

unread,
Jan 25, 2021, 2:46:22 PM1/25/21
to ansible...@googlegroups.com
On Monday 25 January 2021 at 20:37:31, Aris Aguirre wrote:

> you cant...cos a lot of characters there.. i cant find a way how to delete
> them in regex.

Can you explain to us exactly what you want the command to identify?

Are there, for example, certain characters which should never appear in a file,
and if they do, you want the content to be replaced?

If that's not the case, then what do you want to recognise and replace? How
would you tell a person what to do?

> On Monday, January 25, 2021 at 1:05:37 PM UTC-6 raub...@gmail.com wrote:
> > On Mon, Jan 25, 2021 at 1:58 PM Aris Aguirre <aris...@gmail.com> wrote:
> > > I can easily use a file module to delete the existing file and create a
> > > new one with File1 content.
> >
> > > But I want to modifile the file using lineinfile or replace. Please
> > > help. Thanks
> > >
> > > Existing File:
> > > This is an existing file / $ % with invalid characters &*() Blahh
> > >
> > > Expected Output:
> > > File1
> >
> > If you can define it in regexp you can do it.


Regards,


Antony.

--
Police have found a cartoonist dead in his house. They say that details are
currently sketchy.

Please reply to the list;
please *don't* CC me.

nagaraju balusa

unread,
Jan 26, 2021, 5:06:39 AM1/26/21
to ansible...@googlegroups.com
 please *don't* CC me. 

Aris Aguirre

unread,
Jan 28, 2021, 12:12:52 AM1/28/21
to Ansible Project

  - name: Delete Everything in the file
    lineinfile:
      path: /tmp/filetomodify
      regexp: "*"
      line: This is for File1
      state: absent
   

Existing File:
This is an existing file / $ % with invalid characters &*() Blahh, a lot of characters here.. Everything in this file should be delete and be replace by "This is for File1"

Expected Output:
This is for File1

My solution is below. But I would like the lineinfile as solution not as below.
Thanks


- name: Delete file
  file:
      path: /tmp/filetomodify
      state: absent

- name: Add the line
  file:
      path: /tmp/filetomodify
      content: This is for File1

Stefan Hornburg (Racke)

unread,
Jan 28, 2021, 2:43:57 AM1/28/21
to ansible...@googlegroups.com
On 1/28/21 6:12 AM, Aris Aguirre wrote:
>
>   - name: Delete Everything in the file
>     lineinfile:
>       path: /tmp/filetomodify
>       regexp: "*"
>       line: This is for File1
>       state: absent
>    
>
> Existing File:
> This is an existing file / $ % with invalid characters &*() Blahh, a lot of characters here.. Everything in this file
> should be delete and be replace by "This is for File1"
>
> Expected Output:
> This is for File1
>
> My solution is below. But I would like the lineinfile as solution not as below.
> Thanks

lineinfile is definitely the wrong module if you are going to update the whole file. Try copy or content module.

Regards
Racke
> --
> 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 <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/8e5cf9e2-5db2-4423-af76-43816a412a60n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/8e5cf9e2-5db2-4423-af76-43816a412a60n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

OpenPGP_signature

Aris Aguirre

unread,
Jan 28, 2021, 2:50:54 PM1/28/21
to Ansible Project
I know, lineinfile. I was thinking of file or copy module. Like just absent/present and content as argument. My question is, how about replace? you think the module replace can help?
Thanks
Reply all
Reply to author
Forward
0 new messages