diff --git a/tasks/main.yml b/tasks/main.yml index 9707fa8..56daaa7 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -4,16 +4,9 @@ ansible.builtin.include_vars: file: "{{ ansible_distribution|lower }}.yml" -- name: "Import main repositories" +- name: "Import main {{ ansible_distribution|lower }} repositories" + ansible.builtin.include_tasks: "{{ ansible_distribution|lower }}.yml" when: manage_repositories_enable_default_repo - block: - - name: "Import ubuntu.yml" - ansible.builtin.include_tasks: ubuntu.yml - when: ansible_distribution == 'Ubuntu' - - - name: "Import debian.yml" - ansible.builtin.include_tasks: debian.yml - when: ansible_distribution == 'Debian' - name: "Import custom_repositories.yml" ansible.builtin.include_tasks: custom_repositories.yml diff --git a/tasks/ubuntu.yml b/tasks/ubuntu.yml index ca2eebf..e722319 100644 --- a/tasks/ubuntu.yml +++ b/tasks/ubuntu.yml @@ -1,10 +1,47 @@ --- # task/ubuntu file for manage_repositories +- name: "Emtpy /etc/apt/sources.list" + block: + - name: Read the current content of the file + ansible.builtin.slurp: + src: "{{ manage_repositories_sources_list_location }}" + register: sources_list_current_content + ignore_errors: true + + - name: "Convert sources.list current content to string" + ansible.builtin.set_fact: + sources_list_current_content_str: "{{ sources_list_current_content.content | b64decode | default('') }}" + + - name: "Define sources.list new content" + ansible.builtin.set_fact: + sources_list_new_content: "{{ manage_repositories_sources_list_message }}" + + - name: "Replace content of /etc/apt/sources.list" + ansible.builtin.replace: + path: "{{ manage_repositories_sources_list_location }}" + regexp: "{{ sources_list_current_content_str | regex_escape }}" + replace: "{{ sources_list_new_content }}" + when: sources_list_current_content_str != sources_list_new_content + +- name: "Import list files" + block: + - name: "Create mirrors files" + ansible.builtin.file: + path: "{{ manage_repositories_mirrors_location }}" + state: directory + + - name: "Populate mirrors files" + ansible.builtin.copy: + content: | + "{{ item.uri }}" + dest: "{{ manage_repositories_mirrors_location }}/{{ item.name }}.list" + loop: "{{ manage_repositories_default_repo }}" + - name: "Configure ubuntu main repositories into sources.list" vars: repositories: "{{ manage_repositories_default_repo }}" ansible.builtin.template: - src: "sources.list.j2" + src: "repo.sources.j2" dest: "{{ manage_repositories_default_repo_location }}" mode: '0644' owner: root diff --git a/templates/repo.sources.j2 b/templates/repo.sources.j2 new file mode 100644 index 0000000..02c795d --- /dev/null +++ b/templates/repo.sources.j2 @@ -0,0 +1,14 @@ +# {{ ansible_managed }} + +{% for repository in repositories %} +# {{ repository.comments}} +types: {{ repository.types | join(' ') }} +uris: mirror+file://{{ manage_repositories_mirrors_location }}/{{ repository.name }}.list +suites: {{ repository.suites | join(' ') }} +components: {{ repository.components | join(' ') }} +{% if (repository.options is defined) and repository.options %} +{% for option in repository.options %} +{{ option }}: {{ repository.options[option] }} +{% endfor %} +{% endif %} +{% endfor %} \ No newline at end of file diff --git a/templates/sources.list.j2 b/templates/sources.list.j2 deleted file mode 100644 index c8ca4dd..0000000 --- a/templates/sources.list.j2 +++ /dev/null @@ -1,6 +0,0 @@ -# {{ ansible_managed }} - -{% for repository in repositories %} -# {{ repository.comments}} -{{ repository.type }} {% if repository.gpg_key is not none %}[signed-by=/usr/share/keyrings/{{ repository.filename }}-archive-keyring.asc] {% endif %}{{ repository.uri }} {{ repository.suites }} {{ repository.components }} -{% endfor %} \ No newline at end of file diff --git a/vars/main.yml b/vars/main.yml index 4b126a0..e802bd9 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,3 +1,6 @@ --- # vars file for manage_repositories -manage_repositories_default_repo_location: /etc/apt/sources.list +manage_repositories_sources_list_location: /etc/apt/sources.list +manage_repositories_default_repo_location: "/etc/apt/sources.list.d/{{ ansible_distribution|lower }}.sources" +manage_repositories_mirrors_location: /etc/apt/mirrors +manage_repositories_sources_list_message: "# See /etc/apt/sources.list.d/{{ ansible_distribution|lower }}.sources\\n" diff --git a/vars/ubuntu.yml b/vars/ubuntu.yml index 10f2c7d..b8009dd 100644 --- a/vars/ubuntu.yml +++ b/vars/ubuntu.yml @@ -1,31 +1,47 @@ --- # vars file for manage_repositories +# manage_repositories_default_repo: +# # ubuntu main repository +# - uri: "{{ manage_repositories_main_repo_uri[ansible_distribution|lower] }}" +# gpg_key: +# comments: "ubuntu main repository" +# type: "deb" +# suites: "{{ ansible_distribution_release }}" +# components: "main restricted universe multiverse" +# # ubuntu updates repository +# - uri: "{{ manage_repositories_main_repo_uri[ansible_distribution|lower] }}" +# gpg_key: +# comments: "ubuntu updates repository" +# type: "deb" +# suites: "{{ ansible_distribution_release }}-updates" +# components: "main restricted universe multiverse" +# # ubuntu security repository +# - uri: "{{ manage_repositories_main_repo_uri[ansible_distribution|lower] }}" +# gpg_key: +# comments: "ubuntu security repository" +# type: "deb" +# suites: "{{ ansible_distribution_release }}-security" +# components: "main restricted universe multiverse" +# # ubuntu backports repository +# - uri: "{{ manage_repositories_main_repo_uri[ansible_distribution|lower] }}" +# gpg_key: +# comments: "ubuntu backports repository" +# type: "deb" +# suites: "{{ ansible_distribution_release }}-backports" +# components: "main restricted universe multiverse" manage_repositories_default_repo: - # ubuntu main repository - - uri: "{{ manage_repositories_main_repo_uri[ansible_distribution|lower] }}" - gpg_key: + - name: ubuntu + uri: "{{ manage_repositories_main_repo_uri[ansible_distribution|lower] }}" comments: "ubuntu main repository" - type: "deb" - suites: "{{ ansible_distribution_release }}" - components: "main restricted universe multiverse" - # ubuntu updates repository - - uri: "{{ manage_repositories_main_repo_uri[ansible_distribution|lower] }}" - gpg_key: - comments: "ubuntu updates repository" - type: "deb" - suites: "{{ ansible_distribution_release }}-updates" - components: "main restricted universe multiverse" - # ubuntu security repository - - uri: "{{ manage_repositories_main_repo_uri[ansible_distribution|lower] }}" - gpg_key: - comments: "ubuntu security repository" - type: "deb" - suites: "{{ ansible_distribution_release }}-security" - components: "main restricted universe multiverse" - # ubuntu backports repository - - uri: "{{ manage_repositories_main_repo_uri[ansible_distribution|lower] }}" - gpg_key: - comments: "ubuntu backports repository" - type: "deb" - suites: "{{ ansible_distribution_release }}-backports" - components: "main restricted universe multiverse" + types: + - deb + suites: + - "{{ ansible_distribution_release }}" + - "{{ ansible_distribution_release }}-security" + - "{{ ansible_distribution_release }}-updates" + - "{{ ansible_distribution_release }}-backports" + components: + - main + - restricted + - universe + - multiverse