Trying to use lineinfile to add a multiline entry to a logrotate file. The entry needs to look like the following:
/var/www/client/logs/*log {
missingok
notifempty
sharedscripts
compress
monthly
Keep 20 years worth of logs
rotate 240
}
The action that I am using is this:
Add client entries to log scripts and cron jobs.
- name: add client to courseleaf-httpd logrotate
lineinfile: “dest=/etc/logrotate.d/courseleaf-httpd state=present regexp=‘# NOTE:’ insertbefore=‘/var/www/{{ clientname }}/logs/*log {\n missingok\n sharedscripts\n compress\n monthly\n # Keep 20 years worth of logs\n rotate 240\n}’”
The error I am receiving is the following:
fatal: [olympic] => failed to parse: Traceback (most recent call last):
File “/root/.ansible/tmp/ansible-1383251993.34-129818210269435/lineinfile”, line 1255, in
main()
File “/root/.ansible/tmp/ansible-1383251993.34-129818210269435/lineinfile”, line 319, in main
ins_aft, ins_bef, create, backup, backrefs)
File “/root/.ansible/tmp/ansible-1383251993.34-129818210269435/lineinfile”, line 208, in present
if lines[index[0]] != new_line + os.linesep:
TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘str’
FATAL: all hosts have already failed – aborting
Thanks for any help.
-Eric
“Trying to use lineinfile to add a multiline entry to a logrotate file. The entry needs to look like the following:”
This is going to be basically impossible, since it’s “lineinfile”
I would highly recommend templating the resultant file – your OS should have a logrotate.d already.
Nothing like over looking the obvious. Thanks.
I was able to make it work. I kept at it because the file is always being appended as I add a new client to the hosting solution. The file resides in logrotate.d and specifies location of client logs and attributes.
After messing round with {{ item }} and finally getting the line= to respect the \n. I had it add the place holder for the next client (# New Client) so that the regexp would work for subsequent client. My mistake was to use the insertbefore for the actual insertion. Having insertbefore equal the regexp and then using line= to perform the actual insertion.
lineinfile: “dest=/etc/logrotate.d/courseleaf-httpd state=present regexp=‘# New Client’ line=‘/var/www/{{ clientname }}/logs/*log {\n missingok\n notifempty\n sharedscripts\n compress\n monthly\n # Keep 20 years of logs\n rotate 240\n}\n\n# New Client’”
-Eric
I don’t think Ansible likes the looks of that
I am still searching for a more eloquent way. It almost seems as if I need to copy the current file into a template, the copy it back. But as soon as I template ro copy the old file or template is out of date. Appending or adding multi line content to an existing file is the task. I have only played with Ansible for 3 days, so I can definitely spend more time in the documentation.
Thanks.
I see you’re actually pasting the file into logrotate.d, I just mean use the template module to overwrite the file.
There seems to be no reason to try to perform editing voodoo on that file – maintain an authorative copy in version control.