Update multiple lines using lineinfile

54 views
Skip to first unread message

vmadh...@gmail.com

unread,
Jan 1, 2017, 9:59:04 PM1/1/17
to Ansible Project
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
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
gpgcheck=1


#released updates
[updates]
name=CentOS-$releasever - Updates
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
gpgcheck=1


#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
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
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
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.

Kai Stian Olstad

unread,
Jan 2, 2017, 9:33:35 AM1/2/17
to ansible...@googlegroups.com
On 30. des. 2016 19:36, vmadh...@gmail.com wrote:
> 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.

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

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages