9

I want to set an environment proxy only for a particular ansible task like get_url module to download some application from internet. Other all tasks should run without any proxy. How do I achieve this task.

rolz
  • 591
  • 2
  • 11
  • 23

2 Answers2

10

You can set a proxy per task, like so:

get_url:
  url=http://remote.host.com/file
  dest=/tmp/file
environment:
  http_proxy: http://proxy.example.com:8080
Zlemini
  • 4,827
  • 2
  • 21
  • 23
  • Wouldn't you have to define `environment` first and then put `http_proxy` inside `environment`? Like [this](https://pastebin.com/fxTgTwgh) – idjaw Apr 05 '18 at 18:25
  • 1
    Yes you can do it in 2 steps as you describe, my answer shows how do it inside one task. ( I actually omitted the `environment` keyword, thanks for the pointer. ) – Zlemini Apr 06 '18 at 17:49
7

You can define an environment variable for your play and set the proxy option from get_url.

---
- hosts: all

  environment:
    http_proxy: http://127.0.0.1:1234

    # You can also set it over https.
    https_proxy: http://127.0.0.1:1234

- name: Retrieve some repo
  get_url:
    url: https://repos.com/cool.repo
    dest: /etc/yum.repos.d/cool.repo
    use_proxy: yes   

From use_proxy in the documentation:

if [use_proxy is set to] no, it will not use a proxy, even if one is defined in an environment variable on the target hosts.

So, you'll be doing the opposite in the example above.

Yahir Cano
  • 83
  • 2
  • 5
  • 2
    Wouldn't setting this apply the proxy globally for all other tasks as well? If you wanted to isolate it to the task itself, we would want to put the `environment` setting with the task like [this](https://pastebin.com/fxTgTwgh)? – idjaw Apr 05 '18 at 18:27