Hi,
I’m a newbie with Ansible, and I have a problem.
First, I use version 1.8.2, stock, without anything added.
Then, I want to configure /etc/network/interfaces file with Ansible, so I need to work with regexp to do what I want.
My goal is to add or rewrite some bridges configuration in that file.
I use this :
- name: Configure /etc/network/interfaces
lineinfile:
dest: /etc/network/interfaces
regexp: ‘[\r\n](# DO NOT TOUCH BEGIN[\n\r])?auto[ \t]+{{ item.if }}[ \t][\n\r](?:[ \t].[\n\r])?[ \t]bridge_maxwait[ \t]+0[ \t]([\n\r]# DO NOT TOUCH END[\n\r])?’
line: |
DO NOT TOUCH BEGIN
auto {{ item.if }}
iface {{ item.if }} inet static
address {{ item.ip }}{{ ipconfig.stdout|trim }}
netmask 255.255.0.0
bridge_ports none
bridge_stp off
bridge_fd 0
bridge_maxwait 0
DO NOT TOUCH END
with_items: vlans
register: interface_configured
I want to add the “DO NOT TOUCH” for existing bridges too, and of course create lines for non-existing ones.
You can test regexp here : https://regex101.com/r/sK7iF0/1
We can see that for example, it matches for vmbr10.
Instead of replacing those lines, Ansible lineinfile add the line at the end of file :
DO NOT TOUCH BEGIN
auto vmbr10
iface vmbr10 inet static
address 10.111.0.2
netmask 255.255.0.0
bridge_ports none
bridge_stp off
bridge_fd 0
bridge_maxwait 0
DO NOT TOUCH END
If I change “lineinfile” and “line” with “replace” and “replace”, it works ! It replaces the matched result, but of course it does not add lines when bridge does not exists
I see in documentation that replace module is working with “multiline” regexp, but if I force multiline mode in lineinfile adding “(?m)” at the beginning, it does not work better…
Is someone understanding what is my problem ? And how can I do it better
Thank you a lot.