ansibleansible-inventory

Check and change a file


I am working with Ansible and I need to check if file /etc/apt/sources.list has the deb-src repositories in it or not. If not it should add them or uncomment them (depending on if they are commented or not). If they are present, just proceed to other tasks. Not sure if this would be the best approach for this use case, though, but feel free to make any suggestions.

To check if deb-src are in the file, I made this task:

---
- name: Find
  ansible.builtin.lineinfile:
    path: /etc/apt/sources.list
    line: "deb-src"
  check_mode: yes
  register: result

cat /etc/apt/sources.list

#  # Note, this file is written by cloud-init on first boot of an instance
#  # modifications made here will not survive a re-bundle.
#  # if you wish to make changes you can:
#  # a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg
#  # or do the same in user-data
#  # b.) add sources in /etc/apt/sources.list.d
#  # c.) make changes to template file /etc/cloud/templates/sources.list.tmpl

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://azure.archive.ubuntu.com/ubuntu/ focal main restricted
# deb-src http://azure.archive.ubuntu.com/ubuntu/ focal main restricted

#  # Major bug fix updates produced after the final release of the
#  # distribution.
deb http://azure.archive.ubuntu.com/ubuntu/ focal-updates main restricted
# deb-src http://azure.archive.ubuntu.com/ubuntu/ focal-updates main restricted

#  # N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
#  # team. Also, please note that software in universe WILL NOT receive any
#  # review or updates from the Ubuntu security team.
deb http://azure.archive.ubuntu.com/ubuntu/ focal universe
# deb-src http://azure.archive.ubuntu.com/ubuntu/ focal universe
deb http://azure.archive.ubuntu.com/ubuntu/ focal-updates universe
# deb-src http://azure.archive.ubuntu.com/ubuntu/ focal-updates universe

#  # N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
#  # team, and may not be under a free licence. Please satisfy yourself as to
#  # your rights to use the software. Also, please note that software in
#  # multiverse WILL NOT receive any review or updates from the Ubuntu
#  # security team.
deb http://azure.archive.ubuntu.com/ubuntu/ focal multiverse
# deb-src http://azure.archive.ubuntu.com/ubuntu/ focal multiverse
deb http://azure.archive.ubuntu.com/ubuntu/ focal-updates multiverse
# deb-src http://azure.archive.ubuntu.com/ubuntu/ focal-updates multiverse

#  # N.B. software from this repository may not have been tested as
#  # extensively as that contained in the main release, although it includes
#  # newer versions of some applications which may provide useful features.
#  # Also, please note that software in backports WILL NOT receive any review
#  # or updates from the Ubuntu security team.
deb http://azure.archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse
# deb-src http://azure.archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse

#  # Uncomment the following two lines to add software from Canonical's
#  # 'partner' repository.
#  # This software is not part of Ubuntu, but is offered by Canonical and the
#  # respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu focal partner
# deb-src http://archive.canonical.com/ubuntu focal partner

deb http://azure.archive.ubuntu.com/ubuntu/ focal-security main restricted
# deb-src http://azure.archive.ubuntu.com/ubuntu/ focal-security main restricted
deb http://azure.archive.ubuntu.com/ubuntu/ focal-security universe
# deb-src http://azure.archive.ubuntu.com/ubuntu/ focal-security universe
deb http://azure.archive.ubuntu.com/ubuntu/ focal-security multiverse
# deb-src http://azure.archive.ubuntu.com/ubuntu/ focal-security multiverse

Regarding this file (/etc/apt/sources.list), the expected result should be:

  1. I want to capture every line where deb-src is commented and uncomment it.
  2. If the deb-src is already uncommented in every occurrence, just proceed to next steps and don't touch the file.

Solution

  • You can achieve your requirement with the replace module using a simple regexp and capture replacement as demonstrated below:

    - name: Make sure all deb-src lines are uncommented
      ansible.builtin.replace:
        path: /etc/apt/sources.list
        regexp: '^# (deb-src .*)$'
        replace: '\1'