ansible / ansible-modules-core

Ansible modules - these modules ship with ansible
1.3k stars 1.95k forks source link

lineinfile and newlines causing duplicated entries #1379

Closed bhcopeland closed 9 years ago

bhcopeland commented 9 years ago

Issue Type:

Bug Report

Ansible Version:

ansible 1.9.1 - Arch Linux 1.9.1-1

Ansible Configuration:

< log_path = /var/log/ansible.log

---
> #log_path = /var/log/ansible.log
159c159
< retry_files_enabled = False

---
> #retry_files_enabled = False

Environment:

Arch Linux Deploying to Ubuntu 14.04

Summary:

When using newlines /n with lineinfile, it will duplicate lines. Whilst if I use it without \n it only adds it once.

Steps To Reproduce:

- name: Enable mmonit
  lineinfile: dest=/etc/monit/monitrc
              insertafter=EOF
              state=present
              line="set httpd port 2812 and \n
                    use address localhost \n
                    allow localhost"
  notify: restart monit

I will get duplicated lines.

- name: Enable mmonit
  lineinfile: dest=/etc/monit/monitrc
              insertafter=EOF
              state=present
              line="set httpd port 2812 and
                    use address localhost
                    allow localhost"
  notify: restart monit

I will not get duplicated lines

Expected Results:

set httpd port 2812 and
use address localhost
allow localhost

Once at the bottom of the file

Actual Results:

set httpd port 2812 and
use address localhost
allow localhost
set httpd port 2812 and
use address localhost
allow localhost
... etc
sivel commented 9 years ago

Is /n something special for monit? I'm assuming you want a newline? A newline is \n. Not that this change fixes the problem, I can't claim that.

bhcopeland commented 9 years ago

Yeah, I meant /n, not \n. My bad.

abadger commented 9 years ago

Unfortunately, lineinfile isn't intended to work with multiple lines. http://docs.ansible.com/lineinfile_module.html

This is primarily useful when you want to change a single line in a file only. See the replace module if you want to change multiple, similar lines; for other cases, see the copy or template modules.

As the documentation says, template or replace may be better modules for you to look into. I have also used multiple lineinfile tasks one right after the other when the lines I was adding were very simple.

You can hit up the mailing list if you need more help constructing a set of tasks for your particular problem.

List Information

Hi!

Thanks very much for your interest in Ansible. It sincerely means a lot to us.

This appears to be a user question, and we'd like to direct these kinds of things to either the mailing list or the IRC channel.

If you can stop by there, we'd appreciate it. This allows us to keep the issue tracker for bugs, pull requests, RFEs and the like.

Thank you once again and we look forward to seeing you on the list or IRC. Thanks!