G
G
gremlintv22019-01-22 15:18:48
Ansible
gremlintv2, 2019-01-22 15:18:48

ANSIBLE How to add IP to existing entries in a string using a regular expression (explained under the cut)?

Hello,
there is a file: /opt/firewall-rules.sh
It contains a line like this:
TESTING="111.111.111.111/32,111.111.111.112/32"
Tell me how to add a third ip entry to the two existing ones using the lineinfile module - 111.111.111.113/32, so that as a result the line looks like this

TESTING="111.111.111.111/32,111.111.111.112/32,111.111.111.113/32"

Thanks
There is a solution here, but it does not add an IP, but simply replaces the string with a regular expression. And this does not solve the problem. Since the initial condition is that we do not know which ip records we already have in the line.
Found the answer:
- name: Firewall rule
replace:
path: /root/fw/firewall
regexp: '(^TEST_SERVER=.*)"$' replace: ' \ 1,111.111.111.113
/32"'
backup: yes ) Either I was looking badly for an answer in the Russian-speaking space; b) Or off. the ansible dock really explains the replace module so intelligibly; c) Either no one is using iptables scripts, but using a playbook instead;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Puma Thailand, 2019-01-22
@opium

The ansible documentation has an example of how to insert a line after a certain one inline, what is the question

L
Logout_90, 2019-04-25
@Logout_90

You are trying to delegate tasks to a tool that is not designed for this.
Ansible can bring the host to some state from some state. If the state of the host at the time the role/playbook is executed is unknown, then the result will be unknown

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question