'how to selectively upgrade a line with variable for multiple cars based on preceeding line using lineinfile with ansible
My objective to selectively modify settings for cards such as eth0, eth1, wlan0, wlan1 I am trying to modify the IP in the line: static ip_address=192.168.1.201/24 to a new ip for each card and use the IP from the host file
I am not sure if **"lineinfile"**is the right tool.
If used with firstmatch: true I can match to the first card.
But it is definitely not matching as I would like it to. It seems not to care if I define it replace for wlan0 or wlan1. Sometimes it even matches the second one. Depending on if I use regex. Or just simply doesn't modify the line but add the line. Yet again in the way I would think.
It feel like the insertafter: '^interface wlan0' or wlan1 is even considered
I did try and read blogs and lineinfile documentation I read about backrefences and besides I don't quite understand how it works I don't think it the way to go anyways from discussion and examples.
Is it even possible to this selective modification based the preceding line? and if so, which module or what is the trick?
I am using ansbile 2.9
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/pedro/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.8.10 (default, Nov 26 2021, 20:14:08) [GCC 9.3.0]
source file:
interface wlan0
static ip_address=192.168.1.200/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1
interface wlan1
static ip_address=192.168.1.201/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1
playbook:
---
#tasks:
- name: set ips to cards
hosts: radzim
tasks:
- name: test task
lineinfile:
path: /home/pedro/ansible_test/pitest.conf
regexp: '^static ip_address=[0-9 ]*\.[0-9 ]*\.[0-9 ]*\.[0-9 ]*\/[0-9 ]*'
line: "static ip_address={{ ip_address }}/24"
insertafter: '^interface wlan0'
#firstmatch: true
state: present
backup: true
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
