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.
How to set proxy only for a particular ansible task?
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
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 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.
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.
environment
first and then puthttp_proxy
insideenvironment
? Like this – Clarendon