How to copy file to local directory using Ansible?

折月煮酒 提交于 2019-12-08 06:24:06

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/40955440/how-to-copy-file-to-local-directory-using-ansible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!