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
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'])