Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[debian] Fix role idempotence #262

Merged
merged 2 commits into from
Feb 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions tasks/pkg-debian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
state: present
when: datadog_apt_key_url_new is defined

- name: Remove previous datadog apt list file
file:
path: /etc/apt/sources.list.d/ansible_datadog_agent.list
state: absent

- name: Ensure Datadog non-https repositories are deprecated
apt_repository:
repo: "{{ item }}"
Expand All @@ -41,7 +36,7 @@

- name: Ensure Datadog repository is up-to-date
apt_repository:
filename: ansible_datadog_agent
filename: "ansible_datadog_{{ item.key }}"
repo: "{{ item.value }}"
state: "{% if item.key == datadog_agent_major_version|int and datadog_apt_repo | length == 0 %}present{% else %}absent{% endif %}"
update_cache: yes
Expand All @@ -51,9 +46,31 @@
6: '{{ datadog_agent6_apt_repo }}'
7: '{{ datadog_agent7_apt_repo }}'

- name: Initialize custom repo file deletion flag to False
set_fact:
datadog_remove_custom_repo_file: "False"

- name: Check if custom repository file exists
stat:
path: /etc/apt/sources.list.d/ansible_datadog_custom.list
register: datadog_custom_repo_file

- name: Flag custom repository file for deletion if different from current repository config
set_fact:
datadog_remove_custom_repo_file: "{{ datadog_repo_file_contents != datadog_apt_repo }}"
vars:
datadog_repo_file_contents: "{{ lookup('file', '/etc/apt/sources.list.d/ansible_datadog_custom.list') }}"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm getting the following error when running this change on my local system against my production inventory:

TASK [Datadog.datadog : Flag custom repository file for deletion if different from current repository config] ************************************************************************************************************************
task path: Datadog.datadog/tasks/pkg-debian.yml:58
[WARNING]: Unable to find '/etc/apt/sources.list.d/ansible_datadog_custom.list' in expected paths (use -vvvvv to see paths)
fatal: [basil-docker.dcenter.delphix.com]: FAILED! => {"msg": "An unhandled exception occurred while templating '{{ lookup('file', '/etc/apt/sources.list.d/ansible_datadog_custom.list') }}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: An unhandled exception occurred while running the lookup plugin 'file'. Error was a <class 'ansible.errors.AnsibleError'>, original message: could not locate file in lookup: /etc/apt/sources.list.d/ansible_datadog_custom.list"}

The /etc/apt/sources.list.d/ansible_datadog_custom.list file exists on the system in my production inventory, but it does not exist on my local system.

Looking at the documentation for lookup plugins, I see the following:

Like all templating, these plugins are evaluated on the Ansible control machine, not on the target/remote.

Perhaps we should be using the slurp module to read the file from the remote node instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @basil,

Thanks for the report! Indeed, using a lookup plugin doesn't work as we wanted it to do. I'll try using the slurp module instead and do a bugfix release with this if that works.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the fix: #275

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick fix! I just tested this and it works.

when: datadog_custom_repo_file.stat.exists

- name: (Custom) Remove Datadog custom repository file when not set or updated
file:
path: /etc/apt/sources.list.d/ansible_datadog_custom.list
state: absent
when: (datadog_apt_repo | length == 0) or datadog_remove_custom_repo_file and (not ansible_check_mode)

- name: (Custom) Ensure Datadog repository is up-to-date
apt_repository:
filename: ansible_datadog_agent
filename: ansible_datadog_custom
repo: "{{ datadog_apt_repo }}"
state: present
update_cache: yes
Expand Down