Kein
Kein

Reputation: 165

ANSIBLE How to add IP to existing entries in the string by a regex?

There is lineinfile /opt/firewall-rules.sh:

TESTING="111.111.111.111/32,111.111.111.112/32"

How through the lineinfile module to add to the two existing third ip entry - 111.111.111.113/32, so that as a result the lines look like this:

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

There is the particular solution at other post but it doesn't add one IP it replace whole line.

- name: Firewall rule
  replace:
    path: /opt/firewall-rulles.sh
    regexp: '"$'
    replace: ',111.111.111.113/32"'
    after: 'TEST=*'
    before: 'DEVELOPE*'
    backup: yes

I'm trying this but it replace all after DEVELOPE

Solution as I see(Best regards for all answers and advice):

- name: Firewall rule for DB_TESTING
   replace:
      path: /opt/firewall-rules.sh
      regexp: '(^TESTING=.*)"$'
      replace: '\1,{{ ansible_default_ipv4.address }}/32"'
      backup: yes
   delegate_to: DB_TESTING

It's adding IP line from host variable at delegated servers firewall config

Best regards

Upvotes: 1

Views: 165

Answers (1)

Debugger
Debugger

Reputation: 532

try with this regexp='^(TESTING(?!.\b,111.111.111.113/32\b).)$'

Upvotes: 1

Related Questions