Skip to content

Instantly share code, notes, and snippets.

@krisleech
Last active June 19, 2019 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krisleech/a5cbd8de43501ef42624d7ec187ffdd5 to your computer and use it in GitHub Desktop.
Save krisleech/a5cbd8de43501ef42624d7ec187ffdd5 to your computer and use it in GitHub Desktop.
[Ansible] Copying file to another server

Create some files (via templating) on the server

- name: Create backup configuration files
  template:
    src: "{{ item.type }}.yml.j2"
    dest: "/tmp/{{ item.project }}_{{ item.env }}_{{ item.type }}.yml"
  with_items:
    - "{{ backup }}"

METHOD 1: using scp and delegate_to

- name: Copy configuration to Backup server
  become: true
  shell: "scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null admin@{{ inventory_hostname }}:/tmp/{{ item.project }}_{{ item.env }}_{{ item.type }}.yml /{{ backup.install_path }}/config/backups/{{ item.project }}_{{ item.env }}_{{ item.type }}.yml"
  with_items:
    - "{{ backup }}"
  delegate_to: other.example.com

METHOD 2: using synchronize and delegate_to

- name: Copy configuration to Backup server
  synchronize:
    src: "/tmp/{{ item.project }}_{{ item.env }}_{{ item.type }}.yml"
    dest: "/{{ backup.install_path }}/config/backups/{{ item.project }}_{{ item.env }}_{{ item.type }}.yml"
    mode: pull
  delegate_to: other.example.com
  with_items:
    - "{{ backup }}"

mode: pull is important since the command (rsync) is run on the other server and it wants to pull the file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment