Jim
Jim

Reputation: 14300

How to copy file to local directory using Ansible?

I'm trying to set up a playbook that will configure my development system. I'd like to copy the /etc/hosts file from my playbooks "files" directory to the /etc directory on my system. Currently I'm doing the following:

# main.yml
- hosts: all
- tasks:
    - copy: src=files/hosts
            dest=/etc/hosts
            owner=root
            group=wheel
            mode=0644
            backup=true
      become: true
# inventory
localhost   ansible_connection=local

When I run the playbook I'm getting this error:

fatal: [localhost]: FAILED! => {... "msg": Failed to get information on remote file (/etc/hosts): MODULE FAILURE"}

I believe this is because copy is supposed to be used to copy a file to a remote file system. So how do you copy a file to your local management system? I did a Google Search and everything talks about doing the former. I didn't see this addressed in the Ansible docs.

Upvotes: 1

Views: 12552

Answers (1)

techraf
techraf

Reputation: 68649

Your task is ok.

You should add --ask-sudo-pass to the ansible-playbook call.


If you run with -vvv you can see the command starts with sudo -H -S -n -u root /bin/sh -c echo BECOME-SUCCESS-somerandomstring (followed by a call to the Python script). If you execute it yourself, you'll get sudo: a password is required message. Ansible quite unhelpfully replaces this error message with its own Failed to get information on remote file (/etc/hosts): MODULE FAILURE.

Upvotes: 4

Related Questions