使用 lineinfile 插入行但未按预期工作
using lineinfile to insert line but not working as expected
我正在使用 lineinfile
在 syslog 文件中插入行。这是我的系统日志:
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
missingok
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}
我想在 missingok
之后添加 compress
和 delaycompress
。这是我的代码:
- name: "Adding compress line in /etc/logrotate.d/syslog"
lineinfile:
path: /etc/logrotate.d/syslog
insertafter: "^missingok"
line: " compress"
firstmatch: yes
state: present
- name: "Adding delaycompress line in /etc/logrotate.d/syslog"
lineinfile:
path: /etc/logrotate.d/syslog
insertbefore: "^sharedscripts"
line: " delaycompress"
firstmatch: yes
state: present
但它在文件末尾(最后一行)添加了两者。
注意:我在compress
和delaycompress
前加了4个空格。
这是因为插入符号 ^
在正则表达式中匹配字符串的开头而不消耗任何字符。
并且因为您在 missingok
和 sharedscripts
之前确实有 space,所以您的 insertafter
和 insertbefore
正则表达式 are matching nothing.
要解决此问题,您可以在匹配任何 space 的 \s
的帮助下仅在行的开头允许 space 和 space,制表符或换行符以及匹配零个或多个连续字符的星号 *
。
所以正确的正则表达式是
-
^\s*missingok
Test it here
-
^\s*sharedscripts
你的任务的解决方法是:
- name: "Adding compress line in /etc/logrotate.d/syslog"
lineinfile:
path: /etc/logrotate.d/syslog
insertafter: "^\s*missingok"
line: " compress"
firstmatch: yes
state: present
- name: "Adding delaycompress line in /etc/logrotate.d/syslog"
lineinfile:
path: /etc/logrotate.d/syslog
insertbefore: "^\s*sharedscripts"
line: " delaycompress"
firstmatch: yes
state: present
注意,因为Ansible是一个Python应用程序,backslashes \
have a special meaning and have to be escaped.
我正在使用 lineinfile
在 syslog 文件中插入行。这是我的系统日志:
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
missingok
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}
我想在 missingok
之后添加 compress
和 delaycompress
。这是我的代码:
- name: "Adding compress line in /etc/logrotate.d/syslog"
lineinfile:
path: /etc/logrotate.d/syslog
insertafter: "^missingok"
line: " compress"
firstmatch: yes
state: present
- name: "Adding delaycompress line in /etc/logrotate.d/syslog"
lineinfile:
path: /etc/logrotate.d/syslog
insertbefore: "^sharedscripts"
line: " delaycompress"
firstmatch: yes
state: present
但它在文件末尾(最后一行)添加了两者。
注意:我在compress
和delaycompress
前加了4个空格。
这是因为插入符号 ^
在正则表达式中匹配字符串的开头而不消耗任何字符。
并且因为您在 missingok
和 sharedscripts
之前确实有 space,所以您的 insertafter
和 insertbefore
正则表达式 are matching nothing.
要解决此问题,您可以在匹配任何 space 的 \s
的帮助下仅在行的开头允许 space 和 space,制表符或换行符以及匹配零个或多个连续字符的星号 *
。
所以正确的正则表达式是
-
Test it here^\s*missingok
-
^\s*sharedscripts
你的任务的解决方法是:
- name: "Adding compress line in /etc/logrotate.d/syslog"
lineinfile:
path: /etc/logrotate.d/syslog
insertafter: "^\s*missingok"
line: " compress"
firstmatch: yes
state: present
- name: "Adding delaycompress line in /etc/logrotate.d/syslog"
lineinfile:
path: /etc/logrotate.d/syslog
insertbefore: "^\s*sharedscripts"
line: " delaycompress"
firstmatch: yes
state: present
注意,因为Ansible是一个Python应用程序,backslashes \
have a special meaning and have to be escaped.