[Ansible]proxy環境下でget_urlモジュールを使用する
作成するに至った経緯
- proxy環境下でplaybookを実行した際につまづいたことを外部記憶として残したかったため。
- 自宅がproxy環境下ではないため、十中八九忘れてしまうため。
実行環境
# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
# ansible --version
ansible 2.8.5
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /bin/ansible
python version = 2.7.5 (default, Aug 7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
proxy環境下でget_urlモジュールを実行するplaybook
- name: get_url under proxy enviroment
hosts: $inventoryファイルに定義しているparam
enviroment:
http_proxy: http://proxy_ip:port_num
https_proxy: http://proxy_ip:port_num
tasks:
- name: Download the specified file under proxy environment
get_url:
url: $ダウンロードしたいファイルのURL
dest: /usr/local/src
mode: '0755'
use_proxy: yes
validate_certs: no
register: download_result
until: download_result | succeeded
retries: 3
備忘録1
必要最低限のparamだけ記載するとダウンロードに失敗することがあったため、下記を明記しております。
get_url:
(省略)
use_proxy: yes
validate_certs: no
ダウンロードに失敗する理由に関しましては、参考URLの wgetでこういう時はこうする から言葉を引用いたしますと、
httpsなサイトから、sslに対応していないwgetを使うとエラーになるので--no-check-certificateで無視して落とせる
ちなみに、手動で操作した場合を wget コマンドに例えると下記のようなイメージになります。
# wget <$ダウンロードしたいファイルのURL> --no-check-certificate
備忘録2
download _resultの状態が"succeeded"になるまで、3回までリトライする。
register: download_result
until: download_result | succeeded
retries: 3
何回かdownloadに失敗することがあったので、リトライ処理を追記しております。