# blockinfile and lineinfile behaviour

**URL:** <https://forum.ansible.com/t/blockinfile-and-lineinfile-behaviour/34755>\
**Category:** Ansible Project\
**Created:** [March 9, 2021, 9:44pm UTC](https://forum.ansible.com/t/blockinfile-and-lineinfile-behaviour/34755 "2021-03-09T21:44:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stu\_Lipshires](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@Stu\_Lipshires](https://forum.ansible.com/u/Stu_Lipshires)\
**Post date:** [March 9, 2021, 9:44pm UTC](https://forum.ansible.com/t/blockinfile-and-lineinfile-behaviour/34755/1 "2021-03-09T21:44:35Z")

</div>

I have a Host VM that I am using to launch the creation of multiple OpenShift Clusters. Every time I create a new cluster, I need to update the /etc/hosts and dnsmasq.conf files of this Host VM with the new cluster endpoints.  
I am using the following code block:

- name: update hosts  
blockinfile:  
path: /etc/hosts  
block: |  
${var.network\_lb\_ip\_address} api.${var.cluster\_id}.${var.base\_domain}  
${var.network\_lb\_ip\_address} api-int.${var.cluster\_id}.${var.base\_domain}  
state: present
- name: update dnsmasq  
lineinfile:  
path: /etc/dnsmasq.conf  
line: address=/.apps.${var.cluster\_id}.${var.base\_domain}/${var.network\_lb\_ip\_address}  
state: present

What it is doing is instead of adding a new block when I create a new cluster with new ip’s and ranges, it simply replaces the existing block with the new info so I only ever have 1 cluster’s info. When the file started out there was no managed block. The first time I ran the script, it added the block . What I currently get after the 2nd run is

# BEGIN ANSIBLE MANAGED BLOCK

172.16.0.20 [api.ocp2.cdastu.com](http://api.ocp2.cdastu.com)  
172.16.0.20 [api-int.ocp2.cdastu.com](http://api-int.ocp2.cdastu.com)

# END ANSIBLE MANAGED BLOCK

What I would like after the 2nd run is :

# BEGIN ANSIBLE MANAGED BLOCK

172.16.0.19 [api.ocp1.cdastu.com](http://api.ocp1.cdastu.com)  
172.16.0.19 [api-int.ocp1.cdastu.com](http://api-int.ocp1.cdastu.com)

172.16.0.20 [api.ocp2.cdastu.com](http://api.ocp2.cdastu.com)  
172.16.0.20 [api-int.ocp2.cdastu.com](http://api-int.ocp2.cdastu.com)

# END ANSIBLE MANAGED BLOCK

I also had the ever so slight idea that Ansible could delete the entries that it created when I delete the cluster. Thanks!

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [March 10, 2021, 9:42am UTC](https://forum.ansible.com/t/blockinfile-and-lineinfile-behaviour/34755/2 "2021-03-10T09:42:07Z")

</div>

> I have a Host VM that I am using to launch the creation of multiple OpenShift Clusters. Every time I create a new  
> cluster, I need to update the /etc/hosts and dnsmasq.conf files of this Host VM with the new cluster endpoints.  
> I am using the following code block:  
> - name: update hosts  
> blockinfile:  
> path: /etc/hosts  
> block: |  
> ${var.network\_lb\_ip\_address} api.${var.cluster\_id}.${var.base\_domain}  
> ${var.network\_lb\_ip\_address} api-int.${var.cluster\_id}.${var.base\_domain}  
> state: present  
> - name: update dnsmasq  
> lineinfile:  
> path: /etc/dnsmasq.conf  
> line: address=/.apps.${var.cluster\_id}.${var.base\_domain}/${var.network\_lb\_ip\_address}  
> state: present
> 
> What it is doing is instead of adding a new block when I create a new cluster with new ip's and ranges, it simply  
> replaces the existing block with the new info so I only ever have 1 cluster's info. When the file started out there was  
> no managed block. The first time I ran the script, it added the block . What I currently get after the 2nd run is
> 
> # BEGIN ANSIBLE MANAGED BLOCK  
> 172.16.0.20 api.ocp2.cdastu.com  
> 172.16.0.20 api-int.ocp2.cdastu.com  
> # END ANSIBLE MANAGED BLOCK
> 
> What I would like after the 2nd run is :  
> # BEGIN ANSIBLE MANAGED BLOCK  
> 172.16.0.19 api.ocp1.cdastu.com  
> 172.16.0.19 api-int.ocp1.cdastu.com  
> 172.16.0.20 api.ocp2.cdastu.com  
> 172.16.0.20 api-int.ocp2.cdastu.com  
> # END ANSIBLE MANAGED BLOCK
> 
> I also had the ever so slight idea that Ansible could delete the entries that it created when I delete the cluster. Thanks!

Hello,

do make this work in a proper fashion, you need to have \*all\* clusters in your inventory and not the current one on its  
own.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Stu\_Lipshires](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@Stu\_Lipshires](https://forum.ansible.com/u/Stu_Lipshires)\
**Post date:** [March 11, 2021, 4:01pm UTC](https://forum.ansible.com/t/blockinfile-and-lineinfile-behaviour/34755/3 "2021-03-11T16:01:27Z")

</div>

Sorry, a little new to this. So I don’t know all the cluster names up front. I might create 1 cluster today and then tomorrow somebody will ask for a new cluster. My inventory file currently has the ip address of the VM Host that the ansible is running on. Is there something different that I should be doing? Do I have to read the existing host file and then add the new values? Can you point to a snippet that would do that? Thanks!
