我正在编写ansible playbooks,我想要实现的部分任务是从get_url
模块中获取文件。
这个剧本需要在一个必须代理的环境中运行,而在另一个没有代理的环境中运行(即文件的提取是直接的)。
我一直在努力使我的代码保持一致,就在这里。 注意:我也可以使用变量定义的情况#1和未定义的情况#2
group_vars /所有:
---
proxy_env:
http_proxy: <<i enter my proxy here>>
test.yml:
---
- hosts: control_nodes
tasks:
- name: Downloading file through proxy
get_url:
url=http://address-that-requires-proxy/file_thru_proxy.txt dest=/root/file_thru_proxy.txt force=yes use_proxy=yes
when: proxy_env.http_proxy is defined
- name: Downloading file not through proxy
get_url:
url=http://address-that-DOESNOT-require-proxy/file_not_thru_proxy.txt dest=/root/file_not_thru_proxy.txt force=yes
when: proxy_env.http_proxy is undefined
environment:
http_proxy: "{{ proxy_env.http_proxy }}"
案例#1:group_vars / all中的proxy_env.http_proxy已定义,一切正常:
[root@blade8 my_playbooks]# ansible-playbook -i hosts -s test.yml
...
ok: [blade1]
TASK: [Downloading file through proxy] *************************
ok: [blade1]
TASK: [Downloading file not through proxy] *********************
skipping: [blade1]
...
案例#2:group_vars / all中的proxy_env.http_proxy未定义(注释),失败:
[root@blade8 my_playbooks]# ansible-playbook -i hosts -s test.yml
...
TASK: [Downloading file through proxy] *************************
skipping: [blade1]
TASK: [Downloading file not through proxy] *********************
failed: [blade1] => {"dest": "/root/file_not_thru_proxy.txt", "failed": true, "response": "Request failed: <urlopen error [Errno -2] Name or service not known>", "state": "absent", "status_code": -1, "url": "http://address-that-DOESNOT-require-proxy/file_not_thru_proxy.txt"}
msg: Request failed
...
跳过第一个任务,因为变量未定义。但第二个应该工作,因为我可以用一个简单的wget命令下载它。
任何想法,发生了什么?或者更好地实施它的任何建议?
答案 0 :(得分:1)
问题可能是您在playbook级别设置了http_proxy。因此即使未在group_vars中定义它也会设置。这可能仍会导致get_url尝试通过未定义的代理获取URL ...不知道但值得尝试:
---
- hosts: control_nodes
tasks:
- name: Downloading file through proxy
get_url:
url: http://address-that-requires-proxy/file_thru_proxy.txt
dest: /root/file_thru_proxy.txt
force: yes
use_proxy: yes
when: "http_proxy" in proxy_env
environment:
http_proxy: "{{ proxy_env.http_proxy }}"
- name: Downloading file not through proxy
get_url:
url: http://address-that-DOESNOT-require-proxy/file_not_thru_proxy.txt
dest: /root/file_not_thru_proxy.txt
force: yes
when: "http_proxy" not in proxy_env