Files

Document Control

TODO:

  • Overall structure.
  • Initial draft complete
  • Testing
  • Ready

Copying to a remote host

To copy a file from the ansible controller, you can use the copy module. When using a role structure, the files directory acts as a relative path.

Copy content to a file
copy a file to a remote host
- name: Copy file with owner and permissions
  copy:
    src: foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: '0644'
Copy files on the remote node by setting remote_src
- name: Copy a "sudoers" file on the remote machine for editing
  copy:
    src: /etc/sudoers
    dest: /etc/sudoers.edit
    remote_src: yes
    validate: /usr/sbin/visudo -csf %s

Fetch: copy file from a remote host

Copy a file from the remote host to the ansible controller
- name: Fetch remote file into /tmp/fetched/host.example.com/tmp/file
  fetch:
    src: /tmp/file
    dest: /tmp/fetched

Syncing files

Synchronize is generally faster than copy, but requires rsync

Synchronize a lot of files
- name: Synchronize passing in extra rsync options
  synchronize:
    src: myapp
    dest: /var/www/myapp
    rsync_opts:
      - "--no-motd"
      - "--exclude=.git"

get_url: Transfering files from the internet

Download files with a checksum URL
- name: Download file with checksum url (sha256)
  get_url:
    url: http://example.com/path/file.conf
    dest: /etc/foo.conf
    checksum: sha256:http://example.com/path/sha256sum.txt

Using checksum for idempotence prevents re-downloading files if they already exist.


Last update: 2020-01-19