Skip to content

Commit

Permalink
Infer major version from datadog_agent_version (#239)
Browse files Browse the repository at this point in the history
If datadog_agent_version is set, extracts the major version from it and sets datadog_agent_major_version with this value.
If datadog_agent_major_version is already set, checks that the two variables are compatible, otherwise raises an error.

Co-Authored-By: Albert Vaca <[email protected]>
  • Loading branch information
KSerrania and albertvaka authored Dec 16, 2019
1 parent 102d6ba commit 1e76b78
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ To override the default behavior, set the `datadog_windows_download_url` variabl
|-------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `datadog_api_key` | Your Datadog API key. |
| `datadog_site` | The site of the Datadog intake to send Agent data to. Defaults to `datadoghq.com`, set to `datadoghq.eu` to send data to the EU site. This option is only available with agent version >= 6.6.0. |
| `datadog_agent_major_version` | The major version of the Agent which will be installed. This variable must be set. Its possible values are 5, 6, 7. |
| `datadog_agent_version` | The pinned version of the Agent to install (optional, but highly recommended). Examples: `1:6.0.0-1` on apt-based platforms, `6.0.0-1` on yum-based platforms, `6.0.0` on Windows platforms. **Note** It must use the major version set in `datadog_agent_major_version`. **Note** Downgrades are not supported on Windows platforms. |
| `datadog_agent_major_version` | The major version of the Agent which will be installed. Its possible values are 5, 6, 7. If both this and `datadog_agent_version` are not set, then `datadog_agent_major_version` defaults to 7. If this is not set but `datadog_agent_version` is set, then `datadog_agent_major_version` is set to the major version provided in `datadog_agent_version`. |
| `datadog_agent_version` | The pinned version of the Agent to install (optional, but highly recommended). Examples: `1:6.0.0-1` on apt-based platforms, `6.0.0-1` on yum-based platforms, `6.0.0` on Windows platforms. If both this variable and `datadog_agent_major_version` are set, they must be compatible (the major version in `datadog_agent_version` has to be `datadog_agent_major_version`). **Note** Downgrades are not supported on Windows platforms. |
| `datadog_checks` | YAML configuration for agent checks to drop into: <br> - `/etc/datadog-agent/conf.d/<check_name>.d/conf.yaml` for Agent v6. <br> - `/etc/dd-agent/conf.d` for Agent v5. |
| `datadog_config` | Settings to place in the main Agent configuration file: <br> - `/etc/datadog-agent/datadog.yaml` for Agent v6 <br> - `/etc/dd-agent/datadog.conf` for Agent v5 (under the `[Main]` section). |
| `datadog_config_ex` | Extra INI sections to go in `/etc/dd-agent/datadog.conf` (optional). Agent v5 only. |
Expand Down
7 changes: 5 additions & 2 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ datadog_additional_groups: {}

# Major version of the Agent that will be installed.
# Possible values: 5, 6, 7
datadog_agent_major_version: 7
# By default, version 7 will be installed.
# If datadog_agent_version is defined, the major version will be deduced from it.
datadog_agent_major_version: ""

# Pin agent to a version. Highly recommended.
# Defaults to the latest version of the major version chosen in datadog_agent_major_version
# The version pinned needs to have the same major as datadog_agent_major_version
# If both datadog_agent_major_version and datadog_agent_version are set, they must be
# compatible (ie. the major version in datadog_agent_version must be datadog_agent_major_version)
datadog_agent_version: ""

# Default apt repo and keyserver
Expand Down
10 changes: 5 additions & 5 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- include_tasks: parse-version.yml
when: datadog_agent_version | length > 0

- include_tasks: set-parse-version.yml
run_once: true

- include_tasks: pkg-debian.yml
Expand All @@ -16,13 +16,13 @@
when: ansible_os_family == "Windows"

- include_tasks: agent5-linux.yml
when: datadog_agent_major_version == 5 and ansible_os_family != "Windows"
when: datadog_agent_major_version|int == 5 and ansible_os_family != "Windows"

- include_tasks: agent-linux.yml
when: datadog_agent_major_version > 5 and ansible_os_family != "Windows"
when: datadog_agent_major_version|int > 5 and ansible_os_family != "Windows"

- include_tasks: agent-win.yml
when: datadog_agent_major_version > 5 and ansible_os_family == "Windows"
when: datadog_agent_major_version|int > 5 and ansible_os_family == "Windows"

- include_tasks: integration.yml
when: datadog_integration is defined
Expand Down
13 changes: 13 additions & 0 deletions tasks/parse-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@
datadog_release: "1"
when: datadog_release | length == 0

- name: Stop play if datadog_agent_version and datadog_agent_major_version are not compatible
fail:
msg: "The provided major version {{ datadog_agent_major_version }} is not compatible with the
version {{ datadog_major }} deduced from datadog_agent_version ({{ datadog_agent_version }}).
Aborting play."
when: datadog_agent_major_version | length > 0 and datadog_major != datadog_agent_major_version
any_errors_fatal: true # Abort the whole play if an error is raised.
# Necessary since we run_once this task, without this only one host's run is stopped.

- name: Set datadog_agent_major_version to deduced value from datadog_agent_version
set_fact:
datadog_agent_major_version: "{{ datadog_major }}"

- name: Set OS-specific versions
set_fact:
datadog_agent_debian_version: "{{ datadog_epoch }}:{{ datadog_major }}.{{ datadog_minor }}.{{ datadog_bugfix }}{{ datadog_suffix }}-{{ datadog_release }}"
Expand Down
2 changes: 1 addition & 1 deletion tasks/pkg-debian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
apt_repository:
filename: datadog_agent
repo: "{{ item.value }}"
state: "{% if item.key == datadog_agent_major_version and datadog_apt_repo | length == 0 %}present{% else %}absent{% endif %}"
state: "{% if item.key == datadog_agent_major_version|int and datadog_apt_repo | length == 0 %}present{% else %}absent{% endif %}"
update_cache: yes
when: (not ansible_check_mode)
with_dict: "{
Expand Down
4 changes: 2 additions & 2 deletions tasks/pkg-redhat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
baseurl: "{{ item.value }}"
enabled: yes
gpgcheck: yes
gpgkey: "{% if datadog_agent_major_version == 7 %}{{ datadog_yum_gpgkey_e09422b3 }}{% else %}{{ datadog_yum_gpgkey }}{% endif %}"
state: "{% if item.key == datadog_agent_major_version and datadog_yum_repo | length == 0 %}present{% else %}absent{% endif %}"
gpgkey: "{% if datadog_agent_major_version|int == 7 %}{{ datadog_yum_gpgkey_e09422b3 }}{% else %}{{ datadog_yum_gpgkey }}{% endif %}"
state: "{% if item.key == datadog_agent_major_version|int and datadog_yum_repo | length == 0 %}present{% else %}absent{% endif %}"
when: (not ansible_check_mode)
with_dict: "{
5: '{{ datadog_agent5_yum_repo }}',
Expand Down
6 changes: 3 additions & 3 deletions tasks/pkg-suse.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@
args:
warn: no
when: not ddkey.stat.exists
when: datadog_agent_major_version < 7 and ansible_distribution_version|int == 11
when: datadog_agent_major_version|int < 7 and ansible_distribution_version|int == 11

# Do not import old key if installing Agent 7, as all Agent 7 packages are signed with the new key
- name: Download RPM key
get_url:
url: "{{ datadog_zypper_gpgkey }}"
dest: /tmp/DATADOG_RPM_KEY.public
checksum: "sha256:{{ datadog_zypper_gpgkey_sha256sum }}"
when: datadog_agent_major_version < 7 and ansible_distribution_version|int >= 12
when: datadog_agent_major_version|int < 7 and ansible_distribution_version|int >= 12

- name: Import RPM key
rpm_key:
key: /tmp/DATADOG_RPM_KEY.public
state: present
when: datadog_agent_major_version < 7 and not ansible_check_mode
when: datadog_agent_major_version|int < 7 and not ansible_check_mode

- block: # Work around due to SNI check for SLES11
- name: Stat if new RPM key already exists
Expand Down
2 changes: 1 addition & 1 deletion tasks/pkg-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
- name: Fail if Agent 5
fail:
msg: "The Datadog ansible role does not currently support Agent 5"
when: datadog_agent_major_version == 5
when: datadog_agent_major_version|int == 5

- name: Download windows datadog agent 614 fix script
win_get_url:
Expand Down
12 changes: 12 additions & 0 deletions tasks/set-parse-version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
- name: Convert datadog_agent_major_version to string
set_fact:
datadog_agent_major_version: "{{ datadog_agent_major_version | string }}"

- include_tasks: parse-version.yml
when: datadog_agent_version | length > 0

- name: Set Agent default major version
set_fact:
datadog_agent_major_version: "7"
when: datadog_agent_major_version | length == 0
3 changes: 2 additions & 1 deletion tasks/win_agent_latest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@

- name: Set agent download filename to latest
set_fact:
dd_download_url: "{% if datadog_agent_major_version == 7 %}{{ datadog_windows_agent7_latest_url }}{% else %}{{ datadog_windows_agent6_latest_url }}{% endif %}"
dd_download_url: "{% if datadog_agent_major_version|int == 7 %}{{ datadog_windows_agent7_latest_url }}
{% else %}{{ datadog_windows_agent6_latest_url }}{% endif %}"
when: datadog_windows_download_url | length == 0
6 changes: 3 additions & 3 deletions templates/zypper.repo.j2
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{% if datadog_zypper_repo | length > 0 %}
{% set baseurl = datadog_zypper_repo %}
{% elif datadog_agent_major_version == 5 %}
{% elif datadog_agent_major_version|int == 5 %}
{% set baseurl = datadog_agent5_zypper_repo %}
{% elif datadog_agent_major_version == 6 %}
{% elif datadog_agent_major_version|int == 6 %}
{% set baseurl = datadog_agent6_zypper_repo %}
{% elif datadog_agent_major_version == 7 %}
{% elif datadog_agent_major_version|int == 7 %}
{% set baseurl = datadog_agent7_zypper_repo %}
{% endif %}

Expand Down

0 comments on commit 1e76b78

Please sign in to comment.