# Update multiple lines using lineinfile

**URL:** <https://forum.ansible.com/t/update-multiple-lines-using-lineinfile/23350>\
**Category:** Ansible Project\
**Created:** [December 30, 2016, 6:36pm UTC](https://forum.ansible.com/t/update-multiple-lines-using-lineinfile/23350 "2016-12-30T18:36:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vmadhura21](https://avatars.discourse-cdn.com/v4/letter/v/e0b2c6/32.png) [@vmadhura21](https://forum.ansible.com/u/vmadhura21)\
**Post date:** [December 30, 2016, 6:36pm UTC](https://forum.ansible.com/t/update-multiple-lines-using-lineinfile/23350/1 "2016-12-30T18:36:15Z")

</div>

Hi,  
I am new to Ansible and I am finding it hard to update multiple lines in 2 different stanza in a file. Below is the example:

I am trying to update **gpgcheckvalue from 1 to 0 —\>** **gpgcheck=1 to 0 in 2 stanzas below (updates and extras)**

[base]  
name=CentOS-$releasever - Base  
mirrorlist=[http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra](http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra)  
#baseurl=[http://mirror.centos.org/centos/$releasever/os/$basearch/](http://mirror.centos.org/centos/$releasever/os/$basearch/)  
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7  
gpgcheck=1

#released updates  
**_[updates]_**  
name=CentOS-$releasever - Updates  
mirrorlist=[http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra](http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra)  
#baseurl=[http://mirror.centos.org/centos/$releasever/updates/$basearch/](http://mirror.centos.org/centos/$releasever/updates/$basearch/)  
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7  
**gpgcheck=1**

#additional packages that may be useful  
**_[extras]_**  
name=CentOS-$releasever - Extras  
mirrorlist=[http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra](http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra)  
#baseurl=[http://mirror.centos.org/centos/$releasever/extras/$basearch/](http://mirror.centos.org/centos/$releasever/extras/$basearch/)  
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7  
**gpgcheck=1**

#additional packages that extend functionality of existing packages  
[centosplus]  
name=CentOS-$releasever - Plus  
mirrorlist=[http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra](http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra)  
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7  
#baseurl=[http://mirror.centos.org/centos/$releasever/centosplus/$basearch/](http://mirror.centos.org/centos/$releasever/centosplus/$basearch/)  
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7  
gpgcheck=1  
enabled=0

**Below is my code in my yml.**

- name: Splunk Repo Update test  
become: true

lineinfile:  
dest: ‘/etc/yum.repos.d/CentOS-Base.repo’  
state: present  
insertafter: ‘[updates]’  
regexp: ‘gpgcheck’  
line: ‘gpgcheck=1’

- name: Splunk Repo Update test for Extra stanza  
become: true

lineinfile:  
dest: ‘/etc/yum.repos.d/CentOS-Base.repo’  
state: present  
insertafter: ‘[extras]’  
regexp: ‘gpgcheck’  
line: ‘gpgcheck=1’

I am sure this is not the right way to code and it could be combined into 1 lineinfile option using with\_items. But, for now, I want to try this separately and see if I could. If with\_items is the right way, please let me know and I can change it. But, here is the situation though. When I run this code, it is updating the stanza which is at the end (updating gpgcheck in [centosplus] stanza). I understand it is trying to find the last instance of “gpgcheck” and updating the value. But, I want only these 2 stanzas to be updated and nothing before that or after that. How can I achieve that?? Hope my question makes sense.

Any help is greatly appreciated.

Thanks.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 2, 2017, 2:32pm UTC](https://forum.ansible.com/t/update-multiple-lines-using-lineinfile/23350/2 "2017-01-02T14:32:39Z")

</div>

Make lineinfile in this situation is not easy, since this file is a ini-file use the ini\_file module instead  
[https://docs.ansible.com/ansible/ini\_file\_module.html](https://docs.ansible.com/ansible/ini_file_module.html)
