Skip to content

Instantly share code, notes, and snippets.

@BladeWDR
Created December 7, 2024 01:53
Show Gist options
  • Save BladeWDR/355fe911e00dab959846db21a9084769 to your computer and use it in GitHub Desktop.
Save BladeWDR/355fe911e00dab959846db21a9084769 to your computer and use it in GitHub Desktop.
An Ansible version of Apalrd's rewrite.sh :)
---
- name: Edit the sources files on Debian and Ubuntu hosts to use our local caching webserver.
hosts: all
vars:
internal_cache_server_name: http://deb.test.xyz
# Original repo name we need to match on the left, the path in your nginx webserver on the right.
original_repo_targets:
http://archive.ubuntu.com/ubuntu: ubuntu
http://us.archive.ubuntu.com/ubuntu: ubuntu
http://deb.debian.org/debian: debian
http://ftp.us.debian.org/debian: debian
http://security.debian.org: debsec
http://deb.debian.org/debian-security: debsec
http://security.ubuntu.com/ubuntu: ubusec
http://http.kali.org/kali: kali
http://download.proxmox.com/debian: proxmox
https://dl.cloudsmith.io/public/caddy/stable/deb/debian: caddy
https://deb.nodesource.com/node_20.x: node
tasks:
- name: Edit the sources list
become: true
when: ansible_facts['distribution'] in ['Ubuntu', 'Debian']
block:
- name: Get list of existing files in /etc/apt/sources.list.d folder.
ansible.builtin.find:
paths: "/etc/apt/sources.list.d"
file_type: file
register: found_files
- name: Replace the repository URLs for the files in sources.list.d
ansible.builtin.replace:
path: "{{ item.0.path }}"
backup: true
regexp: "{{ item.1[0] | regex_escape() }}"
replace: "{{ internal_cache_server_name }}/{{ item.1[1] }}"
loop: "{{ found_files.files | product(original_repo_targets.items() | list) | list }}"
- name: Replace the repository URLs in sources.list
ansible.builtin.replace:
path: /etc/apt/sources.list
backup: true
regexp: "{{ item[0] | regex_escape() }}"
replace: "{{ internal_cache_server_name }}/{{ item[1] }}"
loop: "{{ original_repo_targets.items() }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment