How to add a string in end of line Ansible

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.

- 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.

Is this what you are looking for? “Append to existing line” section ==> Bootstrap

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'
2 Likes

Hello jrglynn2,

Sorry for delay.

It worked!

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 :slight_smile:

1 Like

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 should earn this consideration; wouldn’t you agree ?

2 Likes

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