0

I have following text in a file where there is space in front of text 'us'

proxies:
  us: ["http://aa.bb.cc.dd","http://ee.ff.gg.hh","http://ii.jj.kk.ll"]

I want to insert a new IP at the end of the line and the output should be the following

proxies:
  us: ["http://aa.bb.cc.dd","http://ee.ff.gg.hh","http://ii.jj.kk.ll,"http://{{ new_server_ip }}]

I am unable to get the desired output. Please help me with the code

lineinfile:
  path: test.sh
  regexp: "^\s*(us:)"
  line: 'http://{{ new_server_ip }}'
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
user11549576
  • 119
  • 1
  • 14
  • Sorry for the unformatted text. – user11549576 Jul 11 '19 at 18:47
  • Hi and welcome to SO. There is a formatting guide at right of screen when you create or edit your question. I suggest your read it. You can also see how other questions and answers are formated: you just have to click on the edit link to see the raw text. – Zeitounator Jul 11 '19 at 19:27
  • See [an other response on a similar request](https://stackoverflow.com/a/56599426/9401096). I suggest you don't use `lineinfile` for this need (you'll end up with an ugly unintelligible regex). The [`template` module](https://docs.ansible.com/ansible/latest/modules/template_module.html) is probably your best friend here. – Zeitounator Jul 11 '19 at 20:00
  • Are you running this playbook in local Ansible machine? Also will the file with proxies array contain any other content? – Prakash Krishna Jul 12 '19 at 01:27
  • @Zeitounator: Following is my final code, ansible playbook runs without any error, but the file is not updated `- name: updating tor_proxies.yml lineinfile: path: test.yml state: present regexp: '^( *us: *\[)(("(?!http://{{ new_server_ip }})",? *)*)(\])' backrefs: yes line: '\1\2, "http://{{ new_server_ip }}:8118"\4'` – user11549576 Jul 12 '19 at 05:36

0 Answers0