Ansible模块——通过 URL 下载文件

通过 URL 下载文件

ansible.builtin.get_url 可以通过 URL 下载文件。

选项名

类型

默认值

描述

attributesstrnull

设置文件系统对象的属性,格式参考 lsattr;支持 +-= 操作符。别名:attr

backupboolfalse

创建目标文件的备份副本(带时间戳)。

checksumstr''

提供格式为 <算法>:<值> 的校验和,如 sha256:abcd1234...,用于校验完整性并决定是否跳过下载。

cipherslistnull

指定 SSL/TLS 加密套件,多个套件以 : 连接。

client_certpathnull

PEM 格式的客户端证书,可包含私钥。

client_keypathnull

客户端私钥 PEM 文件;若 client_cert 已包含,则可省略。

decompressbooltrue

自动解压 gzip 编码响应内容。

destpath

下载文件保存的目标路径(必须)。若为目录,会使用服务器提供的文件名。

forceboolfalse

是否强制下载(即使目标文件存在)。只在 dest 是文件时有效。

force_basic_authboolfalse

是否强制初次请求就发送 Basic Auth 头信息(用于兼容不返回 401 的服务)。

groupstrnull

目标文件所属的组名。默认继承当前用户组。

headersdictnull

自定义 HTTP 请求头,格式为字典。

http_agentstransible-httpget

请求时发送的 User-Agent 标识。

moderawnull

文件权限,如 0644u=rw,g=r,o=r。建议使用字符串格式以避免意外。

ownerstrnull

目标文件所属用户名。默认为当前用户。

selevelstrnull

SELinux 上下文的 level 部分。

serolestrnull

SELinux 上下文的 role 部分。

setypestrnull

SELinux 上下文的 type 部分。

seuserstrnull

SELinux 上下文的 user 部分。

timeoutint10

URL 请求超时时间(秒)。

tmp_destpathnull

下载临时文件的目录,默认 ~/.ansible/tmp/,可以通过 remote_tmp 在配置文件设置默认位置。

unredirected_headerslist[]

不跟随重定向的 HTTP 头名称列表,适用于防止认证信息泄露。

unsafe_writesboolfalse

是否允许非原子写操作(如 Docker 下)。有数据一致性风险。

urlstr

下载的 URL,支持 httphttps 和 ftp

url_passwordstrnull

HTTP 基本认证密码。别名:password

url_usernamestrnull

HTTP 基本认证用户名。别名:username

use_gssapiboolfalse

是否使用 GSSAPI(Kerberos)进行认证。需要安装 gssapi Python 库。

use_netrcbooltrue

是否使用 ~/.netrc 中的凭据。

use_proxybooltrue

是否使用环境变量中的代理配置。

validate_certsbooltrue

是否校验 HTTPS 证书,设为 false 可跳过验证(如自签名证书)。

常用选项:

选项名

类型

默认值

描述

backupboolfalse

创建目标文件的备份副本(带时间戳)。

checksumstr''

提供格式为 <算法>:<值> 的校验和,如 sha256:abcd1234...,用于校验完整性并决定是否跳过下载。

destpath

下载文件保存的目标路径(必须)。若为目录,会使用服务器提供的文件名。

forceboolfalse

是否强制下载(即使目标文件存在)。只在 dest 是文件时有效。

force_basic_authboolfalse

是否强制初次请求就发送 Basic Auth 头信息(用于兼容不返回 401 的服务)。

groupstrnull

目标文件所属的组名。默认继承当前用户组。

headersdictnull

自定义 HTTP 请求头,格式为字典。

http_agentstransible-httpget

请求时发送的 User-Agent 标识。

moderawnull

文件权限,如 0644u=rw,g=r,o=r。建议使用字符串格式以避免意外。

ownerstrnull

目标文件所属用户名。默认为当前用户。

timeoutint10

URL 请求超时时间(秒)。

unredirected_headerslist[]

不跟随重定向的 HTTP 头名称列表,适用于防止认证信息泄露。

urlstr

下载的 URL,支持 httphttps 和 ftp

url_passwordstrnull

HTTP 基本认证密码。别名:password

url_usernamestrnull

HTTP 基本认证用户名。别名:username

use_proxybooltrue

是否使用环境变量中的代理配置。

validate_certsbooltrue

是否校验 HTTPS 证书,设为 false 可跳过验证(如自签名证书)。

- name: Download foo.conf
  ansible.builtin.get_url:
    url: http://example.com/path/file.conf
    dest: /etc/foo.conf
    mode: '0440'

- name: Download file and force basic auth
  ansible.builtin.get_url:
    url: http://example.com/path/file.conf
    dest: /etc/foo.conf
    force_basic_auth: yes

- 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

- name: Download file with check (sha256)
  ansible.builtin.get_url:
    url: http://example.com/path/file.conf
    dest: /etc/foo.conf
    checksum: sha256:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c

- 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 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: Download file from a file path
  ansible.builtin.get_url:
    url: file:///tmp/afile.txt
    dest: /tmp/afilecopy.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 }}'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

遇见火星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值