I have two kinds of machines. One kind does not have internet access (say, we call them outgoing restricted), thus need a proxy to run command like dnf install, another kind can run dnf install directly.
I would like ansible to automatically use a proxy on those outgoing restricted hosts when running dnf install, thus I have the host file and the playbook below:
outgoing_restricted:
hosts:
bar1.foo.com:
bar2.foo.com:
vars:
# Sets up ssh forwarding for remote to use local machine
# Need to enable and start squid with http_port 3128
ansible_ssh_extra_args: "-R 3129:localhost:3128"
proxy_env:
http_proxy: "http://127.0.0.1:3129"
https_proxy: "http://127.0.0.1:3129"
- name: install redhat-lsb-core
dnf:
name:
- redhat-lsb-core
state: present
environment: "{{ proxy_env }}"
However, the playbook fails when running on non-outgoing restricted hosts because on those hosts proxy_env is not defined.
My question is whether there is a way to specify environment only when proxy_env is defined. Adding when: proxy_env is defined under environment seems not working.