# lineinfile using insertafter

**URL:** <https://forum.ansible.com/t/lineinfile-using-insertafter/33426>\
**Category:** Ansible Project\
**Tags:** rhel\
**Created:** [July 17, 2020, 8:21pm UTC](https://forum.ansible.com/t/lineinfile-using-insertafter/33426 "2020-07-17T20:21:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Bidwell](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@Chris\_Bidwell](https://forum.ansible.com/u/Chris_Bidwell)\
**Post date:** [July 17, 2020, 8:21pm UTC](https://forum.ansible.com/t/lineinfile-using-insertafter/33426/1 "2020-07-17T20:21:40Z")

</div>

Hey all,

Trying to insert a line to multiple instances in the same file.

I tried insertafter but that only put in one spot. Here’s the code:

- name: add proxy to zabbix.repo if needed  
lineinfile:  
path: /etc/yum.repos.d/zabbix.repo  
state: present  
insertafter: ‘[zabbix\*’  
line: ‘proxy=[http://proxy:8080](http://proxy:8080)’  
when:
- ansible\_default\_ipv4.address is match(‘192.168.226.’) or  
ansible\_default\_ipv4.address is match(‘10.172.226.’)

Here’s the result:

[zabbix]  
name=Zabbix Official Repository - $basearch  
baseurl=[https://repo.zabbix.com/zabbix/4.4/rhel/8/$basearch/](https://repo.zabbix.com/zabbix/4.4/rhel/8/$basearch/)  
enabled=1  
gpgcheck=1  
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591

[zabbix-non-supported]  
**proxy=[http://proxy:8080](http://proxy:8080)**  
name=Zabbix Official Repository non-supported - $basearch  
baseurl=[https://repo.zabbix.com/non-supported/rhel/8/$basearch/](https://repo.zabbix.com/non-supported/rhel/8/$basearch/)  
enabled=1  
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX  
gpgcheck=1

I would have thought my insertafter regexp would match both of those [zabbix] and [zabbix-non-supported] but it doesn’t.

---

<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:** [July 18, 2020, 3:38am UTC](https://forum.ansible.com/t/lineinfile-using-insertafter/33426/2 "2020-07-18T03:38:11Z")

</div>

> Hey all,
> 
> Trying to insert a line to multiple instances in the same file.
> 
> I tried insertafter but that only put in one spot. Here's the code:
> 
> ```
> \- name: add proxy to zabbix\.repo if needed
> lineinfile:
> path: /etc/yum\.repos\.d/zabbix\.repo
> state: present
> insertafter: &#39;\\\[zabbix\*&#39;
> line: &#39;proxy=http://proxy:8080&#39;
> when:
> \- ansible\_default\_ipv4\.address is match\(&#39;192\\\.168\\\.226\\\.&#39;\) or
> ansible\_default\_ipv4\.address is match\(&#39;10\\\.172\\\.226\\\.&#39;\)
> 
> ```
> 
> Here's the result:
> 
> [zabbix]  
> name=Zabbix Official Repository - $basearch  
> baseurl=[https://repo.zabbix.com/zabbix/4.4/rhel/8/$basearch/](https://repo.zabbix.com/zabbix/4.4/rhel/8/$basearch/)  
> enabled=1  
> gpgcheck=1  
> gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
> 
> [zabbix-non-supported]  
> \*proxy=[http://proxy:8080](http://proxy:8080) \*
> 
> name=Zabbix Official Repository non-supported - $basearch  
> baseurl=[https://repo.zabbix.com/non-supported/rhel/8/$basearch/](https://repo.zabbix.com/non-supported/rhel/8/$basearch/)  
> enabled=1  
> gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX  
> gpgcheck=1
> 
> I would have thought my insertafter regexp would match both of those [zabbix] and [zabbix-non-supported] but it doesn't.

No, lineinfile replaces one line. But why don't you set the proxy variable globally for yum / your system?

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

---

<div class="post-metadata">

**Author:** ![S\_C\_Rigler](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@S\_C\_Rigler](https://forum.ansible.com/u/S_C_Rigler)\
**Post date:** [July 18, 2020, 7:00am UTC](https://forum.ansible.com/t/lineinfile-using-insertafter/33426/3 "2020-07-18T07:00:20Z")

</div>

It would be a lot easier to accomplish this with the ini\_file module instead of lineinfile. For example:

- ini\_file:  
path: /etc/yum.repos.d/zabbix.repo  
section: zabbix-non-supported  
option: proxy  
value: [http://proxy:8080](http://proxy:8080)

---

<div class="post-metadata">

**Author:** ![Chris\_Bidwell](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@Chris\_Bidwell](https://forum.ansible.com/u/Chris_Bidwell)\
**Post date:** [July 18, 2020, 5:48pm UTC](https://forum.ansible.com/t/lineinfile-using-insertafter/33426/4 "2020-07-18T17:48:29Z")

</div>

Because I’ve got checks that applies the proxy based of which subnet the playbook runs on.
