On 08.11.2017 20:47, Marc Haber wrote:
When I change a system from testing to stable, I want the all
testing*.list to vanish, and the stable*.list files to appear, unless
they're already there, in which case I want them untouched. Locally
placed files should also be untouched.

How you can implement this depends on the feature you need
1. one server having a several repo stable, testing and unstable
2. one server only having stable, testing or unstable.


My first approach was to concentrate all the ansible-managed *.list
files under a common prefix, zda, giving, for example,
zda-unstable.list.

I then wrote code to first remove all zda*.list files (and their laegacy-named
instances), and code to deploy the correct zda-foo.list file:

- name: search for sources.list files
  find:
    paths: "/etc/apt/sources.list.d"
    patterns:
"zda-*.list,exp-mc.list,sid-mc.list,sid-zg-stable-mc.list,sid-zg-unstable-mc.list,stretch-mc.list,stretch-security.list,stretch-zg-stable-mc.list,stretch-zg-unstable-mc.list,buster-mc.list,stretch-zg-stable-mc.list,stretch-security.list"
  register: sourceslistfiles
- name: delete sources.list files
  file:
    path: "{{ item.path }}"
    state: absent
  with_items: "{{ sourceslistfiles.files }}"

To clean up legacy I would only run this once and after that make Ansible do the work.


and finally code to roll out new list files:

- name: include repositories
  tags:
          repos
  include_tasks:
          "{{distribution}}/{{distribution_version}}/repos.yml"


$ cat roles/common/tasks/debian/stretch/repos.yml
---
- name: zda-stretch-mc.list
  tags:
  - repos
  - stretch
  copy:
    dest: /etc/apt/sources.list.d/zda-stretch-mc.list
    owner: root
    group: root
    mode: 0644
    content: |
deb http://debian.debian.zugschlus.de/debian/ stretch main contrib
  notify: apt update
- name: zda-stretch-security.list
  tags:
  - repos
  - stretch
  copy:
    dest: /etc/apt/sources.list.d/zda-stretch-security.list
    owner: root
    group: root
    mode: 0644
    content: |
            deb
http://debian-security.debian.zugschlus.de/debian-security/
stretch/updates main contrib
deb http://security.debian.org/ stretch/updates main contrib
  notify: apt update

Feature 1 only need one variable, call it "repo_type", this could contain stable, testing or unstable.

Then your tasks would look something like this

- apt_repository:
repo: deb http://debian.debian.zugschlus.de/debian/ stretch main contrib
    filename: zda-stretch-mc
state: "{{ (repo_type == 'stable') | ternary('present', 'absent') }}"

State present creates the file, and absent remove the file so Ansible will clean up the files so nothing is left behind when you change repo_type.
You would need one for each repo and repo_type.
But with with_items and facts you could probably make a loop to deal with most of them.


With feature 2 you could have something like stable_state, testing_state and unstable_state and set them to absent or present, and then just use that variable in state.

Hopefully this make some sense.

--
Kai Stian Olstad

--
You received this message because you are subscribed to the Google Groups "Ansible 
Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/47d3096c21c05b6fcb5c0f5000a20065%40olstad.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to