DataDog / ansible-datadog

Ansible role for Datadog Agent
Apache License 2.0
297 stars 222 forks source link

Add proxy usage for Windows agent install #547

Closed njt-cla closed 6 months ago

njt-cla commented 6 months ago

Hello, I have tried to install Datadog Agent on a Windows Server that has no direct access to internet. It is available through a Proxy. When trying to run the playbook to install Datadog Agent on a Windows Server, it fails because of this :

fatal: [myserver]: FAILED! => {
    "changed": false, 
    "dest": "C:\\Users\\MyUser\\AppData\\Local\\Temp\\fix_6_14.ps1", 
    "elapsed": 0, 
    "invocation": {
        "module_args": {
            "checksum": null, 
            "checksum_algorithm": "sha1", 
            "checksum_url": null, 
            "dest": "C:\\Users\\MyUser\\AppData\\Local\\Temp\\fix_6_14.ps1", 
            "force": true, 
            "force_basic_auth": false, 
            "headers": {}, 
            "proxy_password": null, 
            "proxy_url": null, 
            "proxy_username": null, 
            "timeout": 10, 
            "url": "https://s3.amazonaws.com/ddagent-windows-stable/scripts/fix_6_14.ps1", 
            "url_password": null, 
            "url_username": null, 
            "use_proxy": true, 
            "validate_certs": true
        }
    }, 
    "msg": "Error requesting 'https://s3.amazonaws.com/ddagent-windows-stable/scripts/fix_6_14.ps1'. Unable to connect to the remote server", 
    "status_code": 0, 
    "url": "https://s3.amazonaws.com/ddagent-windows-stable/scripts/fix_6_14.ps1"
}

Your role is executed from this playbook (with a requirements.yml file that points on the main branch of this repository) :

- hosts: all
  gather_facts: true
  roles:
  - datadog
  environment:
    HTTP_PROXY: "{{ http_proxy | default('') }}"
    HTTPS_PROXY: "{{ http_proxy | default('') }}"

The playbook is executed using this command :

ansible-playbook playbook.yml -i /my-inventory -l myserver -f 5 --private-key mykey.key -u myuser -vvvvvvvv -e http_proxy=http://myproxy:proxyport/ -e HTTP_PROXY=http://myproxy:proxyport/ -e HTTPS_PROXY=http://myproxy:proxyport/

These environment variables are visible in the Gather Facts command that runs at the beginning of the Playbook execution, but it is not used by the win_get_url module.

Is it possible to add a way to use a proxy in win_get_url commands you run for a Windows install ?

Thanks by advance.

Regards.

njt-cla commented 6 months ago

To give more information, I tried forking your repo and change "pkg-windows.yml" file to use a proxy like that :

- name: Download windows datadog agent 614 fix script
  win_get_url:
    url: "{{ datadog_windows_614_fix_script_url }}"
    dest: '%TEMP%\fix_6_14.ps1'
    proxy_url: "{{ HTTP_PROXY }}"
  when: not agent_datadog_skip_install and datadog_apply_windows_614_fix

# ...

- name: Download windows datadog agent
  win_get_url:
    url: "{{ agent_dd_download_url }}"
    dest: '%TEMP%\ddagent.msi'
    proxy_url: "{{ HTTP_PROXY }}"
  register: agent_download_msi_result
  when: (not agent_datadog_skip_install) and (not ansible_check_mode)

  # ...

Changing this and running back my playbook command (ansible-playbook playbook.yml -i my-inventory -l myserver -f 5 --private-key my.key -u myuser -v -e HTTP_PROXY=http://proxy.fqdn:proxy.port/ -e HTTPS_PROXY=http://proxy.fqdn:proxy.port/) is working for an unauthenticated proxy usage.

bkabrda commented 6 months ago

Hi @njt-cla :wave:. I put together PR https://github.com/DataDog/ansible-datadog/pull/553 that should help you - I'd be curious if you could provide feedback on it. First of all, it uses lowercase environment variable names (as recommended in official Ansible docs). Second, it also tells Ansible to omit the proxy_url value if http_proxy is not defined.

Let me know if you think this makes sense, thanks!

njt-cla commented 6 months ago

Hello, thanks for your fast answer. It seems to correct the issue I had. I close the Issue. Thanks :)