I have a ansible play which works correctly as follows, here i have two From
entries which are being changed with TO
entries.
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.
---
- 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
. 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.
Here is the blockinfile
documentation .
Alternatively you could use two lineinfile
tasks and leverage the insertafter
property. 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. 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.
# 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'])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.