对于ansible来说,下载文件是一个很重要的课题,这是build或者deploy的第一步,通常来讲由于不同项目的差异,可能我们的代码包或者资源文件保存在于http,github,nexus,ftp,nas等等。

从ansible server拷贝文件,前提是在使用ansible core

- name: Copy file with owner and permissions
  copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: '0644'

http文件下载,前提是http允许匿名用户下载

- name: download war file
  get_url:
    url: "{{ https_url }}/start.war"
    dest: /tmp
    mode: 0644
    force: yes
    validate_certs: no

github文件下载,前提是已经在github申请了token

- name: donwload docker rpm
  get_url:
    validate_certs: no
    url: https://github.com/raw/org_name/project/master/docker.rpm
    dest: /tmp/docker.rpm
    mode: 0755
    force: yes
    headers:
      Authorization: token xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

如果想让文件下载到ansible master端,只要增加一条

delegate_to: localhost

  • 一个完整的task,如下:
- name: donwload docker rpm
  get_url:
    validate_certs: no
    url: https://github.com/raw/org_name/project/master/docker.rpm
    dest: /tmp/docker.rpm
    mode: 0755
    force: yes
    headers:
      Authorization: token xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  delegate_to: localhost

李欢欢
10 声望2 粉丝

我是一个有多年实施和运维经验的男同学,对开发也很感兴趣,包括前端,python,web等等,


引用和评论

0 条评论