diff --git a/playbooks/update_repositories.yml b/playbooks/update_repositories.yml new file mode 100644 index 0000000..0d5b529 --- /dev/null +++ b/playbooks/update_repositories.yml @@ -0,0 +1,72 @@ +--- +- name: Automatically set up-to-date APT repositories (Debian/Raspberry Pi) + hosts: all + become: yes + gather_facts: yes + + vars: + distro_codename: "{{ ansible_distribution_release }}" + + # Содержимое для /etc/apt/sources.list (CDN Debian) + sources_list_content: | + deb https://deb.debian.org/debian {{ distro_codename }} main contrib non-free non-free-firmware + deb https://security.debian.org/debian-security {{ distro_codename }}-security main contrib non-free non-free-firmware + deb https://deb.debian.org/debian {{ distro_codename }}-updates main contrib non-free non-free-firmware + + # Содержимое для raspi.list (только если это Raspberry Pi) + raspi_list_content: | + deb http://archive.raspberrypi.com/debian/ {{ distro_codename }} main + + tasks: + # 1. Удаляем все старые файлы в /etc/apt/sources.list.d/, + # чтобы исключить конфликты (включая .sources и .list) + - name: Backup current sources.list.d directory + copy: + src: /etc/apt/sources.list.d + dest: "/etc/apt/sources.list.d.backup_{{ ansible_date_time.epoch }}" + remote_src: yes + ignore_errors: yes + + - name: Remove all files in /etc/apt/sources.list.d/ + file: + path: "{{ item }}" + state: absent + loop: + - /etc/apt/sources.list.d/debian.sources + - /etc/apt/sources.list.d/raspi.sources + - /etc/apt/sources.list.d/raspi.list + - /etc/apt/sources.list.d/*.list + - /etc/apt/sources.list.d/*.sources + loop_control: + loop_var: item + ignore_errors: yes + + # 2. Создаём основной sources.list + - name: Write /etc/apt/sources.list + copy: + content: "{{ sources_list_content }}" + dest: /etc/apt/sources.list + owner: root + group: root + mode: '0644' + + # 3. Если это Raspberry Pi – создаём raspi.list + - name: Check if system is Raspberry Pi + stat: + path: /etc/rpi-issue + register: rpi_check + + - name: Write /etc/apt/sources.list.d/raspi.list (only on Raspberry Pi) + copy: + content: "{{ raspi_list_content }}" + dest: /etc/apt/sources.list.d/raspi.list + owner: root + group: root + mode: '0644' + when: rpi_check.stat.exists + + # 4. Обновляем кэш APT + - name: Update APT cache + apt: + update_cache: yes + cache_valid_time: 3600 \ No newline at end of file