当前位置:首页>微思动态 > >详情
全国热线电话 400-881-4699

在线留言

Ansible内置模块之get_url

发布作者:微思网络   发布时间:2024-10-15   浏览量:0

640.png


Ansible内置模块之 get_url

ansible.builtin.get_url 模块用于从 HTTP、HTTPS 或 FTP 服务器下载文件到目标主机。它支持验证下载的文件、设置文件权限和所有者等功能。


1. 选 项 说 明

选项必须类型默认值说明
urlstr要下载文件的 URL
deststr目标文件路径
modestr目标文件的权限,如 0644、0755 等
ownerstr目标文件的所有者
groupstr目标文件的属组
checksumstrno用于验证下载文件的 SHA256 校验和
forcebool如果目标文件存在,是否强制覆盖
timeoutintno设置下载的超时时间(秒)
headerslist用于下载请求的 HTTP 头
url_passwordstr用于下载请求的密码
url_usernamestr用于下载请求的用户名
validate_certsboolyes如果为 no,忽略 SSL 证书验证错误
use_proxyboolyes是否使用系统中定义的代理
client_certstr用于下载请求的客户端证书文件路径
client_keystr用于下载请求的客户端密钥文件路径
http_agentstransible-httpget设置下载请求的 HTTP User-Agent 头


2. 用 例

# 下载文件到目标路径
- 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 }}'

          

Ansible相关文章推荐


RHCE.jpg



返回顶部