[英]ansible lineinfile module to replace a single line with multiple lines
I have a ansible play which works correctly as follows, here i have two From
entries which are being changed with TO
entries. 我有一个很正常的游戏,其工作原理如下,这里有两个
From
条目,这些条目正在被TO
条目更改。
But i'm just wondering if there is way where i can replace one line with two lines in a file called ntp.conf
in my case. 但是我只是想知道是否有办法在我的情况下用
ntp.conf
文件中的两行替换一行。
---
- name: Play to correct the config for NTP clients
hosts: all
remote_user: root
gather_facts: False
tasks:
- name: Changing the ntp server configuration on the client
lineinfile:
path: /etc/ntp.conf
### line to be searched & matched
regexp: '{{ item.From }}'
### line to be in placed
line: '{{ item.To }}'
state: present
backup: yes
backrefs: yes
with_items:
- { From: 'server ros-ntp minpoll 4 maxpoll 10', To: 'server ros-gw.fuzzy.com minpoll 4 maxpoll 10'}
- { From: 'server ros-ntp-b minpoll 4 maxpoll 10', To: 'server ros-b-gw.fuzzy.com minpoll 4 maxpoll 10'}
notify: restart_ntp_service
handlers:
- name: restart_ntp_service
service:
name: ntpd
state: restarted
You'll need to use blockinfile
to add multiple lines to ntp.conf
. 您需要使用
blockinfile
将多行添加到ntp.conf
。 You can use lineinfile
to replace the line you're targeting with a comment, then use the insertafter
parameter of blockinfile
to add your lines after it. 您可以使用
lineinfile
将目标行替换为注释,然后使用insertafter
参数在blockinfile
添加行。
Here is the blockinfile
documentation . 这是
blockinfile
文档 。
Alternatively you could use two lineinfile
tasks and leverage the insertafter
property. 或者,您可以使用两个
lineinfile
任务并利用insertafter
属性。 Something like this: 像这样:
- name: Set NTP server to use ros-ntp-b
lineinfile:
path: /etc/ntp.conf
regexp: 'server ros-ntp-?b? minpoll 4 maxpoll 10'
line: 'server ros-ntp-b minpoll 4 maxpoll 10'
state: present
backup: no
- name: Add NTP server config for ros-ntp-gw
lineinfile:
path: /etc/ntp.conf
regexp: 'server ros-ntp-rw minpoll 4 maxpoll 10'
line: 'server ros-ntp-gw minpoll 4 maxpoll 10'
insertafter: 'server ros-ntp-b minpoll 4 maxpoll 10'
state: present
backup: yes
I got the work around as follows with using lineinfile
module, still looking for the other way around if someone comes across. 通过使用
lineinfile
模块,我得到了如下解决方法,如果有人遇到,我仍然在寻找另一种方法。 Just placing the working answer below for posterity ... 只需将以下工作答案放在后代...
---
- name: Play to correct the config for NTP clients
hosts: all
remote_user: root
gather_facts: False
tasks:
- name: Changing the ntp server configuration on the client
lineinfile:
path: /etc/ntp.conf
### line to be searched & matched
regexp: 'server ros-ntp minpoll 4 maxpoll 10'
### line to be in placed
line: "server ros-ntp-b minpoll 4 maxpoll 10\nserver ros-ntp-gw minpoll 4 maxpoll 10"
state: present
backup: yes
backrefs: yes
notify: restart_ntp_service
handlers:
- name: restart_ntp_service
service:
name: ntpd
state: restarted
The newline \\n
works well with version 2.3 and 2.4 qs well, just to be sure not to use \\\\n
<- thi sis from the actual git commit. 换行符
\\n
与版本2.3和2.4 qs很好地兼容,只是要确保不要从实际的git commit中使用\\\\n
<-thi sis。
# Replace the newline character with an actual newline. Don't replace # escaped \\\\n, hence sub and not str.replace. line = re.sub(r'\\n', os.linesep, params['line']) # Replace the newline character with an actual newline, but be careful # not to trigger other escape sequences (specifically octal \\ooo) line = re.sub(r'((?<!(?:[^\\\\]\\\\))\\\\n)', os.linesep, params['line'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.