# How to add a string in end of line Ansible

**URL:** <https://forum.ansible.com/t/how-to-add-a-string-in-end-of-line-ansible/3525>\
**Category:** Get Help\
**Created:** [January 29, 2024, 11:36pm UTC](https://forum.ansible.com/t/how-to-add-a-string-in-end-of-line-ansible/3525 "2024-01-29T23:36:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dedeboy](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dedeboy/32/2062_2.png) [@dedeboy](https://forum.ansible.com/u/dedeboy)\
**Post date:** [January 29, 2024, 11:36pm UTC](https://forum.ansible.com/t/how-to-add-a-string-in-end-of-line-ansible/3525/1 "2024-01-29T23:36:22Z")

</div>

Hello friends!

I would like a help you about a problem when I attempt to run a playbook in Ansible.

I’ve created a task in my playbook to add IP\_address inside a file called hosts. But I’m facing a problem. I need to put the ip\_address on the end of string K8\_MASTER\_NODE\_IP= But searching the documentation Ansible I didn’t find it, but the only way found was until now is to put below the string, but in this way it doesn’t work for me. Someone, could me help me about this issue?

This is my script I have created.

```auto
- name: Adicionando o IP privado da instancia criado ao arquivo /installk8_s/hosts
      ansible.builtin.lineinfile:
        path: "/home/lpi1/Ansible/descomplicando_ansible/projeto_k8s_cluster/install_k8s/hosts"
        regexp: "{{ item.private_ip_address }}"
        firstmatch: true      
        insertafter: "[K8_MASTER_NODE_IP=]"
        line: "{{ item.private_ip_address }}"                                                
      with_items: "{{ ec2.instances }}"

```

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [January 29, 2024, 11:44pm UTC](https://forum.ansible.com/t/how-to-add-a-string-in-end-of-line-ansible/3525/2 "2024-01-29T23:44:13Z")

</div>

Is this what you are looking for? “Append to existing line” section ==\> [Bootstrap](https://www.freekb.net/Article?id=29)

```auto
Append to an existing line

Let's say foo.txt contains "Hello World". Here is how you would append "example" after Hello World.

- lineinfile:
    path: /tmp/foo.txt
    backrefs: true
    regexp: '^(old line)$'
    line: '\1 example'

```

---

<div class="post-metadata">

**Author:** ![dedeboy](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dedeboy/32/2062_2.png) [@dedeboy](https://forum.ansible.com/u/dedeboy)\
**Post date:** [January 30, 2024, 11:10pm UTC](https://forum.ansible.com/t/how-to-add-a-string-in-end-of-line-ansible/3525/3 "2024-01-30T23:10:42Z")

</div>

Hello jrglynn2,

Sorry for delay.

It worked!

```auto
ansible.builtin.lineinfile:
    path: "/home/lpi1/Ansible/descomplicando_ansible/projeto_k8s_cluster/install_k8s/hosts"
    backrefs: true
    regexp: '^(K8S_MASTER_NODE_IP=)$'
    line: '\1 {{item.private_ip_address}}'
  with_items: "{{ ec2.instances }}"

```

Thank you very much 🙂

---

<div class="post-metadata">

**Author:** ![ptn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ptn/32/481_2.png) [@ptn](https://forum.ansible.com/u/ptn)\
**Post date:** [January 31, 2024, 6:39pm UTC](https://forum.ansible.com/t/how-to-add-a-string-in-end-of-line-ansible/3525/4 "2024-01-31T18:39:13Z")

</div>

Hey,

Glad your issue is resolved, though I can’t help to notice you flagged your post as solution. It seems to me [@jrglynn2 previous post](http://forum.ansible.com/t/how-to-add-a-string-in-end-of-line-ansible/3525/2) should earn this consideration; wouldn’t you agree ?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [March 1, 2024, 6:39pm UTC](https://forum.ansible.com/t/how-to-add-a-string-in-end-of-line-ansible/3525/5 "2024-03-01T18:39:39Z")

</div>

Closed on March 1, 2024, 6:39pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
