Skip to content

Commit 373dafd

Browse files
Stop the apt repository tasks undoing each other every run
"Remove old PowerDNS repository files" deleted /etc/apt/sources.list.d/powerdns.list, and "Add PowerDNS repository" recreated it immediately afterwards through apt_repository's filename: powerdns. So both reported changed on every deploy, forever, and a repeat run of an unchanged playbook never settled to zero. The removal did have a point. apt_repository adds a repository line but will not remove a superseded one, so moving auth_version - 4.9 to 5.0, as happened recently - would leave the old suite sitting in the same file and still enabled. Deleting the file first was the way to guarantee it held only the current line. Declaring the content directly gets that guarantee without the churn: the file is exactly one line by construction, and a changed auth_version rewrites it rather than appending to it. The rendered content is byte-identical to what is on the hosts today, so this settles immediately instead of costing one more transitional change. The legacy filename apt_repository generated before it was given an explicit one is still removed, which is idempotent now that it is the only path in the task. Also stops cache_valid_time hiding a repository change: at 86400 the refresh could be skipped for up to a day after the line moved, and the pinned version would then simply not be found in the stale index. The window now collapses to zero when the repository file actually changed. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 95cab94 commit 373dafd

1 file changed

Lines changed: 27 additions & 10 deletions

File tree

powerdns_setup.yaml

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,41 @@
5656
cmd: gpg --dearmor < /tmp/powerdns-key.asc > /etc/apt/keyrings/powerdns.gpg
5757
creates: /etc/apt/keyrings/powerdns.gpg
5858

59-
- name: Remove old PowerDNS repository files
59+
# Only the legacy filename, which apt_repository generated before it was
60+
# given an explicit one. powerdns.list is managed declaratively below, so
61+
# deleting it here only removed the file the next task then recreated - which
62+
# is why both tasks reported changed on every single run, forever.
63+
- name: Remove the legacy PowerDNS repository file
6064
ansible.builtin.file:
61-
path: "{{ item }}"
65+
path: /etc/apt/sources.list.d/repo_powerdns_com_ubuntu.list
6266
state: absent
63-
loop:
64-
- /etc/apt/sources.list.d/repo_powerdns_com_ubuntu.list
65-
- /etc/apt/sources.list.d/powerdns.list
6667

68+
# Written as content rather than through apt_repository so the file holds
69+
# exactly one line and nothing else. apt_repository adds a line but will not
70+
# take a superseded one away, so moving auth_version - 4.9 to 5.0, say -
71+
# would leave the old suite sitting in the same file, still enabled.
72+
# Deleting the file first was the workaround for that, and idempotency was
73+
# what it cost. Declaring the content gets both.
6774
- name: Add PowerDNS repository
68-
ansible.builtin.apt_repository:
69-
repo: "deb [arch=amd64 signed-by=/etc/apt/keyrings/powerdns.gpg] http://repo.powerdns.com/ubuntu {{ ansible_distribution_release }}-auth-{{ powerdns.auth_version }} main"
70-
state: present
71-
filename: powerdns
75+
ansible.builtin.copy:
76+
dest: /etc/apt/sources.list.d/powerdns.list
77+
content: >
78+
deb [arch=amd64 signed-by=/etc/apt/keyrings/powerdns.gpg]
79+
http://repo.powerdns.com/ubuntu
80+
{{ ansible_distribution_release }}-auth-{{ powerdns.auth_version }} main
81+
owner: root
82+
group: root
83+
mode: "0644"
84+
register: powerdns_repo
7285

86+
# cache_valid_time alone would skip the refresh for up to a day, so a changed
87+
# repository line could be followed by an install resolved against the stale
88+
# index - the pinned version would simply not be found. Force the refresh
89+
# when the line actually moved, and keep the daily window otherwise.
7390
- name: Update apt cache
7491
ansible.builtin.apt:
7592
update_cache: yes
76-
cache_valid_time: 86400
93+
cache_valid_time: "{{ 0 if powerdns_repo.changed else 86400 }}"
7794

7895
- name: Configure DNS resolution
7996
ansible.builtin.copy:

0 commit comments

Comments
 (0)