jschnasse
jschnasse

Reputation: 9588

Ansible - Best way to perform a local copy (in pre_tasks)?

I have a playbook that requires to copy some files from a local directory into another local directory (on the ansible host). What is the correct way to achieve this?

I found the copy module but it seems it is only for copying files to a remote host. I also found local_action but I'am very unsure how to use it.

The playbook looks something like:

---
- hosts: all
  vars:
    proprietary_files: "/some/files/not/managed/by/vcs"
    filesToWorkOnLater: "config_files"
  pre_tasks:
    - name "Copy from {{proprietary_files}} to {{filesToWorkOnLater}}"
   # What to enter here to perform the local copy?
   roles:
     ...   
...     

Upvotes: 1

Views: 7495

Answers (2)

Alassane Ndiaye
Alassane Ndiaye

Reputation: 4787

Ansible now supports import_role so pre_tasks aren't needed anymore. Something like this should fix you issue:

- hosts: all
  tasks:
  - command: cp file1 file2
    delegate_to: 127.0.0.1
  - import_role:
      name: ...

Upvotes: 3

Smily
Smily

Reputation: 2578

Have you tried delegate_to localhost? PLease try as below. (I have not tested)

  - name: copying to local
    copy:
     src: {{proprietary_files}}
     dest:  {{filesToWorkOnLater}}
    delegate_to: localhost

Upvotes: 1

Related Questions