vars: http_proxies: - 192.168.1.5:3128 - proxy5.internal.example.com:3128 tasks: - name: learn which proxy to use script: get_first_open_port.py {{ http_proxies | join( " " ) }} changed_when: false register: open_ports - set_fact: http_proxy: "{{ open_ports.stdout_lines[0] }}" when: - 'open_ports.stdout | length > 0' failed_when: - 'open_ports.stdout | length = 0' - name: use http_proxy environment variable script: script_needing_internet.sh -i {{ inputvar }} environment: http_proxy: "{{ http_proxy | default(omit) }}"