Issue with blockinfile overwriting entire file in version 2.2

760 views
Skip to first unread message

Dave

unread,
Oct 27, 2017, 3:04:13 PM10/27/17
to Ansible Project
It appears when I repeatedly use blockinfile in an Ansible script on the same file, it overwrites any text that was previously in the file before. I'm running version 2.2 and I'm noticing it's also defining markers in the file that aren't referenced in my Ansible script. I've also tried specifying insertafter: EOF and insertafter: "previous line syntax", but it seems I can only use the blockinfile module once in the script, from the overwrites. Any advice on how to resolve?

Zimidar Boy

unread,
Oct 30, 2017, 12:38:15 PM10/30/17
to Ansible Project
Can you give me more details like your script and file you are changing.
Message has been deleted

Dave

unread,
Oct 30, 2017, 2:30:10 PM10/30/17
to Ansible Project
Sure, please see below:

---
- hosts: all
  remote_user
: local_user
  become
: yes
  become_user
: root
  become_method
: sudo


  vars_prompt
:
   
- name: "set_newsite"
     prompt
: "What is the name of the new site to create?"
     
private: no


  tasks
:
   
- name: Create new config file
     file
:
       state
: touch
       path
: "/var/www/html/{{ set_newsite }}.cfg"
       owner
: apache2
       
group: apache2
       mode
: 0644


   
- name: Add test data to config file
     blockinfile
:
       dest
: "/var/www/html/{{ set_newsite }}.cfg"
       block
: |
          test1
          test2
          test3


   
- name: Confirm variable insertion into test config file
      lineinfile
:
       destfile
: "/var/www/html/{{ set_newsite }}.cfg"
       insertafter
: "test3"
       line
: 'Name of new site is: {{ set_newsite }}'


   
- name: Second confirmation
     
when: set_newsite | match("198.7.203.")
     lineinfile
:
       destfile
: "/var/www/html/{{ set_newsite }}.cfg"
       insertafter
: "Name of new site"
       line
: 'new variable'


   
- name: Add more block data to test config file
     blockinfile
:
       dest
: "/var/www/html/{{ set_newsite }}.cfg"
       insertafter
: EOF
       block
: |
          test4
          test5
          test6

It's a simple script that adds lines in a test config file to ensure the basics work before I add code to it. When I edit out the second 'blockinfile' command, I get everything before the word 'test4', so I'm definitely seeing something is up.

Kai Stian Olstad

unread,
Oct 30, 2017, 2:40:00 PM10/30/17
to ansible...@googlegroups.com
On 30. okt. 2017 19:30, Dave wrote:
> - name: Add more block data to test config file
> blockinfile:
> dest: "/var/www/html/{{ set_newsite }}.cfg"
> insertafter: EOF
> block: |
> test4
> test5
> test6
>
> It's a simple script that adds lines in a test config file to ensure the
> basics work before I add code to it. When I edit out the second
> 'blockinfile' command, I get everything before the word 'test4', so I'm
> definitely seeing something is up.

When using several blockinfile on same file it's essential that you set unique marker for each blockinfile, if not the second one will overwrite the first one.

--
Kai Stian Olstad

Dave

unread,
Oct 31, 2017, 10:42:56 AM10/31/17
to Ansible Project
I see. Thanks Kai, I'll give it a try.
Reply all
Reply to author
Forward
0 new messages