How to set proxy only for a particular ansible task?
Asked Answered
S

2

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.

Spar answered 10/11, 2016 at 18:56 Comment(0)
M
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
Malonylurea answered 10/11, 2016 at 19:3 Comment(2)
Wouldn't you have to define environment first and then put http_proxy inside environment? Like thisClarendon
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. )Malonylurea
C
9

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.

Cq answered 17/5, 2017 at 20:48 Comment(1)
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?Clarendon

© 2022 - 2024 — McMap. All rights reserved.