Ansible内置模块之get_url
发布作者:微思网络 发布时间:2024-10-15 浏览量:0次
2. 用 例 # 下载文件到目标路径 选项 必须 类型 默认值 说明 url 是 str 无 要下载文件的 URL dest 是 str 无 目标文件路径 mode 否 str 无 目标文件的权限,如 0644、0755 等 owner 否 str 无 目标文件的所有者 group 否 str 无 目标文件的属组 checksum 否 str no 用于验证下载文件的 SHA256 校验和 force 否 bool 无 如果目标文件存在,是否强制覆盖 timeout 否 int no 设置下载的超时时间(秒) headers 否 list 无 用于下载请求的 HTTP 头 url_password 否 str 无 用于下载请求的密码 url_username 否 str 无 用于下载请求的用户名 validate_certs 否 bool yes 如果为 no,忽略 SSL 证书验证错误 use_proxy 否 bool yes 是否使用系统中定义的代理 client_cert 否 str 无 用于下载请求的客户端证书文件路径 client_key 否 str 无 用于下载请求的客户端密钥文件路径 http_agent 否 str ansible-httpget 设置下载请求的 HTTP User-Agent 头
- name: Download a file
ansible.builtin.get_url:
url: https://example.com/file.tar.gz
dest: /tmp/file.tar.gz
# 设置文件权限和所有者
- name: Download a file and set permissions and owner
ansible.builtin.get_url:
url: https://example.com/file.tar.gz
dest: /tmp/file.tar.gz
mode: '0644'
owner: myuser
group: mygroup
# 使用自定义HTTP 头
- name: Download a file with custom HTTP headers
ansible.builtin.get_url:
url: https://example.com/file.tar.gz
dest: /tmp/file.tar.gz
headers:
- 'Authorization: Basic {{ auth_token }}'
# 使用客户端证书和密钥
- name: Download a file with client certificate and key
ansible.builtin.get_url:
url: https://example.com/file.tar.gz
dest: /tmp/file.tar.gz
client_cert: /path/to/client_cert.pem
client_key: /path/to/client_key.pem
# 下载文件并强制覆盖现有文件
- name: Download a file and force overwrite if it exists
ansible.builtin.get_url:
url: https://example.com/file.tar.gz
dest: /tmp/file.tar.gz
force: yes
# 在下载文件时忽略SSL 证书错误
- name: Download a file and ignore SSL certificate errors
ansible.builtin.get_url:
url: https://example.com/file.tar.gz
dest: /tmp/file.tar.gz
validate_certs: no
# 下载带有自定义HTTP 头的文件
- name: Download file with custom HTTP headers
ansible.builtin.get_url:
url: http://example.com/path/file.conf
dest: /etc/foo.conf
headers:
key1: one
key2: two
# 下载并用checksum (sha256)验证文件完整性
- name: Download file with check (sha256)
ansible.builtin.get_url:
url: http://example.com/path/file.conf
dest: /etc/foo.conf
checksum: sha256:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c
# 下载并用checksum (md5)验证文件
- name: Download file with check (md5)
ansible.builtin.get_url:
url: http://example.com/path/file.conf
dest: /etc/foo.conf
checksum: md5:66dffb5228a211e61d6d7ef4a86f5758
# 下载并验证文件的校验和(变量提供校验和)
- name: Download a file and verify checksum
ansible.builtin.get_url:
url: https://example.com/file.tar.gz
dest: /tmp/file.tar.gz
checksum: 'sha256:{{ expected_checksum }}'
# 下载并用带有checksum (md5) 远程文件来验证文件
- name: Download file with checksum url (sha256)
ansible.builtin.get_url:
url: http://example.com/path/file.conf
dest: /etc/foo.conf
checksum: sha256:http://example.com/path/sha256sum.txt
# 获取需要身份验证的文件
- name: Fetch file that requires authentication.
# username/password only available since 2.8, in older versions you need to use url_username/url_password
ansible.builtin.get_url:
url: http://example.com/path/file.conf
dest: /etc/foo.conf
username: bar
password: '{{ mysecret }}'