I could not understand something from the examples in the documentation - how can I copy a file from a remote server to a local server and vice versa? There are copy and fetch modules, for example. Here I am writing in the playbook:

- name: Copy file from remote host to local machine fetch: src=/tmp/somefile dest=/tmp/fetched(взято из доки) 

How to specify ansible, from which host I want to copy the file? Well, respectively, when copying to a remote host, where to specify this remote host?

PS I figured it out a bit: To copy a file from a remote host to a local host:

 - hosts: localhost vars_files: - config.yml tasks: - include: ../share/dev.yml - name: Get file from remote fetch: src="{{ remote_sources_path }}/test.txt" dest="backup" delegate_to: '{{ remote_host }}' tags: fetch 

The test.txt file will be in the directory ./backup/localhost / {{remote_sources_path}}

Copy from local to remote:

 - name: Send file to remote copy: src="{{ local_sources_path }}/to_remote_test.txt" dest={{ remote_sources_path }} delegate_to: '{{ remote_host }}' tags: fetch 

I suspect that if you specify at the beginning - hosts: {{remote_host}} instead of localhost, you do not need to use delegate_to.

    1 answer 1

    Well, why is it so difficult? ))

    All these tasks are solved without hassle with the Delegation and Local Actions , then it will be harder for you to understand your own playbooks or read others written in a simple way.

    See it.

    In each playbook you specify which hosts to start tasks with - write hosts: '{{ target }}' (if you want to manage the list from the command line using --extra-vars "target = 10.0.100.123") or hosts: dbservers (if you fix playbook for a group of hosts):

     --- # This playbook for quick tests. - name: quick tests hosts: '{{ target }}' become: true become_user: root roles: - role1 - role2 tasks: - name: install mc yum: name=mc state=latest - name: install wget yum: name=wget state=latest 

    These hosts are a “remote” server, but a “local” one is itself a host on which your playbook is located.

    Now:

    • To copy a file from a local server to a remote server, use the copy module.
    • To copy a file from a remote server to a local server, use the fetch module.

    Both in copy and in fetch src is where to get the file, and dest is where to put the file. For copy src = local and dest = remote, for fetch - vice versa src = remote, and dest = local.

    That's all. By the way, this is what you were talking about in the comment to your last question: "In general, Ansible implies that you are already at a remote host," you do not need to cut the extra loops around the local host.

    PS If you need to copy from an arbitrary host to an arbitrary host , then look towards the syncronyze module based on rsync. But in general, in 90% of cases you will need to copy from the local host to the remote one and you will use copy or template.

    • Thank you, I have already figured out this) But the question, I think, should not be removed, you never know, it will be useful for beginners ... Especially since at the end I wrote "I suspect that if you specify in the beginning - hosts: {{remote_host}} instead of localhost, you don't need to use delegate_to. " - Sviderskiy Dmitriy